diff options
| author | 2025-02-17 20:54:36 +0100 | |
|---|---|---|
| committer | 2025-02-17 20:54:36 +0100 | |
| commit | 7eb2556f26baf8ff79fcb7388712d8b714efc4f6 (patch) | |
| tree | 0d11017b6431c514bd6afd16138a06851cd2f09e /xs_json.h | |
| parent | Merge tag '2.72' into curl-smtp (diff) | |
| parent | Merge pull request 'doc/snac8: elaborate regex blocking' (#305) from Menelmac... (diff) | |
| download | snac2-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.h | 40 |
1 files changed, 23 insertions, 17 deletions
| @@ -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 | |||
| 7 | int xs_json_dump(const xs_val *data, int indent, FILE *f); | 11 | int xs_json_dump(const xs_val *data, int indent, FILE *f); |
| 8 | xs_str *xs_json_dumps(const xs_val *data, int indent); | 12 | xs_str *xs_json_dumps(const xs_val *data, int indent); |
| 9 | 13 | ||
| 10 | xs_val *xs_json_load(FILE *f); | 14 | xs_val *xs_json_load_full(FILE *f, int maxdepth); |
| 11 | xs_val *xs_json_loads(const xs_str *json); | 15 | xs_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 | ||
| 13 | xstype xs_json_load_type(FILE *f); | 19 | xstype xs_json_load_type(FILE *f); |
| 14 | int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c); | 20 | int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c); |
| 15 | int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt, int *c); | 21 | int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt, int *c); |
| 16 | xs_list *xs_json_load_array(FILE *f); | 22 | xs_list *xs_json_load_array(FILE *f, int maxdepth); |
| 17 | xs_dict *xs_json_load_object(FILE *f); | 23 | xs_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 | ||
| 374 | xs_list *xs_json_load_array(FILE *f) | 380 | xs_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 | ||
| 462 | xs_dict *xs_json_load_object(FILE *f) | 468 | xs_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 | ||
| 503 | xs_val *xs_json_loads(const xs_str *json) | 509 | xs_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 | ||
| 536 | xs_val *xs_json_load(FILE *f) | 542 | xs_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 | } |