sJson (also known as Secure JSON) is a thread-safe, dynamic-allocation-free, cross-compilable JSON parser based on a PAL (Platform Abstraction Layer) architecture writed in c99
| 1 | # sJson — Safe single-header JSON library in C |
| 2 | sJson (also known as Secure JSON) is a thread-safe, dynamic-allocation-free, cross-compilable JSON parser based on a PAL (Platform Abstraction Layer) architecture writed in c99 |
| 3 | |
| 4 | Version **1.1.0** · GPL-3.0 license |
| 5 | |
| 6 | ### Features |
| 7 | |
| 8 | | Category | Detail | |
| 9 | |---|---| |
| 10 | | **Parsing** | RFC 8259, iterative (no C recursion), depth-limited, node-limited | |
| 11 | | **Numbers** | Integer → `int64_t` exact; Float → `double` (NaN/Inf rejected) | |
| 12 | | **Strings** | Two-pass, exact UTF-8 sizing, full `\uXXXX` + surrogate pairs | |
| 13 | | **Objects** | Insertion-order storage + on-demand introsort index (O(n log n)) | |
| 14 | | **Lookup** | O(log n) binary search after `json_obj_finalize`; O(n) before | |
| 15 | | **Memory** | Arena allocator, zero fragmentation, pluggable hooks | |
| 16 | | **Platform** | C99, freestanding (`JSON_NO_STDLIB`), MSVC/GCC/Clang | |
| 17 | |
| 18 | ### Quick start |
| 19 | |
| 20 | ```c |
| 21 | // In ONE translation unit: |
| 22 | #define JSON_IMPLEMENTATION |
| 23 | #include "json_pal.h" |
| 24 | |
| 25 | JsonArena* arena = json_arena_create(NULL, 64 * 1024); |
| 26 | JsonError err; |
| 27 | JsonValue* root = json_parse_cstr(arena, "{\"x\":1}", &err); |
| 28 | |
| 29 | json_obj_finalize(arena, root); // build sorted index |
| 30 | |
| 31 | JsonValue* v; |
| 32 | json_obj_get(root, "x", &v); // O(log n) lookup |
| 33 | int64_t x; json_get_int(v, &x); // x == 1 |
| 34 | |
| 35 | json_arena_destroy(arena); |
| 36 | ``` |
| 37 | |
| 38 | ### PAL overrides |
| 39 | |
| 40 | ```c |
| 41 | #define JSON_MEMCPY my_memcpy |
| 42 | #define JSON_MEMSET my_memset |
| 43 | #define JSON_MEMCMP my_memcmp |
| 44 | #define JSON_STRLEN my_strlen |
| 45 | #define JSON_ASSERT my_assert |
| 46 | #define JSON_STRTOD my_strtod |
| 47 | #define JSON_STRTOLL my_strtoll |
| 48 | #define JSON_NO_STDLIB // exclude all stdlib headers |
| 49 | ``` |
| 50 | |
| 51 | ### Configuration |
| 52 | |
| 53 | ```c |
| 54 | #define JSON_MAX_DEPTH 64 // max nesting depth |
| 55 | #define JSON_MAX_STRING_LEN 1048576 // max decoded string length |
| 56 | #define JSON_MAX_NODES 1048576 // max value nodes per parse |
| 57 | #define JSON_MAX_ARRAY_LEN 1048576 // max items per array/object |
| 58 | #define JSON_INTROSORT_THRESH 16 // insertion sort threshold |
| 59 | #define JSON_ARENA_ALIGN 8 // allocation alignment |
| 60 | ``` |
| 61 | |
| 62 | ### Path syntax |
| 63 | |
| 64 | ```c |
| 65 | json_path(root, "users[0].name", NULL); |
| 66 | json_path(root, "config[\"host\"]", NULL); |
| 67 | json_path(root, "[2][1]", NULL); |
| 68 | ``` |
| 69 | |
| 70 | ### Build & test |
| 71 | |
| 72 | ```sh |
| 73 | make test # basic build |
| 74 | make asan # AddressSanitizer + UBSan |
| 75 | cmake -B build && cmake --build build && ctest --test-dir build |
| 76 | ``` |
| 77 | |
| 78 | ### Bug fixes in v1.1 |
| 79 | |
| 80 | | # | Description | |
| 81 | |---|---| |
| 82 | | 1 | Parser: spurious extra `PS_OBJ_KEY` frame push caused all non-empty objects to fail | |
| 83 | | 2 | String decoder: `\uXXXX` BMP size estimated as constant 3; now uses exact byte count | |
| 84 | | 3 | Missing `JSON_MAX_ARRAY_LEN` macro definition (compile error) | |
| 85 | | 4 | Missing `#include <stdio.h>` for `snprintf` in implementation block | |
| 86 | | 5 | Removed unused internal `json__utf8_advance` function | |
| 87 |