Example of JSON Serialize and Deserialize in ESP-IDF
Example of JSON Serialize and Deserialize with ESP-IDF.
ESP-IDF includes this cJSON library.
You can use JSON components as standard.
But the documentation is very poor.
The official documentation for the JSON component is here.
Here is a example by Dave Gamble.
git clone https://github.com/nopnop2002/esp-idf-json
cd esp-idf-json/json-basic-object
idf.py flash monitor
From print_value() function, the pointer returned is allocated by cJSON_strdup() and it is returned to the caller.
Buffers returned by cJSON_Print must be freed by the caller.
Please use the proper API (cJSON_free) rather than directly calling stdlib free.
char *my_json_string = cJSON_Print(root);
ESP_LOGI(TAG, "my_json_string\n%s",my_json_string);
cJSON_Delete(root);
cJSON_free(my_json_string);
["abc", 123, true, false, null]
{
"version": "v5.0-dev-3202-ga2d5041492-dirty",
"cores": 2,
}
["abc", 123, true, false, null, {
"version": "v5.0-dev-3202-ga2d5041492-dirty",
"cores": 2
}]
[{
"id": 0,
"version": "v5.0-dev-3202-ga2d5041492-dirty",
"cores": 2
}, {
"id": 1,
"version": "v5.0-dev-3202-ga2d5041492-dirty",
"cores": 2
}]
[["abc", 120], ["abc", 121], ["abc", 122], ["abc", 123], ["abc", 124], ["abc", 125], ["abc", 126], ["abc", 127], ["abc", 128], ["abc", 129]]
{
"name": "Jack (\"Bee\") Nimble",
"format": {
"type": "rect",
"width": 1920,
"height": 1080,
"interlace": false,
"frame rate": 24
}
}
{
"version": "v5.0-dev-3202-ga2d5041492-dirty",
"cores": 2,
"array": ["abc", 123, true, false, null]
}
{
"version": "v5.0-dev-3202-ga2d5041492-dirty",
"cores": 2,
"array": [{
"width": 1280,
"height": 720
}, {
"width": 1920,
"height": 1080
}, {
"width": 3840,
"height": 2160
}]
}
[{
"id": 0,
"version": "v5.0-dev-3202-ga2d5041492-dirty",
"cores": 2,
"array": ["abc", 123, true, false, null]
}, {
"id": 1,
"version": "v5.0-dev-3202-ga2d5041492-dirty",
"cores": 2,
"array": ["abc", 123, true, false, null]
}]