diff --git a/JsonParser/README.md b/JsonParser/README.md index 1e77fb21..a61c386c 100644 --- a/JsonParser/README.md +++ b/JsonParser/README.md @@ -25,7 +25,7 @@ Example JsonParser<32> parser; char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; - JsonHashTable root = parser.parse(json); + JsonObject root = parser.parse(json); char* sensor = root["sensor"]; long time = root["time"]; @@ -58,7 +58,7 @@ To extract data from the JSON string, you need to create a `JsonParser`, and spe > #### How to choose the number of tokens ? -> A token is an element of the JSON object: either a key, a value, an hash-table or an array. +> A token is an element of the JSON object: either a key, a value, an object or an array. > As an example the `char json[]` on the top of this page contains 12 tokens (don't forget to count 1 for the whole object and 1 more for the array itself). > The more tokens you allocate, the more complex the JSON can be, but also the more memory is occupied. @@ -71,28 +71,24 @@ To extract data from the JSON string, you need to create a `JsonParser`, and spe To use this library, you need to know beforehand what is the type of data contained in the JSON string, which is very likely. -The root object has to be either a hash-table (like `{"key":"value"}`) or an array (like `[1,2]`). +The root object has to be either an object (like `{"key":"value"}`) or an array (like `[1,2]`). -The nested objects can be either arrays, booleans, hash-tables, numbers or strings. +The nested objects can be either arrays, booleans, objects, numbers or strings. If you need other type, you can get the string value and parse it yourself. -#### Hash-table +#### Object Consider we have a `char json[]` containing to the following JSON string: { - "Name":"Blanchon", - "Skills":[ - "C", - "C++", - "C#"], - "Age":32, - "Online":true + "sensor":"gps", + "time":1351824120, + "data":[48.756080,2.302038] } -In this case the root object of the JSON string is a hash-table, so you need to extract a `JsonHashTable`: +In this case the string contains a JSON object, so you need to extract a `JsonObject`: - JsonHashTable root = parser.parseHashTable(json); + JsonObject root = parser.parse(json); To check if the parsing was successful, you must check: @@ -103,13 +99,10 @@ To check if the parsing was successful, you must check: And then extract the member you need: - char* name = hashTable.getString("Name"); - - JsonArray skills = hashTable.getArray("Skills"); - - int age = hashTable.getLong("Age"); - - bool online = hashTable.getBool("Online"); + char* sensor = root["sensor"]; + long time = root["time"]; + double latitude = root["data"][0]; + double longitude = root["data"][1]; #### Array @@ -122,7 +115,7 @@ Consider we have a `char json[]` containing to the following JSON string: In this case the root object of the JSON string is an array, so you need to extract a `JsonArray`: - JsonArray root = parser.parseArray(json); + JsonArray root = parser.parse(json); To check if the parsing was successful, you must check: @@ -133,20 +126,17 @@ To check if the parsing was successful, you must check: And then extract the content by its index in the array: - JsonArray row0 = root.getArray(0); - double a = row0.getDouble(0); - -or simply: - - double a = root.getArray(0).getDouble(0); - - + double a = root[0][0]; + double b = root[0][1]; + double c = root[1][0]; + double d = root[1][1]; + Common pitfalls --------------- ### 1. Not enough tokens -By design, the library has no way to tell you why `JsonParser::parseArray()` or `JsonParser::parseHashTable()` failed. +By design, the library has no way to tell you why `JsonParser::parse()` failed. There are basically two reasons why they may fail: @@ -173,7 +163,7 @@ That is why an 8-bit processor is not able to parse long and complex JSON string ### 3. JsonParser not in memory -To reduce the memory consumption, `JsonArray` and `JsonHashTable` contains pointer to the token that are inside the `JsonParser`. This can only work if the `JsonParser` is still in memory. +To reduce the memory consumption, `JsonValue`, `JsonArray` and `JsonObject` contains pointer to the token that are inside the `JsonParser`. This can only work if the `JsonParser` is still in memory. For example, don't do this: @@ -189,7 +179,7 @@ because the local variable `parser` will be *removed* from memory when the funct This will probably never be an issue, but you need to be aware of this feature. -When you pass a `char[]` to `JsonParser::parseArray()` or `JsonParser::parseHashTable()`, the content of the string will be altered to add `\0` at the end of the tokens. +When you pass a `char[]` to `JsonParser::parse()`, the content of the string will be altered to add `\0` at the end of the tokens. This is because we want functions like `JsonArray::getString()` to return a null-terminating string without any memory allocation. @@ -215,188 +205,7 @@ This table is for an 8-bit Arduino, types would be bigger on a 32-bit processor. 4 - JsonHashTable + JsonObject 4 - - - -Code size ---------- - -Theses tables has been created by analyzing the map file generated by AVR-GCC after adding `-Wl,-Map,foo.map` to the command line. - -As you'll see the code size is between 1680 and 3528 bytes, depending on the features you use. - -### Minimum setup - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FunctionSize in bytes
strcmp(char*,char*)18
jsmn_init(jsmn_parser*)20
jsmn_parse(jsmn_parser*, char const*, jsmntok_t*, unsigned int)960
JsonParser::parse(char*)106
JsonObjectBase::getNestedTokenCount(jsmntok_t*)84
JsonObjectBase::getStringFromToken(jsmntok_t*)68
JsonArray::JsonArray(char*, jsmntok_t*)42
JsonArray::getToken(int)112
JsonArray::getString(int)18
JsonHashTable::JsonHashTable(char*, jsmntok_t*)42
JsonHashTable::getToken(char*)180
JsonHashTable::getString(char*)18
TOTAL1680
- -### Additional space to parse nested objects - - - - - - - - - - - - - - - - - - - - - - - - - - -
FunctionSize in bytes
JsonArray::getArray(int)42
JsonArray::getHashTable(int)64
JsonHashTable::getArray(char*)64
JsonHashTable::getHashTable(char*)42
TOTAL212
- -### Additional space to parse `bool` values - - - - - - - - - - - - - - - - - - - - - - -
FunctionSize in bytes
JsonObjectBase::getBoolFromToken(jsmntok_t*)82
JsonArray::getBool(int)18
JsonHashTable::getBool(char*)18
TOTAL130
- -### Additional space to parse `double` values - - - - - - - - - - - - - - - - - - - - - - - - - - -
FunctionSize in bytes
strtod(char*,int)704
JsonObjectBase::getDoubleFromToken(jsmntok_t*)44
JsonArray::getDouble(int)18
JsonHashTable::getDouble(char*)18
TOTAL796
- -### Additional space to parse `long` values - - - - - - - - - - - - - - - - - - - - - - - - - - -
FunctionSize in bytes
strtol(char*,char**,int)606
JsonObjectBase::getLongFromToken(jsmntok_t*)56
JsonArray::getLong(int)18
JsonHashTable::getLong(char*)18
TOTAL710
\ No newline at end of file + \ No newline at end of file