@@ -4,19 +4,19 @@ An easy to use, very fast JSON parsing implementation written in pure C
44
55## Features
66
7- * Fully [ RFC-8259] ( https://datatracker.ietf.org/doc/html/rfc8259 ) compliant
8- * Small 2 file library
9- * Support for all data types
10- * Simple and efficient hash table implementation to search element by key
11- * Rust like ` result ` type used throughout fallible calls
12- * Compile with ` -DJSON_SCRAPE_WHITESPACE ` to parse non-minified JSON with whitespace in between
7+ - Fully [ RFC-8259] ( https://datatracker.ietf.org/doc/html/rfc8259 ) compliant
8+ - Small 2 file library
9+ - Support for all data types
10+ - Simple and efficient hash table implementation to search element by key
11+ - Rust like ` result ` type used throughout fallible calls
12+ - Compile with ` -DJSON_SKIP_WHITESPACE ` to parse non-minified JSON with whitespace in between
1313
1414## Setup
1515
1616Copy the following from this repository in your source
1717
18- * ` json.h `
19- * ` json.c `
18+ - ` json.h `
19+ - ` json.c `
2020
2121And in your code
2222
@@ -25,7 +25,9 @@ And in your code
2525```
2626
2727## MACROs
28+
2829### The ` typed ` helper
30+
2931A uniform type system used throughout the API
3032` typed(x) ` is alias for ` x_t `
3133
@@ -34,6 +36,7 @@ typed(json_element) // json_element_t
3436```
3537
3638### Rust like `result`
39+
3740A tagged union comprising two variants, either `ok` with the data or `err` with `typed(json_error)` with simplified API to manage variants
3841
3942```C
@@ -43,90 +46,118 @@ result(json_element) // json_element_result_t
4346## API
4447
4548### Parse JSON:
49+
4650``` C
4751result (json_element) json_parse(typed(json_string) json_str);
4852```
4953
5054### Find an element by key
55+
5156```C
5257result(json_element) json_object_find(typed(json_object) * object, typed(json_string) key);
5358```
5459
5560### Print JSON with specified indentation
61+
5662``` C
5763void json_print (typed(json_element) * element, int indent);
5864```
5965
6066### Free JSON from memory
67+
6168```C
6269void json_free(typed(json_element) *element);
6370```
6471
6572### Convert error into user friendly error String
73+
6674``` C
6775typed (json_string) json_error_to_string(typed(json_error) error);
6876```
6977
7078## Types
79+
7180### JSON String
81+
7282A null-terminated char-sequence
83+
7384```C
7485typed(json_string) // alias for const char *
7586```
7687
7788### JSON Number
89+
7890A 64-bit floating point number
91+
7992``` C
8093typed (json_number) // alias for double
8194```
8295
8396### JSON Object
97+
8498An array of key-value entries
99+
85100```C
86101typed(json_object)
87102```
103+
88104#### Fields
105+
89106| ** Name** | ** Type** | ** Description** |
90- | ----------- | ----------------------- | ----------------------- |
107+ | --------- | --------------------- | --------------------- |
91108| ` count ` | ` typed(size) ` | The number of entries |
92109| ` entries ` | ` typed(json_entry) * ` | The array of entries |
93110
94111### JSON Array
112+
95113A hetergeneous array of elements
114+
96115``` C
97116typed (json_array)
98117```
118+
99119#### Fields
120+
100121| **Name** | **Type** | **Description** |
101- |------------| -------------------------| ------------------------ |
122+ | ---------- | ----------------------- | ---------------------- |
102123| `count` | `typed(size)` | The number of elements |
103124| `elements` | `typed(json_element) *` | The array of elements |
104125
105126### JSON Boolean
127+
106128A boolean value
129+
107130```C
108131typed(json_boolean)
109132```
110133
111134### Element
135+
112136A tagged union representing a JSON value with its type
137+
113138``` C
114139typed (json_element)
115140```
141+
116142#### Fields
143+
117144| **Name** | **Type** | **Description** |
118- |----------| -----------------------------| ----------------------- |
145+ | -------- | --------------------------- | --------------------- |
119146| `type` | `typed(json_element_type)` | The type of the value |
120147| `value` | `typed(json_element_value)` | The actual value |
121148
122149### Element Type
150+
123151An enum which represents a JSON type
152+
124153```C
125154typed(json_element_type)
126155```
156+
127157#### Variants
158+
128159| ** Variant** | ** Description** |
129- | ----------------------------- | ----------------- |
160+ | --------------------------- | --------------- |
130161| ` JSON_ELEMENT_TYPE_STRING ` | JSON String |
131162| ` JSON_ELEMENT_TYPE_NUMBER ` | JSON Number |
132163| ` JSON_ELEMENT_TYPE_OBJECT ` | JSON Object |
@@ -135,27 +166,35 @@ typed(json_element_type)
135166| ` JSON_ELEMENT_TYPE_NULL ` | JSON Null |
136167
137168### Element Value
169+
138170A union for interpreting JSON data
171+
139172``` C
140173typed (json_element_value)
141174```
175+
142176#### Fields
177+
143178| **Name** | **Type** | **Interpret data as** |
144- |--------------| ------------------------| ----------------------- |
179+ | ------------ | ---------------------- | --------------------- |
145180| `as_string` | `typed(json_string)` | JSON String |
146181| `as_number` | `typed(json_number)` | JSON Number |
147182| `as_object` | `typed(json_object) *` | JSON Object |
148183| `as_array` | `typed(json_array) *` | JSON Array |
149184| `as_boolean` | `typed(json_boolean)` | JSON Boolean |
150185
151186### Error
187+
152188An enum which represents an error
189+
153190```C
154191typed(json_error)
155192```
193+
156194#### Variants
195+
157196| ** Variant** | ** Description** |
158- | ---------------------------- | -------------------------------- |
197+ | -------------------------- | ------------------------------ |
159198| ` JSON_ERROR_EMPTY ` | Null or empty value |
160199| ` JSON_ERROR_INVALID_TYPE ` | Type inference failed |
161200| ` JSON_ERROR_INVALID_KEY ` | Key is not a valid string |
@@ -173,7 +212,7 @@ const char * some_json_str = "{\"hello\":\"world\",\"key\":\"value\"}";
173212int main () {
174213 result (json_element) element_result = json_parse(some_json_str);
175214
176- // Guard if
215+ // Guard if
177216 if(result_is_err(json_element)(&element_result)) {
178217 typed(json_error) error = result_unwrap_err(json_element)(&element_result);
179218 fprintf(stderr, "Error parsing JSON: %s\n", json_error_to_string(error));
@@ -184,7 +223,7 @@ int main() {
184223 typed (json_element) element = result_unwrap(json_element)(&element_result);
185224
186225 // Fetch the "hello" key value
187- result (json_element) hello_element_result = json_object_find(& element.value.as_object, "hello");
226+ result (json_element) hello_element_result = json_object_find(element.value.as_object, "hello");
188227 if(result_is_err(json_element)(&hello_element_result)) {
189228 typed(json_error) error = result_unwrap_err(json_element)(&hello_element_result);
190229 fprintf(stderr, "Error getting element \" hello\" : %s\n", json_error_to_string(error));
@@ -200,6 +239,7 @@ int main() {
200239 return 0;
201240}
202241```
242+
203243Outputs
204244
205245```
@@ -212,9 +252,9 @@ Outputs
212252
213253### Example in repository
214254
215- 1 . Clone this repository ` git clone https://github.com/forkachild/C-Simple-JSON-Parser `
216- 2 . Compile the example ` clang example.c json.c -o example.out `
217- 3 . Run the binary ` ./example.out `
255+ 1 . Clone this repository ` git clone https://github.com/forkachild/C-Simple-JSON-Parser `
256+ 2 . Compile the example ` clang example.c json.c -o example.out `
257+ 3 . Run the binary ` ./example.out `
218258
219259## FAQs
220260
@@ -298,6 +338,6 @@ for(i = 0; i < arr->count; i++) {
298338
299339### What if the JSON is poorly formatted with uneven whitespace
300340
301- Compile using `-DJSON_SCRAPE_WHITESPACE `
341+ Compile using `-DJSON_SKIP_WHITESPACE `
302342
303343## If this helped you in any way you can [buy me a beer](https://www.paypal.me/suhelchakraborty)
0 commit comments