src/json.c

changeset 1583
97fc8c55aeea
parent 1582
32b82c424252
child 1584
7a14af59d356
equal deleted inserted replaced
1582:32b82c424252 1583:97fc8c55aeea
451 cxBufferDestroy(&buf); 451 cxBufferDestroy(&buf);
452 return ret; 452 return ret;
453 } 453 }
454 454
455 static CxJsonObject json_create_object_map(const CxAllocator *allocator) { 455 static CxJsonObject json_create_object_map(const CxAllocator *allocator) {
456 // TODO: we might want to add a comparator that is sorting the elements by their key
457 CxMap *map = cxKvListCreateAsMap(allocator, NULL, CX_STORE_POINTERS); 456 CxMap *map = cxKvListCreateAsMap(allocator, NULL, CX_STORE_POINTERS);
458 if (map == NULL) return NULL; // LCOV_EXCL_LINE 457 if (map == NULL) return NULL; // LCOV_EXCL_LINE
458 // TODO: fix the specification of the compare function
459 map->collection.cmpfunc = (cx_compare_func) cxJsonCompare;
459 cxDefineDestructor(map, cxJsonValueFree); 460 cxDefineDestructor(map, cxJsonValueFree);
460 return map; 461 return map;
461 } 462 }
462 463
463 static void json_free_object_map(CxJsonObject obj) { 464 static void json_free_object_map(CxJsonObject obj) {
1439 1440
1440 cxmutstr cxJsonToPrettyString(CxJsonValue *value, const CxAllocator *allocator) { 1441 cxmutstr cxJsonToPrettyString(CxJsonValue *value, const CxAllocator *allocator) {
1441 CxJsonWriter writer = cxJsonWriterPretty(true); 1442 CxJsonWriter writer = cxJsonWriterPretty(true);
1442 return cx_json_to_string(value, allocator, &writer); 1443 return cx_json_to_string(value, allocator, &writer);
1443 } 1444 }
1445
1446 int cxJsonCompare(const CxJsonValue *json, const CxJsonValue *other) {
1447 if (json == NULL && other == NULL) return 0;
1448 if (json == NULL || other == NULL) return -1;
1449 if (json->type != other->type) return -1;
1450 switch (json->type) {
1451 case CX_JSON_NOTHING:
1452 return 0;
1453 case CX_JSON_OBJECT:
1454 return cxMapCompare(json->object, other->object);
1455 case CX_JSON_ARRAY:
1456 if (json->array.data_size != other->array.data_size) return -1;
1457 for (size_t i = 0; i < json->array.data_size; i++) {
1458 const int d = cxJsonCompare(json->array.data[i], other->array.data[i]);
1459 if (d != 0) return d;
1460 }
1461 return 0;
1462 case CX_JSON_STRING:
1463 return cx_strcmp(json->string, other->string);
1464 case CX_JSON_INTEGER:
1465 return cx_vcmp_int64(json->integer, other->integer);
1466 case CX_JSON_NUMBER:
1467 return cx_vcmp_double(json->number, other->number);
1468 case CX_JSON_LITERAL:
1469 return json->literal == other->literal ? 0 : -1;
1470 default:
1471 // LCOV_EXCL_START
1472 // unreachable
1473 assert(false);
1474 return -1;
1475 // LCOV_EXCL_STOP
1476 }
1477 }

mercurial