Skip to content

Commit a597dd1

Browse files
author
Suhel Chakraborty
authored
Cleanup (#17)
1 parent e37fee2 commit a597dd1

File tree

8 files changed

+154
-171
lines changed

8 files changed

+154
-171
lines changed

.gitignore

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
.vscode/**/*
2-
!.vscode/settings.json
3-
!.vscode/tasks.json
4-
!.vscode/launch.json
5-
!.vscode/extensions.json
61
bin

.vscode/launch.json

Lines changed: 0 additions & 27 deletions
This file was deleted.

.vscode/settings.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{
2-
"[json]": {
3-
"editor.formatOnSave": false
4-
},
2+
"editor.formatOnSave": true,
53
"C_Cpp.clang_format_style": "LLVM"
64
}

.vscode/tasks.json

Lines changed: 0 additions & 25 deletions
This file was deleted.

README.md

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1616
Copy the following from this repository in your source
1717

18-
* `json.h`
19-
* `json.c`
18+
- `json.h`
19+
- `json.c`
2020

2121
And in your code
2222

@@ -25,7 +25,9 @@ And in your code
2525
```
2626

2727
## MACROs
28+
2829
### The `typed` helper
30+
2931
A 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+
3740
A 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
4751
result(json_element) json_parse(typed(json_string) json_str);
4852
```
4953
5054
### Find an element by key
55+
5156
```C
5257
result(json_element) json_object_find(typed(json_object) * object, typed(json_string) key);
5358
```
5459

5560
### Print JSON with specified indentation
61+
5662
```C
5763
void json_print(typed(json_element) *element, int indent);
5864
```
5965
6066
### Free JSON from memory
67+
6168
```C
6269
void json_free(typed(json_element) *element);
6370
```
6471

6572
### Convert error into user friendly error String
73+
6674
```C
6775
typed(json_string) json_error_to_string(typed(json_error) error);
6876
```
6977
7078
## Types
79+
7180
### JSON String
81+
7282
A null-terminated char-sequence
83+
7384
```C
7485
typed(json_string) // alias for const char *
7586
```
7687

7788
### JSON Number
89+
7890
A 64-bit floating point number
91+
7992
```C
8093
typed(json_number) // alias for double
8194
```
8295
8396
### JSON Object
97+
8498
An array of key-value entries
99+
85100
```C
86101
typed(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+
95113
A hetergeneous array of elements
114+
96115
```C
97116
typed(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+
106128
A boolean value
129+
107130
```C
108131
typed(json_boolean)
109132
```
110133

111134
### Element
135+
112136
A tagged union representing a JSON value with its type
137+
113138
```C
114139
typed(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+
123151
An enum which represents a JSON type
152+
124153
```C
125154
typed(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+
138170
A union for interpreting JSON data
171+
139172
```C
140173
typed(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+
152188
An enum which represents an error
189+
153190
```C
154191
typed(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\"}";
173212
int 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+
203243
Outputs
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)

example.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ int main() {
4949
return -1;
5050
}
5151
typed(json_element) element = result_unwrap(json_element)(&element_result);
52-
typed(json_object) *obj = element.value.as_object;
5352

5453
// json_print(&element, 2);
5554
json_free(&element);

0 commit comments

Comments
 (0)