As the title states, this is to be a C-based parser and codec (outputter/writer) for JSON data.
// heap memory
arena_t arena;
arena_create(&arena, 4096 * 4);
// json creation
json_value_t * obj = json_make_object(&arena);
json_object_add(&arena, obj, "name", json_make_string(&arena, "Sailor"));
json_object_add(&arena, obj, "age", json_make_number(&arena, 42));
json_value_t * array = json_make_array(&arena);
json_array_append(&arena, array, json_make_string(&arena, "one"));
json_array_append(&arena, array, json_make_string(&arena, "two"));
json_object_add(&arena, obj, "list", array);
// final node
json_result_t result = {
.root = obj,
.err = NULL
};
// dump to terminal
json_dump(result);
// cleanup heap memory
arena_destroy(&arena);The expected result is:
{
"name" : "Sailor",
"age" : 42,
"list" : [
"one",
"two"
]
}JSON API
JSON ORG
Wikipedia/JSON
RFC4627
Library: cJSON
Library: jsmn (jasmine)
Library: sheredom/utf8.h
IBM: JSON Parser Limits
JSON uses the filename extension ".json"
The media type (MIME) is "application/json"
It uses UTF-8 character encoding.
| Data Type | Description |
|---|---|
| Number | a signed decimal number that may use E notation but cannot be non-numbers like NaN |
| String | zero or more Unicode chars, delimited with '"' and supports '\n' escape syntax |
| Boolean | either 'true' or 'false |
| Array | ordererd list of zero or more elements. Uses '[' and ']' bracket notation with ',' separeted elements |
| Object | A collection of name-value pairs or [UTF-8 String]Key-Value pairs |
| null | an empty value - not a pointer |
Whitespace is allowed and ignored around syntactic elements but not withing a string value.
Four specific characters are whitespace in JSON: Space:' ', Tab:'\t', LF:'\n', CR:'\r'.
For UTF-8 and other, the Byte Order Mark (BOM) must not be generated by a conforming implementation. (U+FEFF = zero width no-break space)
JSON allows for U+2028 (Unicode: Line Separator) and U+2029 (Unicode: Paragraph Separator) to appear UNESCAPED in qouted strings.
JSON does not have comments.
JSON does not differentiate between numbers as integers or floating-point values (real). (Thus '42', '42.0', '4.2E+1' can all be the same number, depending on the implementation)
Note: JS uses IEEE-754 double-precision floating-point format for all its numeric values.
Since JSON uses UTF-8, it can support the full Unicode character set, including characters outside the Basic Multilingual Plane (U+0000 to U+FFFF). If escaped, those characters MUST be written using UTF-16 surrogate pairs.
Example: (U+1F610, Unicode: Neutral Face) '๐' has be (in JSON):
{ "face": "๐" }
// or
{ "face": "\uD83D\uDE10" }{
"first_name": "John",
"last_name": "Smith",
"is_alive": true,
"age": 27,
"address": {
"street_address": "21 2nd Street",
"city": "New York",
"state": "NY",
"postal_code": "10021-3100"
},
"phone_numbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [
"Catherine",
"Thomas",
"Trevor"
],
"spouse": null
}{
"medications":[{
"aceInhibitors":[{
"name":"lisinopril",
"strength":"10 mg Tab",
"dose":"1 tab",
"route":"PO",
"sig":"daily",
"pillCount":"#90",
"refills":"Refill 3"
}],
"antianginal":[{
"name":"nitroglycerin",
"strength":"0.4 mg Sublingual Tab",
"dose":"1 tab",
"route":"SL",
"sig":"q15min PRN",
"pillCount":"#30",
"refills":"Refill 1"
}],
"anticoagulants":[{
"name":"warfarin sodium",
"strength":"3 mg Tab",
"dose":"1 tab",
"route":"PO",
"sig":"daily",
"pillCount":"#90",
"refills":"Refill 3"
}],
"betaBlocker":[{
"name":"metoprolol tartrate",
"strength":"25 mg Tab",
"dose":"1 tab",
"route":"PO",
"sig":"daily",
"pillCount":"#90",
"refills":"Refill 3"
}],
"diuretic":[{
"name":"furosemide",
"strength":"40 mg Tab",
"dose":"1 tab",
"route":"PO",
"sig":"daily",
"pillCount":"#90",
"refills":"Refill 3"
}],
"mineral":[{
"name":"potassium chloride ER",
"strength":"10 mEq Tab",
"dose":"1 tab",
"route":"PO",
"sig":"daily",
"pillCount":"#90",
"refills":"Refill 3"
}]
}
],
"labs":[{
"name":"Arterial Blood Gas",
"time":"Today",
"location":"Main Hospital Lab"
},
{
"name":"BMP",
"time":"Today",
"location":"Primary Care Clinic"
},
{
"name":"BNP",
"time":"3 Weeks",
"location":"Primary Care Clinic"
},
{
"name":"BUN",
"time":"1 Year",
"location":"Primary Care Clinic"
},
{
"name":"Cardiac Enzymes",
"time":"Today",
"location":"Primary Care Clinic"
},
{
"name":"CBC",
"time":"1 Year",
"location":"Primary Care Clinic"
},
{
"name":"Creatinine",
"time":"1 Year",
"location":"Main Hospital Lab"
},
{
"name":"Electrolyte Panel",
"time":"1 Year",
"location":"Primary Care Clinic"
},
{
"name":"Glucose",
"time":"1 Year",
"location":"Main Hospital Lab"
},
{
"name":"PT/INR",
"time":"3 Weeks",
"location":"Primary Care Clinic"
},
{
"name":"PTT",
"time":"3 Weeks",
"location":"Coumadin Clinic"
},
{
"name":"TSH",
"time":"1 Year",
"location":"Primary Care Clinic"
}
],
"imaging":[{
"name":"Chest X-Ray",
"time":"Today",
"location":"Main Hospital Radiology"
},
{
"name":"Chest X-Ray",
"time":"Today",
"location":"Main Hospital Radiology"
},
{
"name":"Chest X-Ray",
"time":"Today",
"location":"Main Hospital Radiology"
}
]
}