src/json.c

changeset 1594
4b9537f93239
parent 1592
c1e17411004f
equal deleted inserted replaced
1593:0df5128ab675 1594:4b9537f93239
1480 assert(false); 1480 assert(false);
1481 return -1; 1481 return -1;
1482 // LCOV_EXCL_STOP 1482 // LCOV_EXCL_STOP
1483 } 1483 }
1484 } 1484 }
1485
1486 CxJsonValue* cxJsonClone(const CxJsonValue* value, const CxAllocator* allocator) {
1487 return cx_json_clone_func(NULL, value, allocator, NULL);
1488 }
1489
1490 CxJsonValue* cx_json_clone_func(CxJsonValue* target, const CxJsonValue* source,
1491 const CxAllocator* allocator, cx_attr_unused void *data) {
1492 if (source == NULL || source->type == CX_JSON_NOTHING) {
1493 return &cx_json_value_nothing;
1494 }
1495
1496 #define return_value(v) { \
1497 CxJsonValue *ret = v; \
1498 if (target == NULL) { \
1499 return ret; \
1500 } else { \
1501 *target = *ret; \
1502 cxFree(allocator, ret); \
1503 return target; \
1504 } \
1505 }
1506
1507 switch (source->type) {
1508 case CX_JSON_OBJECT: {
1509 CxJsonValue *obj = cxJsonCreateObj(allocator);
1510 if (obj == NULL) return NULL; // LCOV_EXCL_LINE
1511 if (cxMapClone(obj->object, source->object, cxJsonCloneFunc, allocator, NULL)) {
1512 // LCOV_EXCL_START
1513 cxJsonValueFree(obj);
1514 return NULL;
1515 // LCOV_EXCL_STOP
1516 }
1517 return obj;
1518 }
1519 case CX_JSON_ARRAY: {
1520 const size_t elem_count = source->array.data_size;
1521 CxArrayReallocator reallocator = cx_array_reallocator(allocator, NULL);
1522 CxJsonValue *arr = cxJsonCreateArr(allocator);
1523 if (arr == NULL) return NULL; // LCOV_EXCL_LINE
1524 if (cx_array_simple_reserve_a(&reallocator, arr->array.data, elem_count)) {
1525 // LCOV_EXCL_START
1526 cxJsonValueFree(arr);
1527 return NULL;
1528 // LCOV_EXCL_STOP
1529 }
1530 arr->array.data_size = elem_count;
1531 for (size_t i = 0 ; i < elem_count ; i++) {
1532 CxJsonValue *e = cx_json_clone_func(NULL, source->array.data[i], allocator, NULL);
1533 if (e == NULL) {
1534 // LCOV_EXCL_START
1535 cxJsonValueFree(arr);
1536 return NULL;
1537 // LCOV_EXCL_STOP
1538 }
1539 arr->array.data[i] = e;
1540 }
1541 return arr;
1542 }
1543 case CX_JSON_STRING:
1544 return_value(cxJsonCreateString(allocator, source->string));
1545 case CX_JSON_INTEGER:
1546 return_value(cxJsonCreateNumber(allocator, source->integer));
1547 case CX_JSON_NUMBER:
1548 return_value(cxJsonCreateNumber(allocator, source->number));
1549 case CX_JSON_LITERAL:
1550 return_value(cxJsonCreateLiteral(allocator, source->literal));
1551 default:
1552 // LCOV_EXCL_START
1553 // unreachable
1554 assert(false);
1555 return NULL;
1556 // LCOV_EXCL_STOP
1557 }
1558 #undef return_value
1559 }

mercurial