src/json.c

changeset 1319
aa1f580f8f59
parent 1318
12fa1d37fe48
equal deleted inserted replaced
1318:12fa1d37fe48 1319:aa1f580f8f59
498 bool escape = c < 0x20 || c == '\\' || c == '"' 498 bool escape = c < 0x20 || c == '\\' || c == '"'
499 || (escape_slash && c == '/'); 499 || (escape_slash && c == '/');
500 500
501 if (all_printable && escape) { 501 if (all_printable && escape) {
502 size_t capa = str.length + 32; 502 size_t capa = str.length + 32;
503 char *space = cxMalloc(cxDefaultAllocator, capa); 503 char *space = cxMallocDefault(capa);
504 if (space == NULL) return cx_mutstrn(NULL, 0); 504 if (space == NULL) return cx_mutstrn(NULL, 0);
505 cxBufferInit(&buf, space, capa, NULL, CX_BUFFER_AUTO_EXTEND); 505 cxBufferInit(&buf, space, capa, NULL, CX_BUFFER_AUTO_EXTEND);
506 cxBufferWrite(str.ptr, 1, i, &buf); 506 cxBufferWrite(str.ptr, 1, i, &buf);
507 all_printable = false; 507 all_printable = false;
508 } 508 }
629 } 629 }
630 630
631 void cxJsonDestroy(CxJson *json) { 631 void cxJsonDestroy(CxJson *json) {
632 cxBufferDestroy(&json->buffer); 632 cxBufferDestroy(&json->buffer);
633 if (json->states != json->states_internal) { 633 if (json->states != json->states_internal) {
634 cxFree(cxDefaultAllocator, json->states); 634 cxFreeDefault(json->states);
635 } 635 }
636 if (json->vbuf != json->vbuf_internal) { 636 if (json->vbuf != json->vbuf_internal) {
637 cxFree(cxDefaultAllocator, json->vbuf); 637 cxFreeDefault(json->vbuf);
638 } 638 }
639 cxJsonValueFree(json->parsed); 639 cxJsonValueFree(json->parsed);
640 json->parsed = NULL; 640 json->parsed = NULL;
641 if (json->uncompleted_member.name.ptr != NULL) { 641 if (json->uncompleted_member.name.ptr != NULL) {
642 cx_strfree_a(json->allocator, &json->uncompleted_member.name); 642 cx_strfree_a(json->allocator, &json->uncompleted_member.name);
982 static void json_arr_free_temp(CxJsonValue** values, size_t count) { 982 static void json_arr_free_temp(CxJsonValue** values, size_t count) {
983 for (size_t i = 0; i < count; i++) { 983 for (size_t i = 0; i < count; i++) {
984 if (values[i] == NULL) break; 984 if (values[i] == NULL) break;
985 cxJsonValueFree(values[i]); 985 cxJsonValueFree(values[i]);
986 } 986 }
987 cxFree(cxDefaultAllocator, values); 987 cxFreeDefault(values);
988 } 988 }
989 // LCOV_EXCL_STOP 989 // LCOV_EXCL_STOP
990 990
991 int cxJsonArrAddNumbers(CxJsonValue* arr, const double* num, size_t count) { 991 int cxJsonArrAddNumbers(CxJsonValue* arr, const double* num, size_t count) {
992 CxJsonValue** values = cxCalloc(cxDefaultAllocator, count, sizeof(CxJsonValue*)); 992 CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*));
993 if (values == NULL) return -1; 993 if (values == NULL) return -1;
994 for (size_t i = 0; i < count; i++) { 994 for (size_t i = 0; i < count; i++) {
995 values[i] = cxJsonCreateNumber(arr->allocator, num[i]); 995 values[i] = cxJsonCreateNumber(arr->allocator, num[i]);
996 if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } 996 if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; }
997 } 997 }
998 int ret = cxJsonArrAddValues(arr, values, count); 998 int ret = cxJsonArrAddValues(arr, values, count);
999 cxFree(cxDefaultAllocator, values); 999 cxFreeDefault(values);
1000 return ret; 1000 return ret;
1001 } 1001 }
1002 1002
1003 int cxJsonArrAddIntegers(CxJsonValue* arr, const int64_t* num, size_t count) { 1003 int cxJsonArrAddIntegers(CxJsonValue* arr, const int64_t* num, size_t count) {
1004 CxJsonValue** values = cxCalloc(cxDefaultAllocator, count, sizeof(CxJsonValue*)); 1004 CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*));
1005 if (values == NULL) return -1; 1005 if (values == NULL) return -1;
1006 for (size_t i = 0; i < count; i++) { 1006 for (size_t i = 0; i < count; i++) {
1007 values[i] = cxJsonCreateInteger(arr->allocator, num[i]); 1007 values[i] = cxJsonCreateInteger(arr->allocator, num[i]);
1008 if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } 1008 if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; }
1009 } 1009 }
1010 int ret = cxJsonArrAddValues(arr, values, count); 1010 int ret = cxJsonArrAddValues(arr, values, count);
1011 cxFree(cxDefaultAllocator, values); 1011 cxFreeDefault(values);
1012 return ret; 1012 return ret;
1013 } 1013 }
1014 1014
1015 int cxJsonArrAddStrings(CxJsonValue* arr, const char* const* str, size_t count) { 1015 int cxJsonArrAddStrings(CxJsonValue* arr, const char* const* str, size_t count) {
1016 CxJsonValue** values = cxCalloc(cxDefaultAllocator, count, sizeof(CxJsonValue*)); 1016 CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*));
1017 if (values == NULL) return -1; 1017 if (values == NULL) return -1;
1018 for (size_t i = 0; i < count; i++) { 1018 for (size_t i = 0; i < count; i++) {
1019 values[i] = cxJsonCreateString(arr->allocator, str[i]); 1019 values[i] = cxJsonCreateString(arr->allocator, str[i]);
1020 if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } 1020 if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; }
1021 } 1021 }
1022 int ret = cxJsonArrAddValues(arr, values, count); 1022 int ret = cxJsonArrAddValues(arr, values, count);
1023 cxFree(cxDefaultAllocator, values); 1023 cxFreeDefault(values);
1024 return ret; 1024 return ret;
1025 } 1025 }
1026 1026
1027 int cxJsonArrAddCxStrings(CxJsonValue* arr, const cxstring* str, size_t count) { 1027 int cxJsonArrAddCxStrings(CxJsonValue* arr, const cxstring* str, size_t count) {
1028 CxJsonValue** values = cxCalloc(cxDefaultAllocator, count, sizeof(CxJsonValue*)); 1028 CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*));
1029 if (values == NULL) return -1; 1029 if (values == NULL) return -1;
1030 for (size_t i = 0; i < count; i++) { 1030 for (size_t i = 0; i < count; i++) {
1031 values[i] = cxJsonCreateCxString(arr->allocator, str[i]); 1031 values[i] = cxJsonCreateCxString(arr->allocator, str[i]);
1032 if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } 1032 if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; }
1033 } 1033 }
1034 int ret = cxJsonArrAddValues(arr, values, count); 1034 int ret = cxJsonArrAddValues(arr, values, count);
1035 cxFree(cxDefaultAllocator, values); 1035 cxFreeDefault(values);
1036 return ret; 1036 return ret;
1037 } 1037 }
1038 1038
1039 int cxJsonArrAddLiterals(CxJsonValue* arr, const CxJsonLiteral* lit, size_t count) { 1039 int cxJsonArrAddLiterals(CxJsonValue* arr, const CxJsonLiteral* lit, size_t count) {
1040 CxJsonValue** values = cxCalloc(cxDefaultAllocator, count, sizeof(CxJsonValue*)); 1040 CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*));
1041 if (values == NULL) return -1; 1041 if (values == NULL) return -1;
1042 for (size_t i = 0; i < count; i++) { 1042 for (size_t i = 0; i < count; i++) {
1043 values[i] = cxJsonCreateLiteral(arr->allocator, lit[i]); 1043 values[i] = cxJsonCreateLiteral(arr->allocator, lit[i]);
1044 if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } 1044 if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; }
1045 } 1045 }
1046 int ret = cxJsonArrAddValues(arr, values, count); 1046 int ret = cxJsonArrAddValues(arr, values, count);
1047 cxFree(cxDefaultAllocator, values); 1047 cxFreeDefault(values);
1048 return ret; 1048 return ret;
1049 } 1049 }
1050 1050
1051 int cxJsonArrAddValues(CxJsonValue* arr, CxJsonValue* const* val, size_t count) { 1051 int cxJsonArrAddValues(CxJsonValue* arr, CxJsonValue* const* val, size_t count) {
1052 CxArrayReallocator value_realloc = cx_array_reallocator(arr->allocator, NULL); 1052 CxArrayReallocator value_realloc = cx_array_reallocator(arr->allocator, NULL);

mercurial