Skip to content

Commit a72edff

Browse files
author
Suhel Chakraborty
authored
Implemented Heterogeneous array type (#9)
* Implemented heterogeneous array * Updated README
1 parent 5b16a20 commit a72edff

File tree

10 files changed

+76
-241
lines changed

10 files changed

+76
-241
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,8 @@
3131
"string_view": "c",
3232
"unordered_map": "c",
3333
"utility": "c"
34+
},
35+
"[json]": {
36+
"editor.formatOnSave": false
3437
}
3538
}

.vscode/tasks.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"args": [
88
"-fdiagnostics-color=always",
99
"-g",
10-
"-rdynamic",
1110
"${workspaceFolder}/*.c",
1211
"-o",
1312
"${workspaceFolder}/bin/a.out"

README.md

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,49 @@
22

33
## Features
44

5+
* [RFC-8259](https://datatracker.ietf.org/doc/html/rfc8259) compliant
56
* Simple interface
67
* `json_parse`: Parse a JSON string into a `json_object_t *`
78
* `json_print`: Print a `json_object_t *` using specified indentation
89
* `json_free`: Free a `json_object_t *` from memory
9-
* Support for maximum data types
10+
* Support for all data types
1011
* `String`
1112
* `Number`
1213
* `Object`
1314
* `Array`
1415
* `Boolean`
1516
* `Null` _(omitted from end result)_
16-
* Systematically structured data types
17-
* Basic JSON types:
18-
* String: `json_string_t` (Sugar for `const char *`)
19-
* Number: `json_number_t` (Sugar for `double`)
20-
* Object: `json_object_t` (Array of `json_entry_t`)
21-
* Array: `json_array_t` (Typed array of `json_value_t`)
22-
* Boolean: `json_boolean_t` (Sugar for `unsigned char`)
23-
* Tag type: `json_type_t` with possible values
17+
* Data types:
18+
* `json_string_t`: The `String` type (alias for `const char *`)
19+
* `json_number_t`: The `Number` type (alias for `double`)
20+
* `json_object_t`: The `Object` type
21+
* `count`: Number of entries (as `size_t`)
22+
* `entries`: Array of `json_entry_t` of `.count` size
23+
* `json_array_t`: The heterogenious `Array` type
24+
* `count`: Number of elements (as `size_t`)
25+
* `elements`: Array of `json_array_element_t` of `.count` size
26+
* `json_boolean_t`: The `Boolean` type (alias for `unsigned char`)
27+
* `json_type_t`: An **enum** of basic JSON type
2428
* `JSON_TYPE_STRING`
2529
* `JSON_TYPE_NUMBER`
2630
* `JSON_TYPE_OBJECT`
2731
* `JSON_TYPE_ARRAY`
2832
* `JSON_TYPE_BOOLEAN`
2933
* `JSON_TYPE_NULL` _(only as an indicator)_
30-
* Key-Value Pair: `json_entry_t`
31-
* `type`: Enum `json_type_t` tag
32-
* `value`: Union `json_value_t` value
33-
* Union: `json_value_t` with easy to interpret fields
34+
* `json_value_t`: The JSON value **union** with easy to interpret fields
3435
* `as_string`: As `json_string_t` value
3536
* `as_number`: As `json_number_t` value
3637
* `as_object`: As `json_object_t *` value
3738
* `as_array`: As `json_array_t *` value
3839
* `as_boolean`: As `json_boolean_t` value
40+
* **Note**: The `null` type is not represented
41+
* `json_entry_t`: The Key-Value entry (used in `json_object_t`)
42+
* `key`: The key of the entry (as `json_string_t`)
43+
* `type`: The type of data represented `.value` field (as `json_type_t`)
44+
* `value`: The value of this entry (as `json_value_t`)
45+
* `json_array_element_t`: A typed element of an array (used in `json_array_t`)
46+
* `type`: The type of data represented by `.value` field (as `json_type_t`)
47+
* `value`: The value of this element (as `json_value_t`)
3948
* **Value or Error** (Rust like) `result` type used throughout fallible calls
4049
* Recursive parsing
4150
* Compile with `-DJSON_SCRAPE_WHITESPACE` to parse non-minified JSON with whitespace in between
@@ -108,7 +117,7 @@ const char * complex_json = "{\"name\":{\"first\":\"John\",\"last\":\"Doe\"},\"a
108117

109118
int main() {
110119
json_object_t *json = json_parse(complex_json);
111-
json_object_t *name_json = json->entires[0].value.as_object;
120+
json_object_t *name_json = json->entries[0].value.as_object;
112121
printf("First name: %s\nLast name: %s\nAge: %f",
113122
name_json->entries[0].value.as_string,
114123
name_json->entries[1].value.as_string,
@@ -143,6 +152,7 @@ At each Key-Value pair `json_entry_t`, there is a member `type`
143152

144153
json_object_t *json = json_parse(some_json_string);
145154
json_entry_t entry = json->entries[0];
155+
146156
switch(entry.type) {
147157
case JSON_TYPE_STRING:
148158
// `entry.value.as_string` is a `json_string_t`
@@ -172,7 +182,9 @@ In each `json_object_t`, there is a member `count`
172182
...
173183

174184
int i;
185+
175186
json_object_t *json = json_parse(some_json_string);
187+
176188
for(i = 0; i < json->count; i++) {
177189
json_entry_t entry = json->entries[i];
178190

@@ -193,11 +205,15 @@ In each `json_array_t`, there is a member `count`
193205
...
194206

195207
int i;
208+
196209
json_object_t *json = json_parse(some_json_string);
197210
json_array_t *array = json->entries[0].value.as_array;
198-
json_type_t type = array->type;
211+
199212
for(i = 0; i < array->count; i++) {
200-
json_value_t value = array->values[i];
213+
json_array_element_t element = array->elements[i];
214+
215+
json_type_t type = element.type;
216+
json_value_t value = element.value;
201217
// Do something with `value`
202218
}
203219
```

0 commit comments

Comments
 (0)