1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include "cx/json.h"
30 #include "cx/compare.h"
31
32 #include <string.h>
33 #include <ctype.h>
34 #include <assert.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <inttypes.h>
38
39
40
41
42
43
44 static CxJsonValue cx_json_value_nothing = {.type =
CX_JSON_NOTHING};
45
46 static int json_cmp_objvalue(
const void *l,
const void *r) {
47 const CxJsonObjValue *left = l;
48 const CxJsonObjValue *right = r;
49 return cx_strcmp(cx_strcast(left->name), cx_strcast(right->name));
50 }
51
52 static CxJsonObjValue *json_find_objvalue(
const CxJsonValue *obj, cxstring name) {
53 assert(obj->type ==
CX_JSON_OBJECT);
54 CxJsonObjValue kv_dummy;
55 kv_dummy.name = cx_mutstrn((
char*) name.ptr, name.length);
56 size_t index = cx_array_binary_search(
57 obj->value.object.values,
58 obj->value.object.values_size,
59 sizeof(CxJsonObjValue),
60 &kv_dummy,
61 json_cmp_objvalue
62 );
63 if (index == obj->value.object.values_size) {
64 return NULL;
65 }
else {
66 return &obj->value.object.values[index];
67 }
68 }
69
70 static int json_add_objvalue(CxJsonValue *objv, CxJsonObjValue member) {
71 assert(objv->type ==
CX_JSON_OBJECT);
72 const CxAllocator *
const al = objv->allocator;
73 CxJsonObject *obj = &(objv->value.object);
74
75
76 size_t index = cx_array_binary_search_sup(
77 obj->values,
78 obj->values_size,
79 sizeof(CxJsonObjValue),
80 &member, json_cmp_objvalue
81 );
82
83
84 if (index < obj->values_size &&
0 == json_cmp_objvalue(&member, &obj->values[index])) {
85
86 cx_strfree_a(al, &obj->values[index].name);
87 cxJsonValueFree(obj->values[index].value);
88
89 obj->values[index] = member;
90
91
92 return 0;
93 }
94
95
96 CxArrayReallocator arealloc = cx_array_reallocator(al,
NULL);
97 size_t oldcap = obj->values_capacity;
98 if (cx_array_simple_reserve_a(&arealloc, obj->values,
1))
return 1;
99
100
101 size_t newcap = obj->values_capacity;
102 if (newcap > oldcap) {
103 if (cxReallocateArray(al, &obj->indices, newcap,
sizeof(
size_t))) {
104 return 1;
105 }
106 }
107
108
109 if (index < obj->values_size) {
110
111 memmove(
112 &obj->values[index+
1],
113 &obj->values[index],
114 (obj->values_size - index) *
sizeof(CxJsonObjValue)
115 );
116
117 for (
size_t i =
0; i < obj->values_size ; i++) {
118 if (obj->indices[i] >= index) {
119 obj->indices[i]++;
120 }
121 }
122 }
123
124
125 obj->values[index] = member;
126 obj->indices[obj->values_size] = index;
127 obj->values_size++;
128
129 return 0;
130 }
131
132 static void token_destroy(CxJsonToken *token) {
133 if (token->allocated) {
134 cx_strfree(&token->content);
135 }
136 }
137
138 static int num_isexp(
const char *content,
size_t length,
size_t pos) {
139 if (pos >= length) {
140 return 0;
141 }
142
143 int ok =
0;
144 for (
size_t i = pos; i < length; i++) {
145 char c = content[i];
146 if (isdigit(c)) {
147 ok =
1;
148 }
else if (i == pos) {
149 if (!(c ==
'+' || c ==
'-')) {
150 return 0;
151 }
152 }
else {
153 return 0;
154 }
155 }
156
157 return ok;
158 }
159
160 static CxJsonTokenType token_numbertype(
const char *content,
size_t length) {
161 if (length ==
0)
return CX_JSON_TOKEN_ERROR;
162
163 if (content[
0] !=
'-' && !isdigit(content[
0])) {
164 return CX_JSON_TOKEN_ERROR;
165 }
166
167 CxJsonTokenType type =
CX_JSON_TOKEN_INTEGER;
168 for (
size_t i =
1; i < length; i++) {
169 if (content[i] ==
'.') {
170 if (type ==
CX_JSON_TOKEN_NUMBER) {
171 return CX_JSON_TOKEN_ERROR;
172 }
173 type =
CX_JSON_TOKEN_NUMBER;
174 }
else if (content[i] ==
'e' || content[i] ==
'E') {
175 return num_isexp(content, length, i +
1) ?
CX_JSON_TOKEN_NUMBER :
CX_JSON_TOKEN_ERROR;
176 }
else if (!isdigit(content[i])) {
177 return CX_JSON_TOKEN_ERROR;
178 }
179 }
180
181 return type;
182 }
183
184 static CxJsonToken token_create(CxJson *json, bool isstring,
size_t start,
size_t end) {
185 cxmutstr str = cx_mutstrn(json->buffer.space + start, end - start);
186 bool allocated = false;
187 if (json->uncompleted.tokentype !=
CX_JSON_NO_TOKEN) {
188 allocated = true;
189 str = cx_strcat_m(json->uncompleted.content,
1, str);
190 if (str.ptr ==
NULL) {
191 return (CxJsonToken){
CX_JSON_NO_TOKEN, false, {
NULL,
0}};
192 }
193 }
194 json->uncompleted = (CxJsonToken){
0};
195 CxJsonTokenType ttype;
196 if (isstring) {
197 ttype =
CX_JSON_TOKEN_STRING;
198 }
else {
199 cxstring s = cx_strcast(str);
200 if (!cx_strcmp(s,
CX_STR(
"true")) || !cx_strcmp(s,
CX_STR(
"false"))
201 || !cx_strcmp(s,
CX_STR(
"null"))) {
202 ttype =
CX_JSON_TOKEN_LITERAL;
203 }
else {
204 ttype = token_numbertype(str.ptr, str.length);
205 }
206 }
207 if (ttype ==
CX_JSON_TOKEN_ERROR) {
208 if (allocated) {
209 cx_strfree(&str);
210 }
211 return (CxJsonToken){
CX_JSON_TOKEN_ERROR, false, {
NULL,
0}};
212 }
213 return (CxJsonToken){ttype, allocated, str};
214 }
215
216 static CxJsonTokenType char2ttype(
char c) {
217 switch (c) {
218 case '[': {
219 return CX_JSON_TOKEN_BEGIN_ARRAY;
220 }
221 case '{': {
222 return CX_JSON_TOKEN_BEGIN_OBJECT;
223 }
224 case ']': {
225 return CX_JSON_TOKEN_END_ARRAY;
226 }
227 case '}': {
228 return CX_JSON_TOKEN_END_OBJECT;
229 }
230 case ':': {
231 return CX_JSON_TOKEN_NAME_SEPARATOR;
232 }
233 case ',': {
234 return CX_JSON_TOKEN_VALUE_SEPARATOR;
235 }
236 case '""': {
237 return CX_JSON_TOKEN_STRING;
238 }
239 default: {
240 if (isspace(c)) {
241 return CX_JSON_TOKEN_SPACE;
242 }
243 }
244 }
245 return CX_JSON_NO_TOKEN;
246 }
247
248 static enum cx_json_status token_parse_next(CxJson *json, CxJsonToken *result) {
249
250 if (cxBufferEof(&json->buffer)) {
251 return json->uncompleted.tokentype ==
CX_JSON_NO_TOKEN ?
252 CX_JSON_NO_DATA :
CX_JSON_INCOMPLETE_DATA;
253 }
254
255
256 CxJsonTokenType ttype = json->uncompleted.tokentype;
257 size_t token_start = json->buffer.pos;
258
259 for (
size_t i = json->buffer.pos; i < json->buffer.size; i++) {
260 char c = json->buffer.space[i];
261 if (ttype !=
CX_JSON_TOKEN_STRING) {
262
263 CxJsonTokenType ctype = char2ttype(c);
264 if (ttype ==
CX_JSON_NO_TOKEN) {
265 if (ctype ==
CX_JSON_TOKEN_SPACE) {
266 json->buffer.pos++;
267 continue;
268 }
else if (ctype ==
CX_JSON_TOKEN_STRING) {
269
270 ttype =
CX_JSON_TOKEN_STRING;
271 token_start = i;
272 }
else if (ctype !=
CX_JSON_NO_TOKEN) {
273
274 json->buffer.pos = i +
1;
275 *result = (CxJsonToken){ctype, false, {
NULL,
0}};
276 return CX_JSON_NO_ERROR;
277 }
else {
278 ttype =
CX_JSON_TOKEN_LITERAL;
279 token_start = i;
280 }
281 }
else {
282
283 if (ctype !=
CX_JSON_NO_TOKEN) {
284 *result = token_create(json, false, token_start, i);
285 if (result->tokentype ==
CX_JSON_NO_TOKEN) {
286 return CX_JSON_BUFFER_ALLOC_FAILED;
287 }
288 if (result->tokentype ==
CX_JSON_TOKEN_ERROR) {
289 return CX_JSON_FORMAT_ERROR_NUMBER;
290 }
291 json->buffer.pos = i;
292 return CX_JSON_NO_ERROR;
293 }
294 }
295 }
else {
296
297 if (json->tokenizer_escape) {
298 json->tokenizer_escape = false;
299 }
else {
300 if (c ==
'""') {
301 *result = token_create(json, true, token_start, i +
1);
302 if (result->tokentype ==
CX_JSON_NO_TOKEN) {
303 return CX_JSON_BUFFER_ALLOC_FAILED;
304 }
305 json->buffer.pos = i +
1;
306 return CX_JSON_NO_ERROR;
307 }
else if (c ==
'\\') {
308 json->tokenizer_escape = true;
309 }
310 }
311 }
312 }
313
314 if (ttype !=
CX_JSON_NO_TOKEN) {
315
316 size_t uncompleted_len = json->buffer.size - token_start;
317 if (json->uncompleted.tokentype ==
CX_JSON_NO_TOKEN) {
318
319
320 CxJsonToken uncompleted = {
321 ttype, true,
322 cx_strdup(cx_strn(json->buffer.space + token_start, uncompleted_len))
323 };
324 if (uncompleted.content.ptr ==
NULL) {
325 return CX_JSON_BUFFER_ALLOC_FAILED;
326 }
327 json->uncompleted = uncompleted;
328 }
else {
329
330
331 assert(json->uncompleted.allocated);
332 cxmutstr str = cx_strcat_m(json->uncompleted.content,
1,
333 cx_strn(json->buffer.space + token_start, uncompleted_len));
334 if (str.ptr ==
NULL) {
335 return CX_JSON_BUFFER_ALLOC_FAILED;
336 }
337 json->uncompleted.content = str;
338 }
339
340 json->buffer.pos += uncompleted_len;
341 }
342
343 return CX_JSON_INCOMPLETE_DATA;
344 }
345
346 static cxmutstr unescape_string(
const CxAllocator *a, cxmutstr str) {
347
348
349 cxmutstr result;
350 result.length =
0;
351 result.ptr = cxMalloc(a, str.length -
1);
352 if (result.ptr ==
NULL)
return result;
353
354 bool u = false;
355 for (
size_t i =
1; i < str.length -
1; i++) {
356 char c = str.ptr[i];
357 if (u) {
358 u = false;
359 if (c ==
'n') {
360 c =
'\n';
361 }
else if (c ==
't') {
362 c =
'\t';
363 }
364 result.ptr[result.length++] = c;
365 }
else {
366 if (c ==
'\\') {
367 u = true;
368 }
else {
369 result.ptr[result.length++] = c;
370 }
371 }
372 }
373 result.ptr[result.length] =
0;
374
375 return result;
376 }
377
378 static CxJsonValue* create_json_value(CxJson *json, CxJsonValueType type) {
379 CxJsonValue *v = cxCalloc(json->allocator,
1,
sizeof(CxJsonValue));
380 if (v ==
NULL)
return NULL;
381
382
383 v->type = type;
384 v->allocator = json->allocator;
385 if (type ==
CX_JSON_ARRAY) {
386 cx_array_initialize_a(json->allocator, v->value.array.array,
16);
387 if (v->value.array.array ==
NULL)
goto create_json_value_exit_error;
388 }
else if (type ==
CX_JSON_OBJECT) {
389 cx_array_initialize_a(json->allocator, v->value.object.values,
16);
390 v->value.object.indices = cxCalloc(json->allocator,
16,
sizeof(
size_t));
391 if (v->value.object.values ==
NULL ||
392 v->value.object.indices ==
NULL)
393 goto create_json_value_exit_error;
394 }
395
396
397 if (json->vbuf_size >
0) {
398 CxJsonValue *parent = json->vbuf[json->vbuf_size -
1];
399 assert(parent !=
NULL);
400 if (parent->type ==
CX_JSON_ARRAY) {
401 CxArrayReallocator value_realloc = cx_array_reallocator(json->allocator,
NULL);
402 if (cx_array_simple_add_a(&value_realloc, parent->value.array.array, v)) {
403 goto create_json_value_exit_error;
404 }
405 }
else if (parent->type ==
CX_JSON_OBJECT) {
406
407 assert(json->uncompleted_member.name.ptr !=
NULL);
408 json->uncompleted_member.value = v;
409 if (json_add_objvalue(parent, json->uncompleted_member)) {
410 goto create_json_value_exit_error;
411 }
412 json->uncompleted_member.name = (cxmutstr) {
NULL,
0};
413 }
else {
414 assert(false);
415 }
416 }
417
418
419 if (type ==
CX_JSON_ARRAY || type ==
CX_JSON_OBJECT) {
420 CxArrayReallocator vbuf_realloc = cx_array_reallocator(
NULL, json->vbuf_internal);
421 if (cx_array_simple_add_a(&vbuf_realloc, json->vbuf, v)) {
422 goto create_json_value_exit_error;
423 }
424 }
425
426
427 if (json->parsed ==
NULL) {
428 json->parsed = v;
429 }
430
431 return v;
432
433 create_json_value_exit_error:
434 cxJsonValueFree(v);
435 return NULL;
436
437 }
438
439 #define JP_STATE_VALUE_BEGIN 0
440 #define JP_STATE_VALUE_END 10
441 #define JP_STATE_VALUE_BEGIN_OBJ 1
442 #define JP_STATE_OBJ_SEP_OR_CLOSE 11
443 #define JP_STATE_VALUE_BEGIN_AR 2
444 #define JP_STATE_ARRAY_SEP_OR_CLOSE 12
445 #define JP_STATE_OBJ_NAME_OR_CLOSE 5
446 #define JP_STATE_OBJ_NAME 6
447 #define JP_STATE_OBJ_COLON 7
448
449 void cxJsonInit(CxJson *json,
const CxAllocator *allocator) {
450 if (allocator ==
NULL) {
451 allocator = cxDefaultAllocator;
452 }
453
454 memset(json,
0,
sizeof(CxJson));
455 json->allocator = allocator;
456
457 json->states = json->states_internal;
458 json->states_capacity = cx_nmemb(json->states_internal);
459 json->states[
0] =
JP_STATE_VALUE_BEGIN;
460 json->states_size =
1;
461
462 json->vbuf = json->vbuf_internal;
463 json->vbuf_capacity = cx_nmemb(json->vbuf_internal);
464 }
465
466 void cxJsonDestroy(CxJson *json) {
467 cxBufferDestroy(&json->buffer);
468 if (json->states != json->states_internal) {
469 free(json->states);
470 }
471 if (json->vbuf != json->vbuf_internal) {
472 free(json->vbuf);
473 }
474 cxJsonValueFree(json->parsed);
475 json->parsed =
NULL;
476 if (json->uncompleted_member.name.ptr !=
NULL) {
477 cx_strfree_a(json->allocator, &json->uncompleted_member.name);
478 json->uncompleted_member = (CxJsonObjValue){{
NULL,
0},
NULL};
479 }
480 }
481
482 int cxJsonFilln(CxJson *json,
const char *buf,
size_t size) {
483 if (cxBufferEof(&json->buffer)) {
484
485 cxBufferDestroy(&json->buffer);
486 cxBufferInit(&json->buffer, (
char*) buf, size,
487 NULL,
CX_BUFFER_AUTO_EXTEND |
CX_BUFFER_COPY_ON_WRITE);
488 json->buffer.size = size;
489 return 0;
490 }
else {
491 return size != cxBufferAppend(buf,
1, size, &json->buffer);
492 }
493 }
494
495 static void json_add_state(CxJson *json,
int state) {
496
497
498 json->states[json->states_size++] = state;
499 }
500
501 #define return_rec(code) \
502 token_destroy(&token); \
503 return code
504
505 static enum cx_json_status json_parse(CxJson *json) {
506
507 CxJsonValue *vbuf =
NULL;
508
509
510 CxJsonToken token;
511 {
512 enum cx_json_status ret = token_parse_next(json, &token);
513 if (ret !=
CX_JSON_NO_ERROR) {
514 return ret;
515 }
516 }
517
518
519 assert(json->states_size >
0);
520 int state = json->states[--json->states_size];
521
522
523 CxArrayReallocator state_realloc = cx_array_reallocator(
NULL, json->states_internal);
524 if (cx_array_simple_reserve_a(&state_realloc, json->states,
2)) {
525 return CX_JSON_BUFFER_ALLOC_FAILED;
526 }
527
528
529
530
531
532
533
534
535
536
537
538
539 if (state <
3) {
540
541 json_add_state(json,
10 + state);
542 switch (token.tokentype) {
543 case CX_JSON_TOKEN_BEGIN_ARRAY: {
544 if (create_json_value(json,
CX_JSON_ARRAY) ==
NULL) {
545 return_rec(
CX_JSON_VALUE_ALLOC_FAILED);
546 }
547 json_add_state(json,
JP_STATE_VALUE_BEGIN_AR);
548 return_rec(
CX_JSON_NO_ERROR);
549 }
550 case CX_JSON_TOKEN_BEGIN_OBJECT: {
551 if (create_json_value(json,
CX_JSON_OBJECT) ==
NULL) {
552 return_rec(
CX_JSON_VALUE_ALLOC_FAILED);
553 }
554 json_add_state(json,
JP_STATE_OBJ_NAME_OR_CLOSE);
555 return_rec(
CX_JSON_NO_ERROR);
556 }
557 case CX_JSON_TOKEN_STRING: {
558 if ((vbuf = create_json_value(json,
CX_JSON_STRING)) ==
NULL) {
559 return_rec(
CX_JSON_VALUE_ALLOC_FAILED);
560 }
561 cxmutstr str = unescape_string(json->allocator, token.content);
562 if (str.ptr ==
NULL) {
563 return_rec(
CX_JSON_VALUE_ALLOC_FAILED);
564 }
565 vbuf->value.string = str;
566 return_rec(
CX_JSON_NO_ERROR);
567 }
568 case CX_JSON_TOKEN_INTEGER:
569 case CX_JSON_TOKEN_NUMBER: {
570 int type = token.tokentype ==
CX_JSON_TOKEN_INTEGER ?
CX_JSON_INTEGER :
CX_JSON_NUMBER;
571 if (
NULL == (vbuf = create_json_value(json, type))) {
572 return_rec(
CX_JSON_VALUE_ALLOC_FAILED);
573 }
574 if (type ==
CX_JSON_INTEGER) {
575 if (cx_strtoi64(token.content, &vbuf->value.integer,
10)) {
576 return_rec(
CX_JSON_FORMAT_ERROR_NUMBER);
577 }
578 }
else {
579 if (cx_strtod(token.content, &vbuf->value.number)) {
580 return_rec(
CX_JSON_FORMAT_ERROR_NUMBER);
581 }
582 }
583 return_rec(
CX_JSON_NO_ERROR);
584 }
585 case CX_JSON_TOKEN_LITERAL: {
586 if ((vbuf = create_json_value(json,
CX_JSON_LITERAL)) ==
NULL) {
587 return_rec(
CX_JSON_VALUE_ALLOC_FAILED);
588 }
589 if (
0 == cx_strcmp(cx_strcast(token.content), cx_str(
"true"))) {
590 vbuf->value.literal =
CX_JSON_TRUE;
591 }
else if (
0 == cx_strcmp(cx_strcast(token.content), cx_str(
"false"))) {
592 vbuf->value.literal =
CX_JSON_FALSE;
593 }
else {
594 vbuf->value.literal =
CX_JSON_NULL;
595 }
596 return_rec(
CX_JSON_NO_ERROR);
597 }
598 default: {
599 return_rec(
CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
600 }
601 }
602 }
else if (state ==
JP_STATE_ARRAY_SEP_OR_CLOSE) {
603
604 if (token.tokentype ==
CX_JSON_TOKEN_VALUE_SEPARATOR) {
605 json_add_state(json,
JP_STATE_VALUE_BEGIN_AR);
606 return_rec(
CX_JSON_NO_ERROR);
607 }
else if (token.tokentype ==
CX_JSON_TOKEN_END_ARRAY) {
608
609 json->vbuf_size--;
610 return_rec(
CX_JSON_NO_ERROR);
611 }
else {
612 return_rec(
CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
613 }
614 }
else if (state ==
JP_STATE_OBJ_NAME_OR_CLOSE || state ==
JP_STATE_OBJ_NAME) {
615 if (state ==
JP_STATE_OBJ_NAME_OR_CLOSE && token.tokentype ==
CX_JSON_TOKEN_END_OBJECT) {
616
617 json->vbuf_size--;
618 return_rec(
CX_JSON_NO_ERROR);
619 }
else {
620
621 if (token.tokentype !=
CX_JSON_TOKEN_STRING) {
622 return_rec(
CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
623 }
624
625
626 cxmutstr name = unescape_string(json->allocator, token.content);
627 if (name.ptr ==
NULL) {
628 return_rec(
CX_JSON_VALUE_ALLOC_FAILED);
629 }
630 assert(json->uncompleted_member.name.ptr ==
NULL);
631 json->uncompleted_member.name = name;
632 assert(json->vbuf_size >
0);
633
634
635 json_add_state(json,
JP_STATE_OBJ_COLON);
636 return_rec(
CX_JSON_NO_ERROR);
637 }
638 }
else if (state ==
JP_STATE_OBJ_COLON) {
639
640 if (token.tokentype !=
CX_JSON_TOKEN_NAME_SEPARATOR) {
641 return_rec(
CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
642 }
643
644 json_add_state(json,
JP_STATE_VALUE_BEGIN_OBJ);
645 return_rec(
CX_JSON_NO_ERROR);
646 }
else if (state ==
JP_STATE_OBJ_SEP_OR_CLOSE) {
647
648 if (token.tokentype ==
CX_JSON_TOKEN_VALUE_SEPARATOR) {
649 json_add_state(json,
JP_STATE_OBJ_NAME);
650 return_rec(
CX_JSON_NO_ERROR);
651 }
else if (token.tokentype ==
CX_JSON_TOKEN_END_OBJECT) {
652
653 json->vbuf_size--;
654 return_rec(
CX_JSON_NO_ERROR);
655 }
else {
656 return_rec(
CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
657 }
658 }
else {
659
660 assert(false);
661 return_rec(-
1);
662 }
663 }
664
665 CxJsonStatus cxJsonNext(CxJson *json, CxJsonValue **value) {
666
667 if (json->buffer.space ==
NULL) {
668 return CX_JSON_NULL_DATA;
669 }
670
671
672 *value = &cx_json_value_nothing;
673
674
675 CxJsonStatus result;
676 do {
677 result = json_parse(json);
678 if (result ==
CX_JSON_NO_ERROR && json->states_size ==
1) {
679
680 assert(json->states[
0] ==
JP_STATE_VALUE_END);
681 assert(json->vbuf_size ==
0);
682
683
684 *value = json->parsed;
685 json->parsed =
NULL;
686
687
688 json->states[
0] =
JP_STATE_VALUE_BEGIN;
689
690 return CX_JSON_NO_ERROR;
691 }
692 }
while (result ==
CX_JSON_NO_ERROR);
693
694
695
696
697 if (result ==
CX_JSON_NO_DATA && json->states_size >
1) {
698 return CX_JSON_INCOMPLETE_DATA;
699 }
700
701 return result;
702 }
703
704 void cxJsonValueFree(CxJsonValue *value) {
705 if (value ==
NULL || value->type ==
CX_JSON_NOTHING)
return;
706 switch (value->type) {
707 case CX_JSON_OBJECT: {
708 CxJsonObject obj = value->value.object;
709 for (
size_t i =
0; i < obj.values_size; i++) {
710 cxJsonValueFree(obj.values[i].value);
711 cx_strfree_a(value->allocator, &obj.values[i].name);
712 }
713 cxFree(value->allocator, obj.values);
714 cxFree(value->allocator, obj.indices);
715 break;
716 }
717 case CX_JSON_ARRAY: {
718 CxJsonArray array = value->value.array;
719 for (
size_t i =
0; i < array.array_size; i++) {
720 cxJsonValueFree(array.array[i]);
721 }
722 cxFree(value->allocator, array.array);
723 break;
724 }
725 case CX_JSON_STRING: {
726 cxFree(value->allocator, value->value.string.ptr);
727 break;
728 }
729 default: {
730 break;
731 }
732 }
733 cxFree(value->allocator, value);
734 }
735
736 CxJsonValue* cxJsonCreateObj(
const CxAllocator* allocator) {
737 CxJsonValue* v = cxMalloc(allocator,
sizeof(CxJsonValue));
738 if (v ==
NULL)
return NULL;
739 v->allocator = allocator;
740 v->type =
CX_JSON_OBJECT;
741 cx_array_initialize_a(allocator, v->value.object.values,
16);
742 if (v->value.object.values ==
NULL) {
743 cxFree(allocator, v);
744 return NULL;
745
746 }
747 v->value.object.indices = cxCalloc(allocator,
16,
sizeof(
size_t));
748 if (v->value.object.indices ==
NULL) {
749 cxFree(allocator, v->value.object.values);
750 cxFree(allocator, v);
751 return NULL;
752
753 }
754 return v;
755 }
756
757 CxJsonValue* cxJsonCreateArr(
const CxAllocator* allocator) {
758 CxJsonValue* v = cxMalloc(allocator,
sizeof(CxJsonValue));
759 if (v ==
NULL)
return NULL;
760 v->allocator = allocator;
761 v->type =
CX_JSON_ARRAY;
762 cx_array_initialize_a(allocator, v->value.array.array,
16);
763 if (v->value.array.array ==
NULL) { cxFree(allocator, v);
return NULL; }
764 return v;
765 }
766
767 CxJsonValue* cxJsonCreateNumber(
const CxAllocator* allocator,
double num) {
768 CxJsonValue* v = cxMalloc(allocator,
sizeof(CxJsonValue));
769 if (v ==
NULL)
return NULL;
770 v->allocator = allocator;
771 v->type =
CX_JSON_NUMBER;
772 v->value.number = num;
773 return v;
774 }
775
776 CxJsonValue* cxJsonCreateInteger(
const CxAllocator* allocator,
int64_t num) {
777 CxJsonValue* v = cxMalloc(allocator,
sizeof(CxJsonValue));
778 if (v ==
NULL)
return NULL;
779 v->allocator = allocator;
780 v->type =
CX_JSON_INTEGER;
781 v->value.integer = num;
782 return v;
783 }
784
785 CxJsonValue* cxJsonCreateString(
const CxAllocator* allocator,
const char* str) {
786 return cxJsonCreateCxString(allocator, cx_str(str));
787 }
788
789 CxJsonValue* cxJsonCreateCxString(
const CxAllocator* allocator, cxstring str) {
790 CxJsonValue* v = cxMalloc(allocator,
sizeof(CxJsonValue));
791 if (v ==
NULL)
return NULL;
792 v->allocator = allocator;
793 v->type =
CX_JSON_STRING;
794 cxmutstr s = cx_strdup_a(allocator, str);
795 if (s.ptr ==
NULL) { cxFree(allocator, v);
return NULL; }
796 v->value.string = s;
797 return v;
798 }
799
800 CxJsonValue* cxJsonCreateLiteral(
const CxAllocator* allocator, CxJsonLiteral lit) {
801 CxJsonValue* v = cxMalloc(allocator,
sizeof(CxJsonValue));
802 if (v ==
NULL)
return NULL;
803 v->allocator = allocator;
804 v->type =
CX_JSON_LITERAL;
805 v->value.literal = lit;
806 return v;
807 }
808
809
810
811 static void cx_json_arr_free_temp(CxJsonValue** values,
size_t count) {
812 for (
size_t i =
0; i < count; i++) {
813 if (values[i] ==
NULL)
break;
814 cxJsonValueFree(values[i]);
815 }
816 free(values);
817 }
818
819
820 int cxJsonArrAddNumbers(CxJsonValue* arr,
const double* num,
size_t count) {
821 CxJsonValue** values = calloc(count,
sizeof(CxJsonValue*));
822 if (values ==
NULL)
return -
1;
823 for (
size_t i =
0; i < count; i++) {
824 values[i] = cxJsonCreateNumber(arr->allocator, num[i]);
825 if (values[i] ==
NULL) { cx_json_arr_free_temp(values, count);
return -
1; }
826 }
827 int ret = cxJsonArrAddValues(arr, values, count);
828 free(values);
829 return ret;
830 }
831
832 int cxJsonArrAddIntegers(CxJsonValue* arr,
const int64_t* num,
size_t count) {
833 CxJsonValue** values = calloc(count,
sizeof(CxJsonValue*));
834 if (values ==
NULL)
return -
1;
835 for (
size_t i =
0; i < count; i++) {
836 values[i] = cxJsonCreateInteger(arr->allocator, num[i]);
837 if (values[i] ==
NULL) { cx_json_arr_free_temp(values, count);
return -
1; }
838 }
839 int ret = cxJsonArrAddValues(arr, values, count);
840 free(values);
841 return ret;
842 }
843
844 int cxJsonArrAddStrings(CxJsonValue* arr,
const char*
const* str,
size_t count) {
845 CxJsonValue** values = calloc(count,
sizeof(CxJsonValue*));
846 if (values ==
NULL)
return -
1;
847 for (
size_t i =
0; i < count; i++) {
848 values[i] = cxJsonCreateString(arr->allocator, str[i]);
849 if (values[i] ==
NULL) { cx_json_arr_free_temp(values, count);
return -
1; }
850 }
851 int ret = cxJsonArrAddValues(arr, values, count);
852 free(values);
853 return ret;
854 }
855
856 int cxJsonArrAddCxStrings(CxJsonValue* arr,
const cxstring* str,
size_t count) {
857 CxJsonValue** values = calloc(count,
sizeof(CxJsonValue*));
858 if (values ==
NULL)
return -
1;
859 for (
size_t i =
0; i < count; i++) {
860 values[i] = cxJsonCreateCxString(arr->allocator, str[i]);
861 if (values[i] ==
NULL) { cx_json_arr_free_temp(values, count);
return -
1; }
862 }
863 int ret = cxJsonArrAddValues(arr, values, count);
864 free(values);
865 return ret;
866 }
867
868 int cxJsonArrAddLiterals(CxJsonValue* arr,
const CxJsonLiteral* lit,
size_t count) {
869 CxJsonValue** values = calloc(count,
sizeof(CxJsonValue*));
870 if (values ==
NULL)
return -
1;
871 for (
size_t i =
0; i < count; i++) {
872 values[i] = cxJsonCreateLiteral(arr->allocator, lit[i]);
873 if (values[i] ==
NULL) { cx_json_arr_free_temp(values, count);
return -
1; }
874 }
875 int ret = cxJsonArrAddValues(arr, values, count);
876 free(values);
877 return ret;
878 }
879
880 int cxJsonArrAddValues(CxJsonValue* arr, CxJsonValue*
const* val,
size_t count) {
881 CxArrayReallocator value_realloc = cx_array_reallocator(arr->allocator,
NULL);
882 assert(arr->type ==
CX_JSON_ARRAY);
883 return cx_array_simple_copy_a(&value_realloc,
884 arr->value.array.array,
885 arr->value.array.array_size,
886 val, count
887 );
888 }
889
890 int cxJsonObjPut(CxJsonValue* obj, cxstring name, CxJsonValue* child) {
891 cxmutstr k = cx_strdup_a(obj->allocator, name);
892 if (k.ptr ==
NULL)
return -
1;
893 CxJsonObjValue kv = {k, child};
894 if (json_add_objvalue(obj, kv)) {
895 cx_strfree_a(obj->allocator, &k);
896 return 1;
897 }
else {
898 return 0;
899 }
900 }
901
902 CxJsonValue* cxJsonObjPutObj(CxJsonValue* obj, cxstring name) {
903 CxJsonValue* v = cxJsonCreateObj(obj->allocator);
904 if (v ==
NULL)
return NULL;
905 if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v);
return NULL; }
906 return v;
907 }
908
909 CxJsonValue* cxJsonObjPutArr(CxJsonValue* obj, cxstring name) {
910 CxJsonValue* v = cxJsonCreateArr(obj->allocator);
911 if (v ==
NULL)
return NULL;
912 if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v);
return NULL; }
913 return v;
914 }
915
916 CxJsonValue* cxJsonObjPutNumber(CxJsonValue* obj, cxstring name,
double num) {
917 CxJsonValue* v = cxJsonCreateNumber(obj->allocator, num);
918 if (v ==
NULL)
return NULL;
919 if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v);
return NULL; }
920 return v;
921 }
922
923 CxJsonValue* cxJsonObjPutInteger(CxJsonValue* obj, cxstring name,
int64_t num) {
924 CxJsonValue* v = cxJsonCreateInteger(obj->allocator, num);
925 if (v ==
NULL)
return NULL;
926 if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v);
return NULL; }
927 return v;
928 }
929
930 CxJsonValue* cxJsonObjPutString(CxJsonValue* obj, cxstring name,
const char* str) {
931 CxJsonValue* v = cxJsonCreateString(obj->allocator, str);
932 if (v ==
NULL)
return NULL;
933 if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v);
return NULL; }
934 return v;
935 }
936
937 CxJsonValue* cxJsonObjPutCxString(CxJsonValue* obj, cxstring name, cxstring str) {
938 CxJsonValue* v = cxJsonCreateCxString(obj->allocator, str);
939 if (v ==
NULL)
return NULL;
940 if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v);
return NULL; }
941 return v;
942 }
943
944 CxJsonValue* cxJsonObjPutLiteral(CxJsonValue* obj, cxstring name, CxJsonLiteral lit) {
945 CxJsonValue* v = cxJsonCreateLiteral(obj->allocator, lit);
946 if (v ==
NULL)
return NULL;
947 if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v);
return NULL;}
948 return v;
949 }
950
951 CxJsonValue *cxJsonArrGet(
const CxJsonValue *value,
size_t index) {
952 if (index >= value->value.array.array_size) {
953 return &cx_json_value_nothing;
954 }
955 return value->value.array.array[index];
956 }
957
958 CxIterator cxJsonArrIter(
const CxJsonValue *value) {
959 return cxIteratorPtr(
960 value->value.array.array,
961 value->value.array.array_size
962 );
963 }
964
965 CxIterator cxJsonObjIter(
const CxJsonValue *value) {
966 return cxIterator(
967 value->value.object.values,
968 sizeof(CxJsonObjValue),
969 value->value.object.values_size
970 );
971 }
972
973 CxJsonValue *cx_json_obj_get_cxstr(
const CxJsonValue *value, cxstring name) {
974 CxJsonObjValue *member = json_find_objvalue(value, name);
975 if (member ==
NULL) {
976 return &cx_json_value_nothing;
977 }
else {
978 return member->value;
979 }
980 }
981
982 static const CxJsonWriter cx_json_writer_default = {
983 false,
984 true,
985 255,
986 false,
987 4
988 };
989
990 CxJsonWriter cxJsonWriterCompact(
void) {
991 return cx_json_writer_default;
992 }
993
994 CxJsonWriter cxJsonWriterPretty(bool use_spaces) {
995 return (CxJsonWriter) {
996 true,
997 true,
998 255,
999 use_spaces,
1000 4
1001 };
1002 }
1003
1004 static int cx_json_writer_indent(
1005 void *target,
1006 cx_write_func wfunc,
1007 const CxJsonWriter *settings,
1008 unsigned int depth
1009 ) {
1010 if (depth ==
0)
return 0;
1011
1012
1013 const char* indent;
1014 size_t width = depth;
1015 if (settings->indent_space) {
1016 if (settings->indent ==
0)
return 0;
1017 width *= settings->indent;
1018 indent =
" ";
1019 }
else {
1020 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";
1021 }
1022
1023
1024 size_t full = width /
32;
1025 size_t remaining = width %
32;
1026 for (
size_t i =
0; i < full; i++) {
1027 if (
32 != wfunc(indent,
1,
32, target))
return 1;
1028 }
1029 if (remaining != wfunc(indent,
1, remaining, target))
return 1;
1030
1031 return 0;
1032 }
1033
1034
1035 int cx_json_write_rec(
1036 void *target,
1037 const CxJsonValue *value,
1038 cx_write_func wfunc,
1039 const CxJsonWriter *settings,
1040 unsigned int depth
1041 ) {
1042
1043
1044 size_t actual =
0, expected =
0;
1045
1046
1047 char numbuf[
32];
1048
1049
1050 switch (value->type) {
1051 case CX_JSON_OBJECT: {
1052 const char *begin_obj =
"{\n";
1053 if (settings->pretty) {
1054 actual += wfunc(begin_obj,
1,
2, target);
1055 expected +=
2;
1056 }
else {
1057 actual += wfunc(begin_obj,
1,
1, target);
1058 expected++;
1059 }
1060 depth++;
1061 size_t elem_count = value->value.object.values_size;
1062 for (
size_t look_idx =
0; look_idx < elem_count; look_idx++) {
1063
1064 size_t elem_idx = settings->sort_members
1065 ? look_idx
1066 : value->value.object.indices[look_idx];
1067 CxJsonObjValue *member = &value->value.object.values[elem_idx];
1068 if (settings->sort_members) {
1069 depth++;depth--;
1070 }
1071
1072
1073 if (settings->pretty) {
1074 if (cx_json_writer_indent(target, wfunc, settings, depth)) {
1075 return 1;
1076 }
1077 }
1078
1079
1080 actual += wfunc(
"\"",
1,
1, target);
1081
1082 actual += wfunc(member->name.ptr,
1,
1083 member->name.length, target);
1084 actual += wfunc(
"\"",
1,
1, target);
1085 const char *obj_name_sep =
": ";
1086 if (settings->pretty) {
1087 actual += wfunc(obj_name_sep,
1,
2, target);
1088 expected +=
4 + member->name.length;
1089 }
else {
1090 actual += wfunc(obj_name_sep,
1,
1, target);
1091 expected +=
3 + member->name.length;
1092 }
1093
1094
1095 if (cx_json_write_rec(target, member->value, wfunc, settings, depth))
return 1;
1096
1097
1098 if (look_idx < elem_count -
1) {
1099 const char *obj_value_sep =
",\n";
1100 if (settings->pretty) {
1101 actual += wfunc(obj_value_sep,
1,
2, target);
1102 expected +=
2;
1103 }
else {
1104 actual += wfunc(obj_value_sep,
1,
1, target);
1105 expected++;
1106 }
1107 }
else {
1108 if (settings->pretty) {
1109 actual += wfunc(
"\n",
1,
1, target);
1110 expected ++;
1111 }
1112 }
1113 }
1114 depth--;
1115 if (settings->pretty) {
1116 if (cx_json_writer_indent(target, wfunc, settings, depth))
return 1;
1117 }
1118 actual += wfunc(
"}",
1,
1, target);
1119 expected++;
1120 break;
1121 }
1122 case CX_JSON_ARRAY: {
1123 actual += wfunc(
"[",
1,
1, target);
1124 expected++;
1125 CxIterator iter = cxJsonArrIter(value);
1126 cx_foreach(CxJsonValue*, element, iter) {
1127 if (cx_json_write_rec(
1128 target, element,
1129 wfunc, settings, depth)
1130 )
return 1;
1131
1132 if (iter.index < iter.elem_count -
1) {
1133 const char *arr_value_sep =
", ";
1134 if (settings->pretty) {
1135 actual += wfunc(arr_value_sep,
1,
2, target);
1136 expected +=
2;
1137 }
else {
1138 actual += wfunc(arr_value_sep,
1,
1, target);
1139 expected++;
1140 }
1141 }
1142 }
1143 actual += wfunc(
"]",
1,
1, target);
1144 expected++;
1145 break;
1146 }
1147 case CX_JSON_STRING: {
1148 actual += wfunc(
"\"",
1,
1, target);
1149
1150 actual += wfunc(value->value.string.ptr,
1,
1151 value->value.string.length, target);
1152 actual += wfunc(
"\"",
1,
1, target);
1153 expected +=
2 + value->value.string.length;
1154 break;
1155 }
1156 case CX_JSON_NUMBER: {
1157
1158
1159 snprintf(numbuf,
32,
"%g", value->value.number);
1160 size_t len = strlen(numbuf);
1161 actual += wfunc(numbuf,
1, len, target);
1162 expected += len;
1163 break;
1164 }
1165 case CX_JSON_INTEGER: {
1166 snprintf(numbuf,
32,
"%" PRIi64, value->value.integer);
1167 size_t len = strlen(numbuf);
1168 actual += wfunc(numbuf,
1, len, target);
1169 expected += len;
1170 break;
1171 }
1172 case CX_JSON_LITERAL: {
1173 if (value->value.literal ==
CX_JSON_TRUE) {
1174 actual += wfunc(
"true",
1,
4, target);
1175 expected +=
4;
1176 }
else if (value->value.literal ==
CX_JSON_FALSE) {
1177 actual += wfunc(
"false",
1,
5, target);
1178 expected +=
5;
1179 }
else {
1180 actual += wfunc(
"null",
1,
4, target);
1181 expected +=
4;
1182 }
1183 break;
1184 }
1185 case CX_JSON_NOTHING: {
1186
1187
1188
1189
1190 break;
1191 }
1192 default: assert(false);
1193 }
1194
1195 return expected != actual;
1196 }
1197
1198 int cxJsonWrite(
1199 void *target,
1200 const CxJsonValue *value,
1201 cx_write_func wfunc,
1202 const CxJsonWriter *settings
1203 ) {
1204 if (settings ==
NULL) {
1205 settings = &cx_json_writer_default;
1206 }
1207 assert(target !=
NULL);
1208 assert(value !=
NULL);
1209 assert(wfunc !=
NULL);
1210
1211 return cx_json_write_rec(target, value, wfunc, settings,
0);
1212 }
1213