summaryrefslogtreecommitdiff
path: root/xs_json.h
diff options
context:
space:
mode:
authorGravatar shtrophic2025-02-17 20:54:36 +0100
committerGravatar shtrophic2025-02-17 20:54:36 +0100
commit7eb2556f26baf8ff79fcb7388712d8b714efc4f6 (patch)
tree0d11017b6431c514bd6afd16138a06851cd2f09e /xs_json.h
parentMerge tag '2.72' into curl-smtp (diff)
parentMerge pull request 'doc/snac8: elaborate regex blocking' (#305) from Menelmac... (diff)
downloadsnac2-7eb2556f26baf8ff79fcb7388712d8b714efc4f6.tar.gz
snac2-7eb2556f26baf8ff79fcb7388712d8b714efc4f6.tar.xz
snac2-7eb2556f26baf8ff79fcb7388712d8b714efc4f6.zip
Merge remote-tracking branch 'upstream/master' into curl-smtp
Diffstat (limited to 'xs_json.h')
-rw-r--r--xs_json.h40
1 files changed, 23 insertions, 17 deletions
diff --git a/xs_json.h b/xs_json.h
index d1b18e4..8b449a9 100644
--- a/xs_json.h
+++ b/xs_json.h
@@ -4,17 +4,23 @@
4 4
5#define _XS_JSON_H 5#define _XS_JSON_H
6 6
7#ifndef MAX_JSON_DEPTH
8#define MAX_JSON_DEPTH 32
9#endif
10
7int xs_json_dump(const xs_val *data, int indent, FILE *f); 11int xs_json_dump(const xs_val *data, int indent, FILE *f);
8xs_str *xs_json_dumps(const xs_val *data, int indent); 12xs_str *xs_json_dumps(const xs_val *data, int indent);
9 13
10xs_val *xs_json_load(FILE *f); 14xs_val *xs_json_load_full(FILE *f, int maxdepth);
11xs_val *xs_json_loads(const xs_str *json); 15xs_val *xs_json_loads_full(const xs_str *json, int maxdepth);
16#define xs_json_load(f) xs_json_load_full(f, MAX_JSON_DEPTH)
17#define xs_json_loads(s) xs_json_loads_full(s, MAX_JSON_DEPTH)
12 18
13xstype xs_json_load_type(FILE *f); 19xstype xs_json_load_type(FILE *f);
14int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c); 20int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c);
15int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt, int *c); 21int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt, int *c);
16xs_list *xs_json_load_array(FILE *f); 22xs_list *xs_json_load_array(FILE *f, int maxdepth);
17xs_dict *xs_json_load_object(FILE *f); 23xs_dict *xs_json_load_object(FILE *f, int maxdepth);
18 24
19 25
20#ifdef XS_IMPLEMENTATION 26#ifdef XS_IMPLEMENTATION
@@ -371,7 +377,7 @@ int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c)
371} 377}
372 378
373 379
374xs_list *xs_json_load_array(FILE *f) 380xs_list *xs_json_load_array(FILE *f, int maxdepth)
375/* loads a full JSON array (after the initial OBRACK) */ 381/* loads a full JSON array (after the initial OBRACK) */
376{ 382{
377 xstype t; 383 xstype t;
@@ -387,12 +393,12 @@ xs_list *xs_json_load_array(FILE *f)
387 393
388 if (r == 1) { 394 if (r == 1) {
389 /* partial load? */ 395 /* partial load? */
390 if (v == NULL) { 396 if (v == NULL && maxdepth != 0) {
391 if (t == XSTYPE_LIST) 397 if (t == XSTYPE_LIST)
392 v = xs_json_load_array(f); 398 v = xs_json_load_array(f, maxdepth - 1);
393 else 399 else
394 if (t == XSTYPE_DICT) 400 if (t == XSTYPE_DICT)
395 v = xs_json_load_object(f); 401 v = xs_json_load_object(f, maxdepth - 1);
396 } 402 }
397 403
398 /* still null? fail */ 404 /* still null? fail */
@@ -459,7 +465,7 @@ int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt,
459} 465}
460 466
461 467
462xs_dict *xs_json_load_object(FILE *f) 468xs_dict *xs_json_load_object(FILE *f, int maxdepth)
463/* loads a full JSON object (after the initial OCURLY) */ 469/* loads a full JSON object (after the initial OCURLY) */
464{ 470{
465 xstype t; 471 xstype t;
@@ -476,12 +482,12 @@ xs_dict *xs_json_load_object(FILE *f)
476 482
477 if (r == 1) { 483 if (r == 1) {
478 /* partial load? */ 484 /* partial load? */
479 if (v == NULL) { 485 if (v == NULL && maxdepth != 0) {
480 if (t == XSTYPE_LIST) 486 if (t == XSTYPE_LIST)
481 v = xs_json_load_array(f); 487 v = xs_json_load_array(f, maxdepth - 1);
482 else 488 else
483 if (t == XSTYPE_DICT) 489 if (t == XSTYPE_DICT)
484 v = xs_json_load_object(f); 490 v = xs_json_load_object(f, maxdepth - 1);
485 } 491 }
486 492
487 /* still null? fail */ 493 /* still null? fail */
@@ -500,14 +506,14 @@ xs_dict *xs_json_load_object(FILE *f)
500} 506}
501 507
502 508
503xs_val *xs_json_loads(const xs_str *json) 509xs_val *xs_json_loads_full(const xs_str *json, int maxdepth)
504/* loads a string in JSON format and converts to a multiple data */ 510/* loads a string in JSON format and converts to a multiple data */
505{ 511{
506 FILE *f; 512 FILE *f;
507 xs_val *v = NULL; 513 xs_val *v = NULL;
508 514
509 if ((f = fmemopen((char *)json, strlen(json), "r")) != NULL) { 515 if ((f = fmemopen((char *)json, strlen(json), "r")) != NULL) {
510 v = xs_json_load(f); 516 v = xs_json_load_full(f, maxdepth);
511 fclose(f); 517 fclose(f);
512 } 518 }
513 519
@@ -533,17 +539,17 @@ xstype xs_json_load_type(FILE *f)
533} 539}
534 540
535 541
536xs_val *xs_json_load(FILE *f) 542xs_val *xs_json_load_full(FILE *f, int maxdepth)
537/* loads a JSON file */ 543/* loads a JSON file */
538{ 544{
539 xs_val *v = NULL; 545 xs_val *v = NULL;
540 xstype t = xs_json_load_type(f); 546 xstype t = xs_json_load_type(f);
541 547
542 if (t == XSTYPE_LIST) 548 if (t == XSTYPE_LIST)
543 v = xs_json_load_array(f); 549 v = xs_json_load_array(f, maxdepth);
544 else 550 else
545 if (t == XSTYPE_DICT) 551 if (t == XSTYPE_DICT)
546 v = xs_json_load_object(f); 552 v = xs_json_load_object(f, maxdepth);
547 553
548 return v; 554 return v;
549} 555}