| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | ||
| 3 | * | ||
| 4 | * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved. | ||
| 5 | * | ||
| 6 | * Redistribution and use in source and binary forms, with or without | ||
| 7 | * modification, are permitted provided that the following conditions are met: | ||
| 8 | * | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * | ||
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 26 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "cx/json.h" | ||
| 30 | |||
| 31 | #include <string.h> | ||
| 32 | #include <assert.h> | ||
| 33 | #include <stdio.h> | ||
| 34 | #include <inttypes.h> | ||
| 35 | |||
| 36 | /* | ||
| 37 | * RFC 8259 | ||
| 38 | * https://tools.ietf.org/html/rfc8259 | ||
| 39 | */ | ||
| 40 | |||
| 41 | static CxJsonValue cx_json_value_nothing = {.type = CX_JSON_NOTHING}; | ||
| 42 | |||
| 43 | 285 | static int json_cmp_objvalue(const void *l, const void *r) { | |
| 44 | 285 | const CxJsonObjValue *left = l; | |
| 45 | 285 | const CxJsonObjValue *right = r; | |
| 46 | 285 | return cx_strcmp(cx_strcast(left->name), cx_strcast(right->name)); | |
| 47 | } | ||
| 48 | |||
| 49 | 32 | static CxJsonObjValue *json_find_objvalue(const CxJsonValue *obj, cxstring name) { | |
| 50 | assert(obj->type == CX_JSON_OBJECT); | ||
| 51 | CxJsonObjValue kv_dummy; | ||
| 52 | 32 | kv_dummy.name = cx_mutstrn((char*) name.ptr, name.length); | |
| 53 | 32 | size_t index = cx_array_binary_search( | |
| 54 | 32 | obj->value.object.values, | |
| 55 | 32 | obj->value.object.values_size, | |
| 56 | sizeof(CxJsonObjValue), | ||
| 57 | &kv_dummy, | ||
| 58 | json_cmp_objvalue | ||
| 59 | ); | ||
| 60 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | if (index == obj->value.object.values_size) { |
| 61 | ✗ | return NULL; | |
| 62 | } else { | ||
| 63 | 32 | return &obj->value.object.values[index]; | |
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | 92 | static int json_add_objvalue(CxJsonValue *objv, CxJsonObjValue member) { | |
| 68 | assert(objv->type == CX_JSON_OBJECT); | ||
| 69 | 92 | const CxAllocator * const al = objv->allocator; | |
| 70 | 92 | CxJsonObject *obj = &(objv->value.object); | |
| 71 | |||
| 72 | // determine the index where we need to insert the new member | ||
| 73 | 92 | size_t index = cx_array_binary_search_sup( | |
| 74 | 92 | obj->values, | |
| 75 | obj->values_size, | ||
| 76 | sizeof(CxJsonObjValue), | ||
| 77 | &member, json_cmp_objvalue | ||
| 78 | ); | ||
| 79 | |||
| 80 | // is the name already present? | ||
| 81 |
3/4✓ Branch 0 taken 35 times.
✓ Branch 1 taken 57 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 35 times.
|
92 | if (index < obj->values_size && 0 == json_cmp_objvalue(&member, &obj->values[index])) { |
| 82 | // free the original value | ||
| 83 | ✗ | cx_strfree_a(al, &obj->values[index].name); | |
| 84 | ✗ | cxJsonValueFree(obj->values[index].value); | |
| 85 | // replace the item | ||
| 86 | ✗ | obj->values[index] = member; | |
| 87 | |||
| 88 | // nothing more to do | ||
| 89 | ✗ | return 0; | |
| 90 | } | ||
| 91 | |||
| 92 | // determine the old capacity and reserve for one more element | ||
| 93 | 92 | CxArrayReallocator arealloc = cx_array_reallocator(al, NULL); | |
| 94 | 92 | size_t oldcap = obj->values_capacity; | |
| 95 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
|
92 | if (cx_array_simple_reserve_a(&arealloc, obj->values, 1)) return 1; |
| 96 | |||
| 97 | // check the new capacity, if we need to realloc the index array | ||
| 98 | 92 | size_t newcap = obj->values_capacity; | |
| 99 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
|
92 | if (newcap > oldcap) { |
| 100 | ✗ | if (cxReallocateArray(al, &obj->indices, newcap, sizeof(size_t))) { | |
| 101 | ✗ | return 1; | |
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | // check if append or insert | ||
| 106 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 57 times.
|
92 | if (index < obj->values_size) { |
| 107 | // move the other elements | ||
| 108 | 35 | memmove( | |
| 109 | 35 | &obj->values[index+1], | |
| 110 | 35 | &obj->values[index], | |
| 111 | 35 | (obj->values_size - index) * sizeof(CxJsonObjValue) | |
| 112 | ); | ||
| 113 | // increase indices for the moved elements | ||
| 114 |
2/2✓ Branch 0 taken 75 times.
✓ Branch 1 taken 35 times.
|
110 | for (size_t i = 0; i < obj->values_size ; i++) { |
| 115 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 29 times.
|
75 | if (obj->indices[i] >= index) { |
| 116 | 46 | obj->indices[i]++; | |
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | // insert the element and set the index | ||
| 122 | 92 | obj->values[index] = member; | |
| 123 | 92 | obj->indices[obj->values_size] = index; | |
| 124 | 92 | obj->values_size++; | |
| 125 | |||
| 126 | 92 | return 0; | |
| 127 | } | ||
| 128 | |||
| 129 | 233 | static void token_destroy(CxJsonToken *token) { | |
| 130 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 228 times.
|
233 | if (token->allocated) { |
| 131 | 5 | cx_strfree(&token->content); | |
| 132 | } | ||
| 133 | 233 | } | |
| 134 | |||
| 135 | 188 | static bool json_isdigit(char c) { | |
| 136 | // TODO: remove once UCX has public API for this | ||
| 137 |
4/4✓ Branch 0 taken 183 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 179 times.
✓ Branch 3 taken 4 times.
|
188 | return c >= '0' && c <= '9'; |
| 138 | } | ||
| 139 | |||
| 140 | 392 | static bool json_isspace(char c) { | |
| 141 | // TODO: remove once UCX has public API for this | ||
| 142 |
9/12✓ Branch 0 taken 315 times.
✓ Branch 1 taken 77 times.
✓ Branch 2 taken 293 times.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 293 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 254 times.
✓ Branch 7 taken 39 times.
✓ Branch 8 taken 254 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 254 times.
|
392 | return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\v' || c == '\f'; |
| 143 | } | ||
| 144 | |||
| 145 | 7 | static int num_isexp(const char *content, size_t length, size_t pos) { | |
| 146 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
|
7 | if (pos >= length) { |
| 147 | 1 | return 0; | |
| 148 | } | ||
| 149 | |||
| 150 | 6 | int ok = 0; | |
| 151 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 3 times.
|
13 | for (size_t i = pos; i < length; i++) { |
| 152 | 10 | char c = content[i]; | |
| 153 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
|
10 | if (json_isdigit(c)) { |
| 154 | 5 | ok = 1; | |
| 155 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
|
5 | } else if (i == pos) { |
| 156 |
3/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
|
3 | if (!(c == '+' || c == '-')) { |
| 157 | 1 | return 0; | |
| 158 | } | ||
| 159 | } else { | ||
| 160 | 2 | return 0; | |
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | 3 | return ok; | |
| 165 | } | ||
| 166 | |||
| 167 | 41 | static CxJsonTokenType token_numbertype(const char *content, size_t length) { | |
| 168 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
|
41 | if (length == 0) return CX_JSON_TOKEN_ERROR; |
| 169 | |||
| 170 |
4/4✓ Branch 0 taken 39 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 37 times.
|
41 | if (content[0] != '-' && !json_isdigit(content[0])) { |
| 171 | 2 | return CX_JSON_TOKEN_ERROR; | |
| 172 | } | ||
| 173 | |||
| 174 | 39 | CxJsonTokenType type = CX_JSON_TOKEN_INTEGER; | |
| 175 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 29 times.
|
189 | for (size_t i = 1; i < length; i++) { |
| 176 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 146 times.
|
160 | if (content[i] == '.') { |
| 177 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13 times.
|
14 | if (type == CX_JSON_TOKEN_NUMBER) { |
| 178 | 1 | return CX_JSON_TOKEN_ERROR; // more than one decimal separator | |
| 179 | } | ||
| 180 | 13 | type = CX_JSON_TOKEN_NUMBER; | |
| 181 |
4/4✓ Branch 0 taken 140 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 139 times.
|
146 | } else if (content[i] == 'e' || content[i] == 'E') { |
| 182 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 4 times.
|
7 | return num_isexp(content, length, i + 1) ? CX_JSON_TOKEN_NUMBER : CX_JSON_TOKEN_ERROR; |
| 183 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 137 times.
|
139 | } else if (!json_isdigit(content[i])) { |
| 184 | 2 | return CX_JSON_TOKEN_ERROR; // char is not a digit, decimal separator or exponent sep | |
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | 29 | return type; | |
| 189 | } | ||
| 190 | |||
| 191 | 113 | static CxJsonToken token_create(CxJson *json, bool isstring, size_t start, size_t end) { | |
| 192 | 113 | cxmutstr str = cx_mutstrn(json->buffer.space + start, end - start); | |
| 193 | 113 | bool allocated = false; | |
| 194 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 107 times.
|
113 | if (json->uncompleted.tokentype != CX_JSON_NO_TOKEN) { |
| 195 | 6 | allocated = true; | |
| 196 | 6 | str = cx_strcat_m(json->uncompleted.content, 1, str); | |
| 197 | − | if (str.ptr == NULL) { // LCOV_EXCL_START | |
| 198 | − | return (CxJsonToken){CX_JSON_NO_TOKEN, false, {NULL, 0}}; | |
| 199 | } // LCOV_EXCL_STOP | ||
| 200 | } | ||
| 201 | 113 | json->uncompleted = (CxJsonToken){0}; | |
| 202 | CxJsonTokenType ttype; | ||
| 203 |
2/2✓ Branch 0 taken 67 times.
✓ Branch 1 taken 46 times.
|
113 | if (isstring) { |
| 204 | 67 | ttype = CX_JSON_TOKEN_STRING; | |
| 205 | } else { | ||
| 206 | 46 | cxstring s = cx_strcast(str); | |
| 207 |
4/4✓ Branch 1 taken 44 times.
✓ Branch 2 taken 2 times.
✓ Branch 4 taken 43 times.
✓ Branch 5 taken 1 times.
|
46 | if (!cx_strcmp(s, CX_STR("true")) || !cx_strcmp(s, CX_STR("false")) |
| 208 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 41 times.
|
43 | || !cx_strcmp(s, CX_STR("null"))) { |
| 209 | 5 | ttype = CX_JSON_TOKEN_LITERAL; | |
| 210 | } else { | ||
| 211 | 41 | ttype = token_numbertype(str.ptr, str.length); | |
| 212 | } | ||
| 213 | } | ||
| 214 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 104 times.
|
113 | if (ttype == CX_JSON_TOKEN_ERROR) { |
| 215 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
|
9 | if (allocated) { |
| 216 | 1 | cx_strfree(&str); | |
| 217 | } | ||
| 218 | 9 | return (CxJsonToken){CX_JSON_TOKEN_ERROR, false, {NULL, 0}}; | |
| 219 | } | ||
| 220 | 104 | return (CxJsonToken){ttype, allocated, str}; | |
| 221 | } | ||
| 222 | |||
| 223 | 611 | static CxJsonTokenType char2ttype(char c) { | |
| 224 |
8/8✓ Branch 0 taken 9 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 20 times.
✓ Branch 4 taken 37 times.
✓ Branch 5 taken 53 times.
✓ Branch 6 taken 67 times.
✓ Branch 7 taken 392 times.
|
611 | switch (c) { |
| 225 | 9 | case '[': { | |
| 226 | 9 | return CX_JSON_TOKEN_BEGIN_ARRAY; | |
| 227 | } | ||
| 228 | 23 | case '{': { | |
| 229 | 23 | return CX_JSON_TOKEN_BEGIN_OBJECT; | |
| 230 | } | ||
| 231 | 10 | case ']': { | |
| 232 | 10 | return CX_JSON_TOKEN_END_ARRAY; | |
| 233 | } | ||
| 234 | 20 | case '}': { | |
| 235 | 20 | return CX_JSON_TOKEN_END_OBJECT; | |
| 236 | } | ||
| 237 | 37 | case ':': { | |
| 238 | 37 | return CX_JSON_TOKEN_NAME_SEPARATOR; | |
| 239 | } | ||
| 240 | 53 | case ',': { | |
| 241 | 53 | return CX_JSON_TOKEN_VALUE_SEPARATOR; | |
| 242 | } | ||
| 243 | 67 | case '"': { | |
| 244 | 67 | return CX_JSON_TOKEN_STRING; | |
| 245 | } | ||
| 246 | 392 | default: { | |
| 247 |
2/2✓ Branch 1 taken 138 times.
✓ Branch 2 taken 254 times.
|
392 | if (json_isspace(c)) { |
| 248 | 138 | return CX_JSON_TOKEN_SPACE; | |
| 249 | } | ||
| 250 | } | ||
| 251 | } | ||
| 252 | 254 | return CX_JSON_NO_TOKEN; | |
| 253 | } | ||
| 254 | |||
| 255 | 261 | static enum cx_json_status token_parse_next(CxJson *json, CxJsonToken *result) { | |
| 256 | // check if there is data in the buffer | ||
| 257 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 257 times.
|
261 | if (cxBufferEof(&json->buffer)) { |
| 258 | 4 | return json->uncompleted.tokentype == CX_JSON_NO_TOKEN ? | |
| 259 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | CX_JSON_NO_DATA : CX_JSON_INCOMPLETE_DATA; |
| 260 | } | ||
| 261 | |||
| 262 | // current token type and start index | ||
| 263 | 257 | CxJsonTokenType ttype = json->uncompleted.tokentype; | |
| 264 | 257 | size_t token_part_start = json->buffer.pos; | |
| 265 | |||
| 266 | 257 | bool escape_end_of_string = ttype == CX_JSON_TOKEN_STRING | |
| 267 |
4/4✓ Branch 0 taken 8 times.
✓ Branch 1 taken 249 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 7 times.
|
257 | && json->uncompleted.content.ptr[json->uncompleted.content.length-1] == '\\'; |
| 268 | |||
| 269 |
2/2✓ Branch 0 taken 1361 times.
✓ Branch 1 taken 15 times.
|
1376 | for (size_t i = json->buffer.pos; i < json->buffer.size; i++) { |
| 270 | 1361 | char c = json->buffer.space[i]; | |
| 271 |
2/2✓ Branch 0 taken 611 times.
✓ Branch 1 taken 750 times.
|
1361 | if (ttype != CX_JSON_TOKEN_STRING) { |
| 272 | // currently non-string token | ||
| 273 | 611 | CxJsonTokenType ctype = char2ttype(c); // start of new token? | |
| 274 |
2/2✓ Branch 0 taken 357 times.
✓ Branch 1 taken 254 times.
|
611 | if (ttype == CX_JSON_NO_TOKEN) { |
| 275 |
2/2✓ Branch 0 taken 115 times.
✓ Branch 1 taken 242 times.
|
357 | if (ctype == CX_JSON_TOKEN_SPACE) { |
| 276 | 115 | json->buffer.pos++; | |
| 277 | 115 | continue; | |
| 278 |
2/2✓ Branch 0 taken 67 times.
✓ Branch 1 taken 175 times.
|
242 | } else if (ctype == CX_JSON_TOKEN_STRING) { |
| 279 | // begin string | ||
| 280 | 67 | ttype = CX_JSON_TOKEN_STRING; | |
| 281 | 67 | token_part_start = i; | |
| 282 |
2/2✓ Branch 0 taken 129 times.
✓ Branch 1 taken 46 times.
|
175 | } else if (ctype != CX_JSON_NO_TOKEN) { |
| 283 | // single-char token | ||
| 284 | 129 | json->buffer.pos = i + 1; | |
| 285 | 129 | *result = (CxJsonToken){ctype, false, {NULL, 0}}; | |
| 286 | 129 | return CX_JSON_NO_ERROR; | |
| 287 | } else { | ||
| 288 | 46 | ttype = CX_JSON_TOKEN_LITERAL; // number or literal | |
| 289 | 46 | token_part_start = i; | |
| 290 | } | ||
| 291 | } else { | ||
| 292 | // finish token | ||
| 293 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 208 times.
|
254 | if (ctype != CX_JSON_NO_TOKEN) { |
| 294 | 46 | *result = token_create(json, false, token_part_start, i); | |
| 295 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
|
46 | if (result->tokentype == CX_JSON_NO_TOKEN) { |
| 296 | − | return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE | |
| 297 | } | ||
| 298 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 37 times.
|
46 | if (result->tokentype == CX_JSON_TOKEN_ERROR) { |
| 299 | 9 | return CX_JSON_FORMAT_ERROR_NUMBER; | |
| 300 | } | ||
| 301 | 37 | json->buffer.pos = i; | |
| 302 | 37 | return CX_JSON_NO_ERROR; | |
| 303 | } | ||
| 304 | } | ||
| 305 | } else { | ||
| 306 | // currently inside a string | ||
| 307 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 708 times.
|
750 | if (escape_end_of_string) { |
| 308 | 42 | escape_end_of_string = false; | |
| 309 | } else { | ||
| 310 |
2/2✓ Branch 0 taken 67 times.
✓ Branch 1 taken 641 times.
|
708 | if (c == '"') { |
| 311 | 67 | *result = token_create(json, true, token_part_start, i + 1); | |
| 312 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
|
67 | if (result->tokentype == CX_JSON_NO_TOKEN) { |
| 313 | − | return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE | |
| 314 | } | ||
| 315 | 67 | json->buffer.pos = i + 1; | |
| 316 | 67 | return CX_JSON_NO_ERROR; | |
| 317 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 599 times.
|
641 | } else if (c == '\\') { |
| 318 | 42 | escape_end_of_string = true; | |
| 319 | } | ||
| 320 | } | ||
| 321 | } | ||
| 322 | } | ||
| 323 | |||
| 324 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
|
15 | if (ttype != CX_JSON_NO_TOKEN) { |
| 325 | // uncompleted token | ||
| 326 | 12 | size_t uncompleted_len = json->buffer.size - token_part_start; | |
| 327 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (json->uncompleted.tokentype == CX_JSON_NO_TOKEN) { |
| 328 | // current token is uncompleted | ||
| 329 | // save current token content | ||
| 330 | 6 | CxJsonToken uncompleted = { | |
| 331 | ttype, true, | ||
| 332 | 6 | cx_strdup(cx_strn(json->buffer.space + token_part_start, uncompleted_len)) | |
| 333 | }; | ||
| 334 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (uncompleted.content.ptr == NULL) { |
| 335 | − | return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE | |
| 336 | } | ||
| 337 | 6 | json->uncompleted = uncompleted; | |
| 338 | } else { | ||
| 339 | // previously we also had an uncompleted token | ||
| 340 | // combine the uncompleted token with the current token | ||
| 341 | assert(json->uncompleted.allocated); | ||
| 342 | 6 | cxmutstr str = cx_strcat_m(json->uncompleted.content, 1, | |
| 343 | cx_strn(json->buffer.space + token_part_start, uncompleted_len)); | ||
| 344 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (str.ptr == NULL) { |
| 345 | − | return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE | |
| 346 | } | ||
| 347 | 6 | json->uncompleted.content = str; | |
| 348 | } | ||
| 349 | // advance the buffer position - we saved the stuff in the uncompleted token | ||
| 350 | 12 | json->buffer.pos += uncompleted_len; | |
| 351 | } | ||
| 352 | |||
| 353 | 15 | return CX_JSON_INCOMPLETE_DATA; | |
| 354 | } | ||
| 355 | |||
| 356 | // converts a Unicode codepoint to utf8 | ||
| 357 | 17 | static unsigned codepoint_to_utf8(uint32_t codepoint, char *output_buf) { | |
| 358 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 12 times.
|
17 | if (codepoint <= 0x7F) { |
| 359 | 5 | *output_buf = (char)codepoint; | |
| 360 | 5 | return 1; | |
| 361 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
|
12 | } else if (codepoint <= 0x7FF) { |
| 362 | 8 | output_buf[0] = (char)(0xC0 | ((codepoint >> 6) & 0x1F)); | |
| 363 | 8 | output_buf[1] = (char)(0x80 | (codepoint & 0x3F)); | |
| 364 | 8 | return 2; | |
| 365 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
|
4 | } else if (codepoint <= 0xFFFF) { |
| 366 | 1 | output_buf[0] = (char)(0xE0 | ((codepoint >> 12) & 0x0F)); | |
| 367 | 1 | output_buf[1] = (char)(0x80 | ((codepoint >> 6) & 0x3F)); | |
| 368 | 1 | output_buf[2] = (char)(0x80 | (codepoint & 0x3F)); | |
| 369 | 1 | return 3; | |
| 370 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | } else if (codepoint <= 0x10FFFF) { |
| 371 | 3 | output_buf[0] = (char)(0xF0 | ((codepoint >> 18) & 0x07)); | |
| 372 | 3 | output_buf[1] = (char)(0x80 | ((codepoint >> 12) & 0x3F)); | |
| 373 | 3 | output_buf[2] = (char)(0x80 | ((codepoint >> 6) & 0x3F)); | |
| 374 | 3 | output_buf[3] = (char)(0x80 | (codepoint & 0x3F)); | |
| 375 | 3 | return 4; | |
| 376 | } | ||
| 377 | |||
| 378 | − | return 0; // LCOV_EXCL_LINE | |
| 379 | } | ||
| 380 | |||
| 381 | // converts a utf16 surrogate pair to utf8 | ||
| 382 | 3 | static inline uint32_t utf16pair_to_codepoint(uint16_t c0, uint16_t c1) { | |
| 383 | 3 | return ((c0 - 0xD800) << 10) + (c1 - 0xDC00) + 0x10000; | |
| 384 | } | ||
| 385 | |||
| 386 | 24 | static unsigned unescape_unicode_string(cxstring str, char *utf8buf) { | |
| 387 | // str is supposed to start with "\uXXXX" or "\uXXXX\uXXXX" | ||
| 388 | // remaining bytes in the string are ignored (str may be larger!) | ||
| 389 | |||
| 390 |
3/6✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 24 times.
|
24 | if (str.length < 6 || str.ptr[0] != '\\' || str.ptr[1] != 'u') { |
| 391 | ✗ | return 0; | |
| 392 | } | ||
| 393 | |||
| 394 | 24 | unsigned utf8len = 0; | |
| 395 | 24 | cxstring ustr1 = { str.ptr + 2, 4}; | |
| 396 | uint16_t utf16a, utf16b; | ||
| 397 |
2/2✓ Branch 2 taken 22 times.
✓ Branch 3 taken 2 times.
|
24 | if (!cx_strtou16_lc(ustr1, &utf16a, 16, "")) { |
| 398 | uint32_t codepoint; | ||
| 399 |
3/4✓ Branch 0 taken 8 times.
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
|
22 | if (utf16a < 0xD800 || utf16a > 0xE000) { |
| 400 | // character is in the Basic Multilingual Plane | ||
| 401 | // and encoded as a single utf16 char | ||
| 402 | 14 | codepoint = utf16a; | |
| 403 | 14 | utf8len = codepoint_to_utf8(codepoint, utf8buf); | |
| 404 |
3/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 1 times.
|
8 | } else if (utf16a >= 0xD800 && utf16a <= 0xDBFF) { |
| 405 | // character is encoded as a surrogate pair | ||
| 406 | // get next 6 bytes | ||
| 407 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | if (str.length >= 12) { |
| 408 |
3/4✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
7 | if (str.ptr[6] == '\\' && str.ptr[7] == 'u') { |
| 409 | 5 | cxstring ustr2 = { str.ptr+8, 4 }; | |
| 410 |
2/2✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1 times.
|
5 | if (!cx_strtou16_lc(ustr2, &utf16b, 16, "") |
| 411 |
3/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
4 | && utf16b >= 0xDC00 && utf16b <= 0xDFFF) { |
| 412 | 3 | codepoint = utf16pair_to_codepoint(utf16a, utf16b); | |
| 413 | 3 | utf8len = codepoint_to_utf8(codepoint, utf8buf); | |
| 414 | } | ||
| 415 | } | ||
| 416 | } | ||
| 417 | } | ||
| 418 | } | ||
| 419 | 24 | return utf8len; | |
| 420 | } | ||
| 421 | |||
| 422 | 66 | static cxmutstr unescape_string(const CxAllocator *a, cxmutstr str) { | |
| 423 | // note: this function expects that str contains the enclosing quotes! | ||
| 424 | |||
| 425 | cxmutstr result; | ||
| 426 | 66 | result.length = 0; | |
| 427 | 66 | result.ptr = cxMalloc(a, str.length - 1); | |
| 428 | − | if (result.ptr == NULL) return result; // LCOV_EXCL_LINE | |
| 429 | |||
| 430 | 66 | bool u = false; | |
| 431 |
2/2✓ Branch 0 taken 593 times.
✓ Branch 1 taken 66 times.
|
659 | for (size_t i = 1; i < str.length - 1; i++) { |
| 432 | 593 | char c = str.ptr[i]; | |
| 433 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 554 times.
|
593 | if (u) { |
| 434 | 39 | u = false; | |
| 435 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 36 times.
|
39 | if (c == 'n') { |
| 436 | 3 | c = '\n'; | |
| 437 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 30 times.
|
36 | } else if (c == '"') { |
| 438 | 6 | c = '"'; | |
| 439 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 29 times.
|
30 | } else if (c == 't') { |
| 440 | 1 | c = '\t'; | |
| 441 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 28 times.
|
29 | } else if (c == 'r') { |
| 442 | 1 | c = '\r'; | |
| 443 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 27 times.
|
28 | } else if (c == '\\') { |
| 444 | 1 | c = '\\'; | |
| 445 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 26 times.
|
27 | } else if (c == '/') { |
| 446 | 1 | c = '/'; // always unescape, we don't need settings here | |
| 447 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 25 times.
|
26 | } else if (c == 'f') { |
| 448 | 1 | c = '\f'; | |
| 449 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 24 times.
|
25 | } else if (c == 'b') { |
| 450 | 1 | c = '\b'; | |
| 451 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | } else if (c == 'u') { |
| 452 | char utf8buf[4]; | ||
| 453 | 24 | unsigned utf8len = unescape_unicode_string( | |
| 454 | 24 | cx_strn(str.ptr + i - 1, str.length + 1 - i), | |
| 455 | utf8buf | ||
| 456 | ); | ||
| 457 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 7 times.
|
24 | if(utf8len > 0) { |
| 458 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 3 times.
|
17 | i += utf8len < 4 ? 4 : 10; |
| 459 | // add all bytes from utf8buf except the last char | ||
| 460 | // to the result (last char will be added below) | ||
| 461 | 17 | utf8len--; | |
| 462 | 17 | c = utf8buf[utf8len]; | |
| 463 |
2/2✓ Branch 0 taken 19 times.
✓ Branch 1 taken 17 times.
|
36 | for (unsigned x = 0; x < utf8len; x++) { |
| 464 | 19 | result.ptr[result.length++] = utf8buf[x]; | |
| 465 | } | ||
| 466 | } else { | ||
| 467 | // decoding failed, ignore the entire sequence | ||
| 468 | 7 | result.ptr[result.length++] = '\\'; | |
| 469 | } | ||
| 470 | } else { | ||
| 471 | // TODO: discuss the behavior for unrecognized escape sequences | ||
| 472 | // most parsers throw an error here - we just ignore it | ||
| 473 | ✗ | result.ptr[result.length++] = '\\'; | |
| 474 | } | ||
| 475 | |||
| 476 | 39 | result.ptr[result.length++] = c; | |
| 477 | } else { | ||
| 478 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 515 times.
|
554 | if (c == '\\') { |
| 479 | 39 | u = true; | |
| 480 | } else { | ||
| 481 | 515 | result.ptr[result.length++] = c; | |
| 482 | } | ||
| 483 | } | ||
| 484 | } | ||
| 485 | 66 | result.ptr[result.length] = 0; | |
| 486 | |||
| 487 | 66 | return result; | |
| 488 | } | ||
| 489 | |||
| 490 | 60 | static cxmutstr escape_string(cxmutstr str, bool escape_slash) { | |
| 491 | // note: this function produces the string without enclosing quotes | ||
| 492 | // the reason is that we don't want to allocate memory just for that | ||
| 493 | 60 | CxBuffer buf = {0}; | |
| 494 | |||
| 495 | 60 | bool all_printable = true; | |
| 496 |
2/2✓ Branch 0 taken 416 times.
✓ Branch 1 taken 60 times.
|
476 | for (size_t i = 0; i < str.length; i++) { |
| 497 | 416 | unsigned char c = str.ptr[i]; | |
| 498 |
4/4✓ Branch 0 taken 402 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 398 times.
✓ Branch 3 taken 4 times.
|
404 | bool escape = c < 0x20 || c == '\\' || c == '"' |
| 499 |
6/6✓ Branch 0 taken 404 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 386 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 11 times.
|
820 | || (escape_slash && c == '/'); |
| 500 | |||
| 501 |
4/4✓ Branch 0 taken 329 times.
✓ Branch 1 taken 87 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 326 times.
|
416 | if (all_printable && escape) { |
| 502 | 3 | size_t capa = str.length + 32; | |
| 503 | 3 | char *space = malloc(capa); | |
| 504 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (space == NULL) return cx_mutstrn(NULL, 0); |
| 505 | 3 | cxBufferInit(&buf, space, capa, NULL, CX_BUFFER_AUTO_EXTEND); | |
| 506 | 3 | cxBufferWrite(str.ptr, 1, i, &buf); | |
| 507 | 3 | all_printable = false; | |
| 508 | } | ||
| 509 |
2/2✓ Branch 0 taken 19 times.
✓ Branch 1 taken 397 times.
|
416 | if (escape) { |
| 510 | 19 | cxBufferPut(&buf, '\\'); | |
| 511 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 15 times.
|
19 | if (c == '\"') { |
| 512 | 4 | cxBufferPut(&buf, '\"'); | |
| 513 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 13 times.
|
15 | } else if (c == '\n') { |
| 514 | 2 | cxBufferPut(&buf, 'n'); | |
| 515 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 11 times.
|
13 | } else if (c == '\t') { |
| 516 | 2 | cxBufferPut(&buf, 't'); | |
| 517 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 9 times.
|
11 | } else if (c == '\r') { |
| 518 | 2 | cxBufferPut(&buf, 'r'); | |
| 519 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
|
9 | } else if (c == '\\') { |
| 520 | 2 | cxBufferPut(&buf, '\\'); | |
| 521 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
|
7 | } else if (c == '/') { |
| 522 | 1 | cxBufferPut(&buf, '/'); | |
| 523 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | } else if (c == '\f') { |
| 524 | 2 | cxBufferPut(&buf, 'f'); | |
| 525 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | } else if (c == '\b') { |
| 526 | 2 | cxBufferPut(&buf, 'b'); | |
| 527 | } else { | ||
| 528 | char code[6]; | ||
| 529 | 2 | snprintf(code, sizeof(code), "u%04x", (unsigned int) c); | |
| 530 | 2 | cxBufferPutString(&buf, code); | |
| 531 | } | ||
| 532 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 326 times.
|
397 | } else if (!all_printable) { |
| 533 | 71 | cxBufferPut(&buf, c); | |
| 534 | } | ||
| 535 | } | ||
| 536 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 57 times.
|
60 | if (!all_printable) { |
| 537 | 3 | str = cx_mutstrn(buf.space, buf.size); | |
| 538 | } | ||
| 539 | 60 | cxBufferDestroy(&buf); | |
| 540 | 60 | return str; | |
| 541 | } | ||
| 542 | |||
| 543 | 97 | static CxJsonValue* json_create_value(CxJson *json, CxJsonValueType type) { | |
| 544 | 97 | CxJsonValue *v = cxCalloc(json->allocator, 1, sizeof(CxJsonValue)); | |
| 545 | − | if (v == NULL) return NULL; // LCOV_EXCL_LINE | |
| 546 | |||
| 547 | // initialize the value | ||
| 548 | 97 | v->type = type; | |
| 549 | 97 | v->allocator = json->allocator; | |
| 550 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 88 times.
|
97 | if (type == CX_JSON_ARRAY) { |
| 551 | 9 | cx_array_initialize_a(json->allocator, v->value.array.array, 16); | |
| 552 | − | if (v->value.array.array == NULL) goto create_json_value_exit_error; // LCOV_EXCL_LINE | |
| 553 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 65 times.
|
88 | } else if (type == CX_JSON_OBJECT) { |
| 554 | 23 | cx_array_initialize_a(json->allocator, v->value.object.values, 16); | |
| 555 | 23 | v->value.object.indices = cxCalloc(json->allocator, 16, sizeof(size_t)); | |
| 556 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | if (v->value.object.values == NULL || |
| 557 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | v->value.object.indices == NULL) |
| 558 | − | goto create_json_value_exit_error; // LCOV_EXCL_LINE | |
| 559 | } | ||
| 560 | |||
| 561 | // add the new value to a possible parent | ||
| 562 |
2/2✓ Branch 0 taken 61 times.
✓ Branch 1 taken 36 times.
|
97 | if (json->vbuf_size > 0) { |
| 563 | 61 | CxJsonValue *parent = json->vbuf[json->vbuf_size - 1]; | |
| 564 | assert(parent != NULL); | ||
| 565 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 35 times.
|
61 | if (parent->type == CX_JSON_ARRAY) { |
| 566 | 26 | CxArrayReallocator value_realloc = cx_array_reallocator(json->allocator, NULL); | |
| 567 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
|
26 | if (cx_array_simple_add_a(&value_realloc, parent->value.array.array, v)) { |
| 568 | − | goto create_json_value_exit_error; // LCOV_EXCL_LINE | |
| 569 | } | ||
| 570 |
1/2✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
|
35 | } else if (parent->type == CX_JSON_OBJECT) { |
| 571 | // the member was already created after parsing the name | ||
| 572 | assert(json->uncompleted_member.name.ptr != NULL); | ||
| 573 | 35 | json->uncompleted_member.value = v; | |
| 574 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
|
35 | if (json_add_objvalue(parent, json->uncompleted_member)) { |
| 575 | − | goto create_json_value_exit_error; // LCOV_EXCL_LINE | |
| 576 | } | ||
| 577 | 35 | json->uncompleted_member.name = (cxmutstr) {NULL, 0}; | |
| 578 | } else { | ||
| 579 | assert(false); // LCOV_EXCL_LINE | ||
| 580 | } | ||
| 581 | } | ||
| 582 | |||
| 583 | // add the new value to the stack, if it is an array or object | ||
| 584 |
4/4✓ Branch 0 taken 88 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 65 times.
|
97 | if (type == CX_JSON_ARRAY || type == CX_JSON_OBJECT) { |
| 585 | 32 | CxArrayReallocator vbuf_realloc = cx_array_reallocator(NULL, json->vbuf_internal); | |
| 586 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
|
32 | if (cx_array_simple_add_a(&vbuf_realloc, json->vbuf, v)) { |
| 587 | − | goto create_json_value_exit_error; // LCOV_EXCL_LINE | |
| 588 | } | ||
| 589 | } | ||
| 590 | |||
| 591 | // if currently no value is parsed, this is now the value of interest | ||
| 592 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 61 times.
|
97 | if (json->parsed == NULL) { |
| 593 | 36 | json->parsed = v; | |
| 594 | } | ||
| 595 | |||
| 596 | 97 | return v; | |
| 597 | // LCOV_EXCL_START | ||
| 598 | − | create_json_value_exit_error: | |
| 599 | − | cxJsonValueFree(v); | |
| 600 | − | return NULL; | |
| 601 | // LCOV_EXCL_STOP | ||
| 602 | } | ||
| 603 | |||
| 604 | #define JP_STATE_VALUE_BEGIN 0 | ||
| 605 | #define JP_STATE_VALUE_END 10 | ||
| 606 | #define JP_STATE_VALUE_BEGIN_OBJ 1 | ||
| 607 | #define JP_STATE_OBJ_SEP_OR_CLOSE 11 | ||
| 608 | #define JP_STATE_VALUE_BEGIN_AR 2 | ||
| 609 | #define JP_STATE_ARRAY_SEP_OR_CLOSE 12 | ||
| 610 | #define JP_STATE_OBJ_NAME_OR_CLOSE 5 | ||
| 611 | #define JP_STATE_OBJ_NAME 6 | ||
| 612 | #define JP_STATE_OBJ_COLON 7 | ||
| 613 | |||
| 614 | 29 | void cxJsonInit(CxJson *json, const CxAllocator *allocator) { | |
| 615 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 11 times.
|
29 | if (allocator == NULL) { |
| 616 | 18 | allocator = cxDefaultAllocator; | |
| 617 | } | ||
| 618 | |||
| 619 | 29 | memset(json, 0, sizeof(CxJson)); | |
| 620 | 29 | json->allocator = allocator; | |
| 621 | |||
| 622 | 29 | json->states = json->states_internal; | |
| 623 | 29 | json->states_capacity = cx_nmemb(json->states_internal); | |
| 624 | 29 | json->states[0] = JP_STATE_VALUE_BEGIN; | |
| 625 | 29 | json->states_size = 1; | |
| 626 | |||
| 627 | 29 | json->vbuf = json->vbuf_internal; | |
| 628 | 29 | json->vbuf_capacity = cx_nmemb(json->vbuf_internal); | |
| 629 | 29 | } | |
| 630 | |||
| 631 | 29 | void cxJsonDestroy(CxJson *json) { | |
| 632 | 29 | cxBufferDestroy(&json->buffer); | |
| 633 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 28 times.
|
29 | if (json->states != json->states_internal) { |
| 634 | 1 | free(json->states); | |
| 635 | } | ||
| 636 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 28 times.
|
29 | if (json->vbuf != json->vbuf_internal) { |
| 637 | 1 | free(json->vbuf); | |
| 638 | } | ||
| 639 | 29 | cxJsonValueFree(json->parsed); | |
| 640 | 29 | json->parsed = NULL; | |
| 641 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 26 times.
|
29 | if (json->uncompleted_member.name.ptr != NULL) { |
| 642 | 3 | cx_strfree_a(json->allocator, &json->uncompleted_member.name); | |
| 643 | 3 | json->uncompleted_member = (CxJsonObjValue){{NULL, 0}, NULL}; | |
| 644 | } | ||
| 645 | 29 | } | |
| 646 | |||
| 647 | 59 | int cxJsonFilln(CxJson *json, const char *buf, size_t size) { | |
| 648 |
2/2✓ Branch 1 taken 49 times.
✓ Branch 2 taken 10 times.
|
59 | if (cxBufferEof(&json->buffer)) { |
| 649 | // reinitialize the buffer | ||
| 650 | 49 | cxBufferDestroy(&json->buffer); | |
| 651 | 49 | cxBufferInit(&json->buffer, (char*) buf, size, | |
| 652 | NULL, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_COPY_ON_WRITE); | ||
| 653 | 49 | json->buffer.size = size; | |
| 654 | 49 | return 0; | |
| 655 | } else { | ||
| 656 | 10 | return size != cxBufferAppend(buf, 1, size, &json->buffer); | |
| 657 | } | ||
| 658 | } | ||
| 659 | |||
| 660 | 239 | static void json_add_state(CxJson *json, int state) { | |
| 661 | // we have guaranteed the necessary space with cx_array_simple_reserve() | ||
| 662 | // therefore, we can safely add the state in the simplest way possible | ||
| 663 | 239 | json->states[json->states_size++] = state; | |
| 664 | 239 | } | |
| 665 | |||
| 666 | #define return_rec(code) \ | ||
| 667 | token_destroy(&token); \ | ||
| 668 | return code | ||
| 669 | |||
| 670 | 261 | static enum cx_json_status json_parse(CxJson *json) { | |
| 671 | // Reserve a pointer for a possibly read value | ||
| 672 | 261 | CxJsonValue *vbuf = NULL; | |
| 673 | |||
| 674 | // grab the next token | ||
| 675 | CxJsonToken token; | ||
| 676 | { | ||
| 677 | 261 | enum cx_json_status ret = token_parse_next(json, &token); | |
| 678 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 233 times.
|
261 | if (ret != CX_JSON_NO_ERROR) { |
| 679 | 28 | return ret; | |
| 680 | } | ||
| 681 | } | ||
| 682 | |||
| 683 | // pop the current state | ||
| 684 | assert(json->states_size > 0); | ||
| 685 | 233 | int state = json->states[--json->states_size]; | |
| 686 | |||
| 687 | // guarantee that at least two more states fit on the stack | ||
| 688 | 233 | CxArrayReallocator state_realloc = cx_array_reallocator(NULL, json->states_internal); | |
| 689 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 233 times.
|
233 | if (cx_array_simple_reserve_a(&state_realloc, json->states, 2)) { |
| 690 | − | return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE | |
| 691 | } | ||
| 692 | |||
| 693 | |||
| 694 | // 0 JP_STATE_VALUE_BEGIN value begin | ||
| 695 | // 10 JP_STATE_VALUE_END expect value end | ||
| 696 | // 1 JP_STATE_VALUE_BEGIN_OBJ value begin (inside object) | ||
| 697 | // 11 JP_STATE_OBJ_SEP_OR_CLOSE object, expect separator, objclose | ||
| 698 | // 2 JP_STATE_VALUE_BEGIN_AR value begin (inside array) | ||
| 699 | // 12 JP_STATE_ARRAY_SEP_OR_CLOSE array, expect separator or arrayclose | ||
| 700 | // 5 JP_STATE_OBJ_NAME_OR_CLOSE object, expect name or objclose | ||
| 701 | // 6 JP_STATE_OBJ_NAME object, expect name | ||
| 702 | // 7 JP_STATE_OBJ_COLON object, expect ':' | ||
| 703 | |||
| 704 |
2/2✓ Branch 0 taken 98 times.
✓ Branch 1 taken 135 times.
|
233 | if (state < 3) { |
| 705 | // push expected end state to the stack | ||
| 706 | 98 | json_add_state(json, 10 + state); | |
| 707 |
6/6✓ Branch 0 taken 9 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 32 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 1 times.
|
98 | switch (token.tokentype) { |
| 708 | 9 | case CX_JSON_TOKEN_BEGIN_ARRAY: { | |
| 709 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
|
9 | if (json_create_value(json, CX_JSON_ARRAY) == NULL) { |
| 710 | − | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE | |
| 711 | } | ||
| 712 | 9 | json_add_state(json, JP_STATE_VALUE_BEGIN_AR); | |
| 713 | 9 | return_rec(CX_JSON_NO_ERROR); | |
| 714 | } | ||
| 715 | 23 | case CX_JSON_TOKEN_BEGIN_OBJECT: { | |
| 716 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
|
23 | if (json_create_value(json, CX_JSON_OBJECT) == NULL) { |
| 717 | − | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE | |
| 718 | } | ||
| 719 | 23 | json_add_state(json, JP_STATE_OBJ_NAME_OR_CLOSE); | |
| 720 | 23 | return_rec(CX_JSON_NO_ERROR); | |
| 721 | } | ||
| 722 | 28 | case CX_JSON_TOKEN_STRING: { | |
| 723 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
|
28 | if ((vbuf = json_create_value(json, CX_JSON_STRING)) == NULL) { |
| 724 | − | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE | |
| 725 | } | ||
| 726 | 28 | cxmutstr str = unescape_string(json->allocator, token.content); | |
| 727 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (str.ptr == NULL) { |
| 728 | − | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE | |
| 729 | } | ||
| 730 | 28 | vbuf->value.string = str; | |
| 731 | 28 | return_rec(CX_JSON_NO_ERROR); | |
| 732 | } | ||
| 733 | 32 | case CX_JSON_TOKEN_INTEGER: | |
| 734 | case CX_JSON_TOKEN_NUMBER: { | ||
| 735 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 7 times.
|
32 | int type = token.tokentype == CX_JSON_TOKEN_INTEGER ? CX_JSON_INTEGER : CX_JSON_NUMBER; |
| 736 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
|
32 | if (NULL == (vbuf = json_create_value(json, type))) { |
| 737 | − | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE | |
| 738 | } | ||
| 739 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 7 times.
|
32 | if (type == CX_JSON_INTEGER) { |
| 740 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 3 taken 24 times.
|
25 | if (cx_strtoi64(token.content, &vbuf->value.integer, 10)) { |
| 741 | 1 | return_rec(CX_JSON_FORMAT_ERROR_NUMBER); | |
| 742 | } | ||
| 743 | } else { | ||
| 744 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
|
7 | if (cx_strtod(token.content, &vbuf->value.number)) { |
| 745 | ✗ | return_rec(CX_JSON_FORMAT_ERROR_NUMBER); | |
| 746 | } | ||
| 747 | } | ||
| 748 | 31 | return_rec(CX_JSON_NO_ERROR); | |
| 749 | } | ||
| 750 | 5 | case CX_JSON_TOKEN_LITERAL: { | |
| 751 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
|
5 | if ((vbuf = json_create_value(json, CX_JSON_LITERAL)) == NULL) { |
| 752 | − | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE | |
| 753 | } | ||
| 754 |
2/2✓ Branch 3 taken 2 times.
✓ Branch 4 taken 3 times.
|
5 | if (0 == cx_strcmp(cx_strcast(token.content), cx_str("true"))) { |
| 755 | 2 | vbuf->value.literal = CX_JSON_TRUE; | |
| 756 |
2/2✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2 times.
|
3 | } else if (0 == cx_strcmp(cx_strcast(token.content), cx_str("false"))) { |
| 757 | 1 | vbuf->value.literal = CX_JSON_FALSE; | |
| 758 | } else { | ||
| 759 | 2 | vbuf->value.literal = CX_JSON_NULL; | |
| 760 | } | ||
| 761 | 5 | return_rec(CX_JSON_NO_ERROR); | |
| 762 | } | ||
| 763 | 1 | default: { | |
| 764 | 1 | return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); | |
| 765 | } | ||
| 766 | } | ||
| 767 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 109 times.
|
135 | } else if (state == JP_STATE_ARRAY_SEP_OR_CLOSE) { |
| 768 | // expect ',' or ']' | ||
| 769 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 8 times.
|
26 | if (token.tokentype == CX_JSON_TOKEN_VALUE_SEPARATOR) { |
| 770 | 18 | json_add_state(json, JP_STATE_VALUE_BEGIN_AR); | |
| 771 | 18 | return_rec(CX_JSON_NO_ERROR); | |
| 772 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | } else if (token.tokentype == CX_JSON_TOKEN_END_ARRAY) { |
| 773 | // discard the array from the value buffer | ||
| 774 | 8 | json->vbuf_size--; | |
| 775 | 8 | return_rec(CX_JSON_NO_ERROR); | |
| 776 | } else { | ||
| 777 | ✗ | return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); | |
| 778 | } | ||
| 779 |
4/4✓ Branch 0 taken 86 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 70 times.
|
109 | } else if (state == JP_STATE_OBJ_NAME_OR_CLOSE || state == JP_STATE_OBJ_NAME) { |
| 780 |
4/4✓ Branch 0 taken 23 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 22 times.
|
39 | if (state == JP_STATE_OBJ_NAME_OR_CLOSE && token.tokentype == CX_JSON_TOKEN_END_OBJECT) { |
| 781 | // discard the obj from the value buffer | ||
| 782 | 1 | json->vbuf_size--; | |
| 783 | 1 | return_rec(CX_JSON_NO_ERROR); | |
| 784 | } else { | ||
| 785 | // expect string | ||
| 786 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
|
38 | if (token.tokentype != CX_JSON_TOKEN_STRING) { |
| 787 | ✗ | return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); | |
| 788 | } | ||
| 789 | |||
| 790 | // add new entry | ||
| 791 | 38 | cxmutstr name = unescape_string(json->allocator, token.content); | |
| 792 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
|
38 | if (name.ptr == NULL) { |
| 793 | − | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE | |
| 794 | } | ||
| 795 | assert(json->uncompleted_member.name.ptr == NULL); | ||
| 796 | 38 | json->uncompleted_member.name = name; | |
| 797 | assert(json->vbuf_size > 0); | ||
| 798 | |||
| 799 | // next state | ||
| 800 | 38 | json_add_state(json, JP_STATE_OBJ_COLON); | |
| 801 | 38 | return_rec(CX_JSON_NO_ERROR); | |
| 802 | } | ||
| 803 |
2/2✓ Branch 0 taken 38 times.
✓ Branch 1 taken 32 times.
|
70 | } else if (state == JP_STATE_OBJ_COLON) { |
| 804 | // expect ':' | ||
| 805 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 37 times.
|
38 | if (token.tokentype != CX_JSON_TOKEN_NAME_SEPARATOR) { |
| 806 | 1 | return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); | |
| 807 | } | ||
| 808 | // next state | ||
| 809 | 37 | json_add_state(json, JP_STATE_VALUE_BEGIN_OBJ); | |
| 810 | 37 | return_rec(CX_JSON_NO_ERROR); | |
| 811 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | } else if (state == JP_STATE_OBJ_SEP_OR_CLOSE) { |
| 812 | // expect ',' or '}' | ||
| 813 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
|
32 | if (token.tokentype == CX_JSON_TOKEN_VALUE_SEPARATOR) { |
| 814 | 16 | json_add_state(json, JP_STATE_OBJ_NAME); | |
| 815 | 16 | return_rec(CX_JSON_NO_ERROR); | |
| 816 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 3 times.
|
16 | } else if (token.tokentype == CX_JSON_TOKEN_END_OBJECT) { |
| 817 | // discard the obj from the value buffer | ||
| 818 | 13 | json->vbuf_size--; | |
| 819 | 13 | return_rec(CX_JSON_NO_ERROR); | |
| 820 | } else { | ||
| 821 | 3 | return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); | |
| 822 | } | ||
| 823 | } else { | ||
| 824 | // should be unreachable | ||
| 825 | assert(false); | ||
| 826 | ✗ | return_rec(-1); | |
| 827 | } | ||
| 828 | } | ||
| 829 | |||
| 830 | 62 | CxJsonStatus cxJsonNext(CxJson *json, CxJsonValue **value) { | |
| 831 | // check if buffer has been filled | ||
| 832 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
|
62 | if (json->buffer.space == NULL) { |
| 833 | ✗ | return CX_JSON_NULL_DATA; | |
| 834 | } | ||
| 835 | |||
| 836 | // initialize output value | ||
| 837 | 62 | *value = &cx_json_value_nothing; | |
| 838 | |||
| 839 | // parse data | ||
| 840 | CxJsonStatus result; | ||
| 841 | do { | ||
| 842 | 261 | result = json_parse(json); | |
| 843 |
4/4✓ Branch 0 taken 227 times.
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 199 times.
|
261 | if (result == CX_JSON_NO_ERROR && json->states_size == 1) { |
| 844 | // final state reached | ||
| 845 | assert(json->states[0] == JP_STATE_VALUE_END); | ||
| 846 | assert(json->vbuf_size == 0); | ||
| 847 | |||
| 848 | // write output value | ||
| 849 | 28 | *value = json->parsed; | |
| 850 | 28 | json->parsed = NULL; | |
| 851 | |||
| 852 | // re-initialize state machine | ||
| 853 | 28 | json->states[0] = JP_STATE_VALUE_BEGIN; | |
| 854 | |||
| 855 | 28 | return CX_JSON_NO_ERROR; | |
| 856 | } | ||
| 857 |
2/2✓ Branch 0 taken 199 times.
✓ Branch 1 taken 34 times.
|
233 | } while (result == CX_JSON_NO_ERROR); |
| 858 | |||
| 859 | // the parser might think there is no data | ||
| 860 | // but when we did not reach the final state, | ||
| 861 | // we know that there must be more to come | ||
| 862 |
4/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 3 times.
|
34 | if (result == CX_JSON_NO_DATA && json->states_size > 1) { |
| 863 | 1 | return CX_JSON_INCOMPLETE_DATA; | |
| 864 | } | ||
| 865 | |||
| 866 | 33 | return result; | |
| 867 | } | ||
| 868 | |||
| 869 | 266 | void cxJsonValueFree(CxJsonValue *value) { | |
| 870 |
3/4✓ Branch 0 taken 245 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 245 times.
|
266 | if (value == NULL || value->type == CX_JSON_NOTHING) return; |
| 871 |
4/4✓ Branch 0 taken 42 times.
✓ Branch 1 taken 37 times.
✓ Branch 2 taken 41 times.
✓ Branch 3 taken 125 times.
|
245 | switch (value->type) { |
| 872 | 42 | case CX_JSON_OBJECT: { | |
| 873 | 42 | CxJsonObject obj = value->value.object; | |
| 874 |
2/2✓ Branch 0 taken 92 times.
✓ Branch 1 taken 42 times.
|
134 | for (size_t i = 0; i < obj.values_size; i++) { |
| 875 | 92 | cxJsonValueFree(obj.values[i].value); | |
| 876 | 92 | cx_strfree_a(value->allocator, &obj.values[i].name); | |
| 877 | } | ||
| 878 | 42 | cxFree(value->allocator, obj.values); | |
| 879 | 42 | cxFree(value->allocator, obj.indices); | |
| 880 | 42 | break; | |
| 881 | } | ||
| 882 | 37 | case CX_JSON_ARRAY: { | |
| 883 | 37 | CxJsonArray array = value->value.array; | |
| 884 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 37 times.
|
145 | for (size_t i = 0; i < array.array_size; i++) { |
| 885 | 108 | cxJsonValueFree(array.array[i]); | |
| 886 | } | ||
| 887 | 37 | cxFree(value->allocator, array.array); | |
| 888 | 37 | break; | |
| 889 | } | ||
| 890 | 41 | case CX_JSON_STRING: { | |
| 891 | 41 | cxFree(value->allocator, value->value.string.ptr); | |
| 892 | 41 | break; | |
| 893 | } | ||
| 894 | 125 | default: { | |
| 895 | 125 | break; | |
| 896 | } | ||
| 897 | } | ||
| 898 | 245 | cxFree(value->allocator, value); | |
| 899 | } | ||
| 900 | |||
| 901 | 19 | CxJsonValue* cxJsonCreateObj(const CxAllocator* allocator) { | |
| 902 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 18 times.
|
19 | if (allocator == NULL) allocator = cxDefaultAllocator; |
| 903 | 19 | CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue)); | |
| 904 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
|
19 | if (v == NULL) return NULL; |
| 905 | 19 | v->allocator = allocator; | |
| 906 | 19 | v->type = CX_JSON_OBJECT; | |
| 907 | 19 | cx_array_initialize_a(allocator, v->value.object.values, 16); | |
| 908 | − | if (v->value.object.values == NULL) { // LCOV_EXCL_START | |
| 909 | − | cxFree(allocator, v); | |
| 910 | − | return NULL; | |
| 911 | // LCOV_EXCL_STOP | ||
| 912 | } | ||
| 913 | 19 | v->value.object.indices = cxCalloc(allocator, 16, sizeof(size_t)); | |
| 914 | − | if (v->value.object.indices == NULL) { // LCOV_EXCL_START | |
| 915 | − | cxFree(allocator, v->value.object.values); | |
| 916 | − | cxFree(allocator, v); | |
| 917 | − | return NULL; | |
| 918 | // LCOV_EXCL_STOP | ||
| 919 | } | ||
| 920 | 19 | return v; | |
| 921 | } | ||
| 922 | |||
| 923 | 28 | CxJsonValue* cxJsonCreateArr(const CxAllocator* allocator) { | |
| 924 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (allocator == NULL) allocator = cxDefaultAllocator; |
| 925 | 28 | CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue)); | |
| 926 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (v == NULL) return NULL; |
| 927 | 28 | v->allocator = allocator; | |
| 928 | 28 | v->type = CX_JSON_ARRAY; | |
| 929 | 28 | cx_array_initialize_a(allocator, v->value.array.array, 16); | |
| 930 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (v->value.array.array == NULL) { cxFree(allocator, v); return NULL; } |
| 931 | 28 | return v; | |
| 932 | } | ||
| 933 | |||
| 934 | 20 | CxJsonValue* cxJsonCreateNumber(const CxAllocator* allocator, double num) { | |
| 935 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 19 times.
|
20 | if (allocator == NULL) allocator = cxDefaultAllocator; |
| 936 | 20 | CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue)); | |
| 937 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | if (v == NULL) return NULL; |
| 938 | 20 | v->allocator = allocator; | |
| 939 | 20 | v->type = CX_JSON_NUMBER; | |
| 940 | 20 | v->value.number = num; | |
| 941 | 20 | return v; | |
| 942 | } | ||
| 943 | |||
| 944 | 47 | CxJsonValue* cxJsonCreateInteger(const CxAllocator* allocator, int64_t num) { | |
| 945 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
|
47 | if (allocator == NULL) allocator = cxDefaultAllocator; |
| 946 | 47 | CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue)); | |
| 947 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
|
47 | if (v == NULL) return NULL; |
| 948 | 47 | v->allocator = allocator; | |
| 949 | 47 | v->type = CX_JSON_INTEGER; | |
| 950 | 47 | v->value.integer = num; | |
| 951 | 47 | return v; | |
| 952 | } | ||
| 953 | |||
| 954 | 4 | CxJsonValue* cxJsonCreateString(const CxAllocator* allocator, const char* str) { | |
| 955 | 4 | return cxJsonCreateCxString(allocator, cx_str(str)); | |
| 956 | } | ||
| 957 | |||
| 958 | 13 | CxJsonValue* cxJsonCreateCxString(const CxAllocator* allocator, cxstring str) { | |
| 959 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 11 times.
|
13 | if (allocator == NULL) allocator = cxDefaultAllocator; |
| 960 | 13 | CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue)); | |
| 961 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if (v == NULL) return NULL; |
| 962 | 13 | v->allocator = allocator; | |
| 963 | 13 | v->type = CX_JSON_STRING; | |
| 964 | 13 | cxmutstr s = cx_strdup_a(allocator, str); | |
| 965 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if (s.ptr == NULL) { cxFree(allocator, v); return NULL; } |
| 966 | 13 | v->value.string = s; | |
| 967 | 13 | return v; | |
| 968 | } | ||
| 969 | |||
| 970 | 21 | CxJsonValue* cxJsonCreateLiteral(const CxAllocator* allocator, CxJsonLiteral lit) { | |
| 971 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (allocator == NULL) allocator = cxDefaultAllocator; |
| 972 | 21 | CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue)); | |
| 973 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (v == NULL) return NULL; |
| 974 | 21 | v->allocator = allocator; | |
| 975 | 21 | v->type = CX_JSON_LITERAL; | |
| 976 | 21 | v->value.literal = lit; | |
| 977 | 21 | return v; | |
| 978 | } | ||
| 979 | |||
| 980 | // LCOV_EXCL_START | ||
| 981 | // never called as long as malloc() does not return NULL | ||
| 982 | − | static void json_arr_free_temp(CxJsonValue** values, size_t count) { | |
| 983 | − | for (size_t i = 0; i < count; i++) { | |
| 984 | − | if (values[i] == NULL) break; | |
| 985 | − | cxJsonValueFree(values[i]); | |
| 986 | } | ||
| 987 | − | free(values); | |
| 988 | − | } | |
| 989 | // LCOV_EXCL_STOP | ||
| 990 | |||
| 991 | 5 | int cxJsonArrAddNumbers(CxJsonValue* arr, const double* num, size_t count) { | |
| 992 | 5 | CxJsonValue** values = calloc(count, sizeof(CxJsonValue*)); | |
| 993 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (values == NULL) return -1; |
| 994 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5 times.
|
20 | for (size_t i = 0; i < count; i++) { |
| 995 | 15 | values[i] = cxJsonCreateNumber(arr->allocator, num[i]); | |
| 996 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } |
| 997 | } | ||
| 998 | 5 | int ret = cxJsonArrAddValues(arr, values, count); | |
| 999 | 5 | free(values); | |
| 1000 | 5 | return ret; | |
| 1001 | } | ||
| 1002 | |||
| 1003 | 13 | int cxJsonArrAddIntegers(CxJsonValue* arr, const int64_t* num, size_t count) { | |
| 1004 | 13 | CxJsonValue** values = calloc(count, sizeof(CxJsonValue*)); | |
| 1005 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if (values == NULL) return -1; |
| 1006 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 13 times.
|
43 | for (size_t i = 0; i < count; i++) { |
| 1007 | 30 | values[i] = cxJsonCreateInteger(arr->allocator, num[i]); | |
| 1008 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } |
| 1009 | } | ||
| 1010 | 13 | int ret = cxJsonArrAddValues(arr, values, count); | |
| 1011 | 13 | free(values); | |
| 1012 | 13 | return ret; | |
| 1013 | } | ||
| 1014 | |||
| 1015 | 1 | int cxJsonArrAddStrings(CxJsonValue* arr, const char* const* str, size_t count) { | |
| 1016 | 1 | CxJsonValue** values = calloc(count, sizeof(CxJsonValue*)); | |
| 1017 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (values == NULL) return -1; |
| 1018 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (size_t i = 0; i < count; i++) { |
| 1019 | 2 | values[i] = cxJsonCreateString(arr->allocator, str[i]); | |
| 1020 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } |
| 1021 | } | ||
| 1022 | 1 | int ret = cxJsonArrAddValues(arr, values, count); | |
| 1023 | 1 | free(values); | |
| 1024 | 1 | return ret; | |
| 1025 | } | ||
| 1026 | |||
| 1027 | 4 | int cxJsonArrAddCxStrings(CxJsonValue* arr, const cxstring* str, size_t count) { | |
| 1028 | 4 | CxJsonValue** values = calloc(count, sizeof(CxJsonValue*)); | |
| 1029 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (values == NULL) return -1; |
| 1030 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
|
12 | for (size_t i = 0; i < count; i++) { |
| 1031 | 8 | values[i] = cxJsonCreateCxString(arr->allocator, str[i]); | |
| 1032 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } |
| 1033 | } | ||
| 1034 | 4 | int ret = cxJsonArrAddValues(arr, values, count); | |
| 1035 | 4 | free(values); | |
| 1036 | 4 | return ret; | |
| 1037 | } | ||
| 1038 | |||
| 1039 | 5 | int cxJsonArrAddLiterals(CxJsonValue* arr, const CxJsonLiteral* lit, size_t count) { | |
| 1040 | 5 | CxJsonValue** values = calloc(count, sizeof(CxJsonValue*)); | |
| 1041 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (values == NULL) return -1; |
| 1042 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5 times.
|
20 | for (size_t i = 0; i < count; i++) { |
| 1043 | 15 | values[i] = cxJsonCreateLiteral(arr->allocator, lit[i]); | |
| 1044 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } |
| 1045 | } | ||
| 1046 | 5 | int ret = cxJsonArrAddValues(arr, values, count); | |
| 1047 | 5 | free(values); | |
| 1048 | 5 | return ret; | |
| 1049 | } | ||
| 1050 | |||
| 1051 | 36 | int cxJsonArrAddValues(CxJsonValue* arr, CxJsonValue* const* val, size_t count) { | |
| 1052 | 36 | CxArrayReallocator value_realloc = cx_array_reallocator(arr->allocator, NULL); | |
| 1053 | assert(arr->type == CX_JSON_ARRAY); | ||
| 1054 | 36 | return cx_array_simple_copy_a(&value_realloc, | |
| 1055 | arr->value.array.array, | ||
| 1056 | arr->value.array.array_size, | ||
| 1057 | val, count | ||
| 1058 | ); | ||
| 1059 | } | ||
| 1060 | |||
| 1061 | 57 | int cxJsonObjPut(CxJsonValue* obj, cxstring name, CxJsonValue* child) { | |
| 1062 | 57 | cxmutstr k = cx_strdup_a(obj->allocator, name); | |
| 1063 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
|
57 | if (k.ptr == NULL) return -1; |
| 1064 | 57 | CxJsonObjValue kv = {k, child}; | |
| 1065 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
|
57 | if (json_add_objvalue(obj, kv)) { |
| 1066 | ✗ | cx_strfree_a(obj->allocator, &k); | |
| 1067 | ✗ | return 1; | |
| 1068 | } else { | ||
| 1069 | 57 | return 0; | |
| 1070 | } | ||
| 1071 | } | ||
| 1072 | |||
| 1073 | 5 | CxJsonValue* cxJsonObjPutObj(CxJsonValue* obj, cxstring name) { | |
| 1074 | 5 | CxJsonValue* v = cxJsonCreateObj(obj->allocator); | |
| 1075 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (v == NULL) return NULL; |
| 1076 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
|
5 | if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } |
| 1077 | 5 | return v; | |
| 1078 | } | ||
| 1079 | |||
| 1080 | 24 | CxJsonValue* cxJsonObjPutArr(CxJsonValue* obj, cxstring name) { | |
| 1081 | 24 | CxJsonValue* v = cxJsonCreateArr(obj->allocator); | |
| 1082 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (v == NULL) return NULL; |
| 1083 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
|
24 | if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } |
| 1084 | 24 | return v; | |
| 1085 | } | ||
| 1086 | |||
| 1087 | 4 | CxJsonValue* cxJsonObjPutNumber(CxJsonValue* obj, cxstring name, double num) { | |
| 1088 | 4 | CxJsonValue* v = cxJsonCreateNumber(obj->allocator, num); | |
| 1089 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (v == NULL) return NULL; |
| 1090 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } |
| 1091 | 4 | return v; | |
| 1092 | } | ||
| 1093 | |||
| 1094 | 17 | CxJsonValue* cxJsonObjPutInteger(CxJsonValue* obj, cxstring name, int64_t num) { | |
| 1095 | 17 | CxJsonValue* v = cxJsonCreateInteger(obj->allocator, num); | |
| 1096 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
|
17 | if (v == NULL) return NULL; |
| 1097 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
|
17 | if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } |
| 1098 | 17 | return v; | |
| 1099 | } | ||
| 1100 | |||
| 1101 | ✗ | CxJsonValue* cxJsonObjPutString(CxJsonValue* obj, cxstring name, const char* str) { | |
| 1102 | ✗ | CxJsonValue* v = cxJsonCreateString(obj->allocator, str); | |
| 1103 | ✗ | if (v == NULL) return NULL; | |
| 1104 | ✗ | if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } | |
| 1105 | ✗ | return v; | |
| 1106 | } | ||
| 1107 | |||
| 1108 | 1 | CxJsonValue* cxJsonObjPutCxString(CxJsonValue* obj, cxstring name, cxstring str) { | |
| 1109 | 1 | CxJsonValue* v = cxJsonCreateCxString(obj->allocator, str); | |
| 1110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (v == NULL) return NULL; |
| 1111 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } |
| 1112 | 1 | return v; | |
| 1113 | } | ||
| 1114 | |||
| 1115 | 6 | CxJsonValue* cxJsonObjPutLiteral(CxJsonValue* obj, cxstring name, CxJsonLiteral lit) { | |
| 1116 | 6 | CxJsonValue* v = cxJsonCreateLiteral(obj->allocator, lit); | |
| 1117 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (v == NULL) return NULL; |
| 1118 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
|
6 | if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL;} |
| 1119 | 6 | return v; | |
| 1120 | } | ||
| 1121 | |||
| 1122 | 22 | CxJsonValue *cxJsonArrGet(const CxJsonValue *value, size_t index) { | |
| 1123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (index >= value->value.array.array_size) { |
| 1124 | ✗ | return &cx_json_value_nothing; | |
| 1125 | } | ||
| 1126 | 22 | return value->value.array.array[index]; | |
| 1127 | } | ||
| 1128 | |||
| 1129 | 25 | CxIterator cxJsonArrIter(const CxJsonValue *value) { | |
| 1130 | 50 | return cxIteratorPtr( | |
| 1131 | 25 | value->value.array.array, | |
| 1132 | 25 | value->value.array.array_size | |
| 1133 | ); | ||
| 1134 | } | ||
| 1135 | |||
| 1136 | ✗ | CxIterator cxJsonObjIter(const CxJsonValue *value) { | |
| 1137 | ✗ | return cxIterator( | |
| 1138 | ✗ | value->value.object.values, | |
| 1139 | sizeof(CxJsonObjValue), | ||
| 1140 | ✗ | value->value.object.values_size | |
| 1141 | ); | ||
| 1142 | } | ||
| 1143 | |||
| 1144 | 32 | CxJsonValue *cx_json_obj_get_cxstr(const CxJsonValue *value, cxstring name) { | |
| 1145 | 32 | CxJsonObjValue *member = json_find_objvalue(value, name); | |
| 1146 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | if (member == NULL) { |
| 1147 | ✗ | return &cx_json_value_nothing; | |
| 1148 | } else { | ||
| 1149 | 32 | return member->value; | |
| 1150 | } | ||
| 1151 | } | ||
| 1152 | |||
| 1153 | 20 | CxJsonWriter cxJsonWriterCompact(void) { | |
| 1154 | 20 | return (CxJsonWriter) { | |
| 1155 | false, | ||
| 1156 | true, | ||
| 1157 | 6, | ||
| 1158 | false, | ||
| 1159 | 4, | ||
| 1160 | false | ||
| 1161 | }; | ||
| 1162 | } | ||
| 1163 | |||
| 1164 | 3 | CxJsonWriter cxJsonWriterPretty(bool use_spaces) { | |
| 1165 | 3 | return (CxJsonWriter) { | |
| 1166 | true, | ||
| 1167 | true, | ||
| 1168 | 6, | ||
| 1169 | use_spaces, | ||
| 1170 | 4, | ||
| 1171 | false | ||
| 1172 | }; | ||
| 1173 | } | ||
| 1174 | |||
| 1175 | 48 | static int cx_json_writer_indent( | |
| 1176 | void *target, | ||
| 1177 | cx_write_func wfunc, | ||
| 1178 | const CxJsonWriter *settings, | ||
| 1179 | unsigned int depth | ||
| 1180 | ) { | ||
| 1181 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 45 times.
|
48 | if (depth == 0) return 0; |
| 1182 | |||
| 1183 | // determine the width and characters to use | ||
| 1184 | const char* indent; // for 32 prepared chars | ||
| 1185 | 45 | size_t width = depth; | |
| 1186 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 15 times.
|
45 | if (settings->indent_space) { |
| 1187 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (settings->indent == 0) return 0; |
| 1188 | 30 | width *= settings->indent; | |
| 1189 | 30 | indent = " "; | |
| 1190 | } else { | ||
| 1191 | 15 | indent = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; | |
| 1192 | } | ||
| 1193 | |||
| 1194 | // calculate the number of write calls and write | ||
| 1195 | 45 | size_t full = width / 32; | |
| 1196 | 45 | size_t remaining = width % 32; | |
| 1197 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
|
45 | for (size_t i = 0; i < full; i++) { |
| 1198 | ✗ | if (32 != wfunc(indent, 1, 32, target)) return 1; | |
| 1199 | } | ||
| 1200 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
|
45 | if (remaining != wfunc(indent, 1, remaining, target)) return 1; |
| 1201 | |||
| 1202 | 45 | return 0; | |
| 1203 | } | ||
| 1204 | |||
| 1205 | |||
| 1206 | 132 | int cx_json_write_rec( | |
| 1207 | void *target, | ||
| 1208 | const CxJsonValue *value, | ||
| 1209 | cx_write_func wfunc, | ||
| 1210 | const CxJsonWriter *settings, | ||
| 1211 | unsigned int depth | ||
| 1212 | ) { | ||
| 1213 | // keep track of written items | ||
| 1214 | // the idea is to reduce the number of jumps for error checking | ||
| 1215 | 132 | size_t actual = 0, expected = 0; | |
| 1216 | |||
| 1217 | // small buffer for number to string conversions | ||
| 1218 | char numbuf[40]; | ||
| 1219 | |||
| 1220 | // recursively write the values | ||
| 1221 |
6/8✓ Branch 0 taken 17 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 23 times.
✓ Branch 4 taken 40 times.
✓ Branch 5 taken 17 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
132 | switch (value->type) { |
| 1222 | 17 | case CX_JSON_OBJECT: { | |
| 1223 | 17 | const char *begin_obj = "{\n"; | |
| 1224 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 5 times.
|
17 | if (settings->pretty) { |
| 1225 | 12 | actual += wfunc(begin_obj, 1, 2, target); | |
| 1226 | 12 | expected += 2; | |
| 1227 | } else { | ||
| 1228 | 5 | actual += wfunc(begin_obj, 1, 1, target); | |
| 1229 | 5 | expected++; | |
| 1230 | } | ||
| 1231 | 17 | depth++; | |
| 1232 | 17 | size_t elem_count = value->value.object.values_size; | |
| 1233 |
2/2✓ Branch 0 taken 49 times.
✓ Branch 1 taken 17 times.
|
66 | for (size_t look_idx = 0; look_idx < elem_count; look_idx++) { |
| 1234 | // get the member either via index array or directly | ||
| 1235 | 98 | size_t elem_idx = settings->sort_members | |
| 1236 | ? look_idx | ||
| 1237 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 12 times.
|
49 | : value->value.object.indices[look_idx]; |
| 1238 | 49 | CxJsonObjValue *member = &value->value.object.values[elem_idx]; | |
| 1239 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 12 times.
|
49 | if (settings->sort_members) { |
| 1240 | 37 | depth++;depth--; | |
| 1241 | } | ||
| 1242 | |||
| 1243 | // possible indentation | ||
| 1244 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 13 times.
|
49 | if (settings->pretty) { |
| 1245 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
|
36 | if (cx_json_writer_indent(target, wfunc, settings, depth)) { |
| 1246 | − | return 1; // LCOV_EXCL_LINE | |
| 1247 | } | ||
| 1248 | } | ||
| 1249 | |||
| 1250 | // the name | ||
| 1251 | 49 | actual += wfunc("\"", 1, 1, target); | |
| 1252 | 49 | cxmutstr name = escape_string(member->name, settings->escape_slash); | |
| 1253 | 49 | actual += wfunc(name.ptr, 1, name.length, target); | |
| 1254 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 48 times.
|
49 | if (name.ptr != member->name.ptr) { |
| 1255 | 1 | cx_strfree(&name); | |
| 1256 | } | ||
| 1257 | 49 | actual += wfunc("\"", 1, 1, target); | |
| 1258 | 49 | const char *obj_name_sep = ": "; | |
| 1259 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 13 times.
|
49 | if (settings->pretty) { |
| 1260 | 36 | actual += wfunc(obj_name_sep, 1, 2, target); | |
| 1261 | 36 | expected += 4 + member->name.length; | |
| 1262 | } else { | ||
| 1263 | 13 | actual += wfunc(obj_name_sep, 1, 1, target); | |
| 1264 | 13 | expected += 3 + member->name.length; | |
| 1265 | } | ||
| 1266 | |||
| 1267 | // the value | ||
| 1268 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
|
49 | if (cx_json_write_rec(target, member->value, wfunc, settings, depth)) return 1; |
| 1269 | |||
| 1270 | // end of object-value | ||
| 1271 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 17 times.
|
49 | if (look_idx < elem_count - 1) { |
| 1272 | 32 | const char *obj_value_sep = ",\n"; | |
| 1273 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8 times.
|
32 | if (settings->pretty) { |
| 1274 | 24 | actual += wfunc(obj_value_sep, 1, 2, target); | |
| 1275 | 24 | expected += 2; | |
| 1276 | } else { | ||
| 1277 | 8 | actual += wfunc(obj_value_sep, 1, 1, target); | |
| 1278 | 8 | expected++; | |
| 1279 | } | ||
| 1280 | } else { | ||
| 1281 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 5 times.
|
17 | if (settings->pretty) { |
| 1282 | 12 | actual += wfunc("\n", 1, 1, target); | |
| 1283 | 12 | expected ++; | |
| 1284 | } | ||
| 1285 | } | ||
| 1286 | } | ||
| 1287 | 17 | depth--; | |
| 1288 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 5 times.
|
17 | if (settings->pretty) { |
| 1289 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
|
12 | if (cx_json_writer_indent(target, wfunc, settings, depth)) return 1; |
| 1290 | } | ||
| 1291 | 17 | actual += wfunc("}", 1, 1, target); | |
| 1292 | 17 | expected++; | |
| 1293 | 17 | break; | |
| 1294 | } | ||
| 1295 | 24 | case CX_JSON_ARRAY: { | |
| 1296 | 24 | actual += wfunc("[", 1, 1, target); | |
| 1297 | 24 | expected++; | |
| 1298 | 24 | CxIterator iter = cxJsonArrIter(value); | |
| 1299 |
3/4✓ Branch 2 taken 68 times.
✓ Branch 3 taken 24 times.
✓ Branch 5 taken 68 times.
✗ Branch 6 not taken.
|
92 | cx_foreach(CxJsonValue*, element, iter) { |
| 1300 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 68 times.
|
68 | if (cx_json_write_rec( |
| 1301 | target, element, | ||
| 1302 | wfunc, settings, depth) | ||
| 1303 | ✗ | ) return 1; | |
| 1304 | |||
| 1305 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 24 times.
|
68 | if (iter.index < iter.elem_count - 1) { |
| 1306 | 44 | const char *arr_value_sep = ", "; | |
| 1307 |
2/2✓ Branch 0 taken 33 times.
✓ Branch 1 taken 11 times.
|
44 | if (settings->pretty) { |
| 1308 | 33 | actual += wfunc(arr_value_sep, 1, 2, target); | |
| 1309 | 33 | expected += 2; | |
| 1310 | } else { | ||
| 1311 | 11 | actual += wfunc(arr_value_sep, 1, 1, target); | |
| 1312 | 11 | expected++; | |
| 1313 | } | ||
| 1314 | } | ||
| 1315 | } | ||
| 1316 | 24 | actual += wfunc("]", 1, 1, target); | |
| 1317 | 24 | expected++; | |
| 1318 | 24 | break; | |
| 1319 | } | ||
| 1320 | 11 | case CX_JSON_STRING: { | |
| 1321 | 11 | actual += wfunc("\"", 1, 1, target); | |
| 1322 | 11 | cxmutstr str = escape_string(value->value.string, settings->escape_slash); | |
| 1323 | 11 | actual += wfunc(str.ptr, 1, str.length, target); | |
| 1324 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 9 times.
|
11 | if (str.ptr != value->value.string.ptr) { |
| 1325 | 2 | cx_strfree(&str); | |
| 1326 | } | ||
| 1327 | 11 | actual += wfunc("\"", 1, 1, target); | |
| 1328 | 11 | expected += 2 + value->value.string.length; | |
| 1329 | 11 | break; | |
| 1330 | } | ||
| 1331 | 23 | case CX_JSON_NUMBER: { | |
| 1332 | 23 | int precision = settings->frac_max_digits; | |
| 1333 | // because of the way how %g is defined, we need to | ||
| 1334 | // double the precision and truncate ourselves | ||
| 1335 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 1 times.
|
23 | precision = 1 + (precision > 15 ? 30 : 2 * precision); |
| 1336 | 23 | snprintf(numbuf, 40, "%.*g", precision, value->value.number); | |
| 1337 | char *dot, *exp; | ||
| 1338 | unsigned char max_digits; | ||
| 1339 | // find the decimal separator and hope that it's one of . or , | ||
| 1340 | 23 | dot = strchr(numbuf, '.'); | |
| 1341 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 18 times.
|
23 | if (dot == NULL) { |
| 1342 | 5 | dot = strchr(numbuf, ','); | |
| 1343 | } | ||
| 1344 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 18 times.
|
23 | if (dot == NULL) { |
| 1345 | // no decimal separator found | ||
| 1346 | // output everything until a possible exponent | ||
| 1347 | 5 | max_digits = 30; | |
| 1348 | 5 | dot = numbuf; | |
| 1349 | } else { | ||
| 1350 | // found a decimal separator | ||
| 1351 | // output everything until the separator | ||
| 1352 | // and set max digits to what the settings say | ||
| 1353 | 18 | size_t len = dot - numbuf; | |
| 1354 | 18 | actual += wfunc(numbuf, 1, len, target); | |
| 1355 | 18 | expected += len; | |
| 1356 | 18 | max_digits = settings->frac_max_digits; | |
| 1357 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 17 times.
|
18 | if (max_digits > 15) { |
| 1358 | 1 | max_digits = 15; | |
| 1359 | } | ||
| 1360 | // locale independent separator | ||
| 1361 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | if (max_digits > 0) { |
| 1362 | 18 | actual += wfunc(".", 1, 1, target); | |
| 1363 | 18 | expected++; | |
| 1364 | } | ||
| 1365 | 18 | dot++; | |
| 1366 | } | ||
| 1367 | // find the exponent | ||
| 1368 | 23 | exp = strchr(dot, 'e'); | |
| 1369 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 1 times.
|
23 | if (exp == NULL) { |
| 1370 | // no exponent - output the rest | ||
| 1371 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22 | if (max_digits > 0) { |
| 1372 | 22 | size_t len = strlen(dot); | |
| 1373 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 18 times.
|
22 | if (len > max_digits) { |
| 1374 | 4 | len = max_digits; | |
| 1375 | } | ||
| 1376 | 22 | actual += wfunc(dot, 1, len, target); | |
| 1377 | 22 | expected += len; | |
| 1378 | } | ||
| 1379 | } else { | ||
| 1380 | // exponent found - truncate the frac digits | ||
| 1381 | // and then output the rest | ||
| 1382 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (max_digits > 0) { |
| 1383 | 1 | size_t len = exp - dot - 1; | |
| 1384 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (len > max_digits) { |
| 1385 | 1 | len = max_digits; | |
| 1386 | } | ||
| 1387 | 1 | actual += wfunc(dot, 1, len, target); | |
| 1388 | 1 | expected += len; | |
| 1389 | } | ||
| 1390 | 1 | actual += wfunc("e", 1, 1, target); | |
| 1391 | 1 | expected++; | |
| 1392 | 1 | exp++; | |
| 1393 | 1 | size_t len = strlen(exp); | |
| 1394 | 1 | actual += wfunc(exp, 1, len, target); | |
| 1395 | 1 | expected += len; | |
| 1396 | } | ||
| 1397 | 23 | break; | |
| 1398 | } | ||
| 1399 | 40 | case CX_JSON_INTEGER: { | |
| 1400 | 40 | snprintf(numbuf, 32, "%" PRIi64, value->value.integer); | |
| 1401 | 40 | size_t len = strlen(numbuf); | |
| 1402 | 40 | actual += wfunc(numbuf, 1, len, target); | |
| 1403 | 40 | expected += len; | |
| 1404 | 40 | break; | |
| 1405 | } | ||
| 1406 | 17 | case CX_JSON_LITERAL: { | |
| 1407 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 12 times.
|
17 | if (value->value.literal == CX_JSON_TRUE) { |
| 1408 | 5 | actual += wfunc("true", 1, 4, target); | |
| 1409 | 5 | expected += 4; | |
| 1410 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
|
12 | } else if (value->value.literal == CX_JSON_FALSE) { |
| 1411 | 8 | actual += wfunc("false", 1, 5, target); | |
| 1412 | 8 | expected += 5; | |
| 1413 | } else { | ||
| 1414 | 4 | actual += wfunc("null", 1, 4, target); | |
| 1415 | 4 | expected += 4; | |
| 1416 | } | ||
| 1417 | 17 | break; | |
| 1418 | } | ||
| 1419 | ✗ | case CX_JSON_NOTHING: { | |
| 1420 | // deliberately supported as an empty string! | ||
| 1421 | // users might want to just write the result | ||
| 1422 | // of a get operation without testing the value | ||
| 1423 | // and therefore this should not blow up | ||
| 1424 | ✗ | break; | |
| 1425 | } | ||
| 1426 | − | default: assert(false); // LCOV_EXCL_LINE | |
| 1427 | } | ||
| 1428 | |||
| 1429 | 132 | return expected != actual; | |
| 1430 | } | ||
| 1431 | |||
| 1432 | 15 | int cxJsonWrite( | |
| 1433 | void *target, | ||
| 1434 | const CxJsonValue *value, | ||
| 1435 | cx_write_func wfunc, | ||
| 1436 | const CxJsonWriter *settings | ||
| 1437 | ) { | ||
| 1438 | assert(target != NULL); | ||
| 1439 | assert(value != NULL); | ||
| 1440 | assert(wfunc != NULL); | ||
| 1441 | |||
| 1442 | 15 | CxJsonWriter writer_default = cxJsonWriterCompact(); | |
| 1443 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (settings == NULL) { |
| 1444 | ✗ | settings = &writer_default; | |
| 1445 | } | ||
| 1446 | 15 | return cx_json_write_rec(target, value, wfunc, settings, 0); | |
| 1447 | } | ||
| 1448 |