cleanup: move cJSON to component manager

This commit is contained in:
Mahavir Jain
2025-10-06 11:52:40 +05:30
parent f8935f87e7
commit 439b1d6d08
21 changed files with 112 additions and 279 deletions
-1
View File
@@ -136,7 +136,6 @@
/components/http_parser/ @esp-idf-codeowners/app-utilities
/components/idf_test/ @esp-idf-codeowners/peripherals @esp-idf-codeowners/system
/components/ieee802154/ @esp-idf-codeowners/ieee802154
/components/json/ @esp-idf-codeowners/app-utilities
/components/linux/ @esp-idf-codeowners/system
/components/log/ @esp-idf-codeowners/system
/components/lwip/ @esp-idf-codeowners/lwip
-1
View File
@@ -147,7 +147,6 @@
- "components/esp_phy/lib"
- "components/esp_wifi/lib"
- "components/esp_coex/lib"
- "components/json/cJSON"
- "components/lwip/lwip"
- "components/mbedtls/mbedtls"
- "components/mqtt/esp-mqtt"
-11
View File
@@ -46,17 +46,6 @@
sbom-description = Wear-leveled SPI flash file system for embedded devices
sbom-hash = 0dbb3f71c5f6fae3747a9d935372773762baf852
[submodule "components/json/cJSON"]
path = components/json/cJSON
url = ../../DaveGamble/cJSON.git
sbom-version = 1.7.19
sbom-cpe = cpe:2.3:a:cjson_project:cjson:{}:*:*:*:*:*:*:*
sbom-cpe = cpe:2.3:a:davegamble:cjson:{}:*:*:*:*:*:*:*
sbom-supplier = Person: Dave Gamble
sbom-url = https://github.com/DaveGamble/cJSON
sbom-description = Ultralightweight JSON parser in ANSI C
sbom-hash = c859b25da02955fef659d658b8f324b5cde87be3
[submodule "components/mbedtls/mbedtls"]
path = components/mbedtls/mbedtls
url = ../../espressif/mbedtls.git
@@ -2,7 +2,7 @@ idf_build_get_property(idf_path IDF_PATH)
set(priv_requires bootloader_support esp_driver_gptimer esp_tee esp_timer mbedtls spi_flash)
# Test FW related
list(APPEND priv_requires json nvs_flash test_utils unity)
list(APPEND priv_requires nvs_flash test_utils unity)
# TEE related
list(APPEND priv_requires tee_sec_storage tee_attestation tee_ota_ops test_sec_srv)
@@ -1,5 +1,6 @@
dependencies:
ccomp_timer: "^1.0.0"
espressif/cjson: "^1.7.19"
tee_attestation:
path: ${IDF_PATH}/components/esp_tee/subproject/components/tee_attestation
tee_ota_ops:
-3
View File
@@ -1,3 +0,0 @@
idf_component_register(SRCS "cJSON/cJSON.c"
"cJSON/cJSON_Utils.c"
INCLUDE_DIRS cJSON)
-247
View File
@@ -1,247 +0,0 @@
/*
Copyright (c) 2009 Dave Gamble
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
Welcome to cJSON.
cJSON aims to be the dumbest possible parser that you can get your job done with.
It's a single file of C, and a single header file.
JSON is described best here: http://www.json.org/
It's like XML, but fat-free. You use it to move data around, store things, or just
generally represent your program's state.
First up, how do I build?
Add cJSON.c to your project, and put cJSON.h somewhere in the header search path.
For example, to build the test app:
gcc cJSON.c test.c -o test -lm
./test
As a library, cJSON exists to take away as much legwork as it can, but not get in your way.
As a point of pragmatism (i.e. ignoring the truth), I'm going to say that you can use it
in one of two modes: Auto and Manual. Let's have a quick run-through.
I lifted some JSON from this page: http://www.json.org/fatfree.html
That page inspired me to write cJSON, which is a parser that tries to share the same
philosophy as JSON itself. Simple, dumb, out of the way.
Some JSON:
{
"name": "Jack (\"Bee\") Nimble",
"format": {
"type": "rect",
"width": 1920,
"height": 1080,
"interlace": false,
"frame rate": 24
}
}
Assume that you got this from a file, a webserver, or magic JSON elves, whatever,
you have a char * to it. Everything is a cJSON struct.
Get it parsed:
cJSON *root = cJSON_Parse(my_json_string);
This is an object. We're in C. We don't have objects. But we do have structs.
What's the framerate?
cJSON *format = cJSON_GetObjectItem(root,"format");
int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;
Want to change the framerate?
cJSON_GetObjectItem(format,"frame rate")->valueint=25;
Back to disk?
char *rendered=cJSON_Print(root);
Finished? Delete the root (this takes care of everything else).
cJSON_Delete(root);
That's AUTO mode. If you're going to use Auto mode, you really ought to check pointers
before you dereference them. If you want to see how you'd build this struct in code?
cJSON *root,*fmt;
root=cJSON_CreateObject();
cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
cJSON_AddItemToObject(root, "format", fmt=cJSON_CreateObject());
cJSON_AddStringToObject(fmt,"type", "rect");
cJSON_AddNumberToObject(fmt,"width", 1920);
cJSON_AddNumberToObject(fmt,"height", 1080);
cJSON_AddFalseToObject (fmt,"interlace");
cJSON_AddNumberToObject(fmt,"frame rate", 24);
Hopefully we can agree that's not a lot of code? There's no overhead, no unnecessary setup.
Look at test.c for a bunch of nice examples, mostly all ripped off the json.org site, and
a few from elsewhere.
What about manual mode? First up you need some detail.
Let's cover how the cJSON objects represent the JSON data.
cJSON doesn't distinguish arrays from objects in handling; just type.
Each cJSON has, potentially, a child, siblings, value, a name.
The root object has: Object Type and a Child
The Child has name "name", with value "Jack ("Bee") Nimble", and a sibling:
Sibling has type Object, name "format", and a child.
That child has type String, name "type", value "rect", and a sibling:
Sibling has type Number, name "width", value 1920, and a sibling:
Sibling has type Number, name "height", value 1080, and a sibling:
Sibling has type False, name "interlace", and a sibling:
Sibling has type Number, name "frame rate", value 24
Here's the structure:
typedef struct cJSON {
struct cJSON *next,*prev;
struct cJSON *child;
int type;
char *valuestring;
int valueint;
double valuedouble;
char *string;
} cJSON;
By default all values are 0 unless set by virtue of being meaningful.
next/prev is a doubly linked list of siblings. next takes you to your sibling,
prev takes you back from your sibling to you.
Only objects and arrays have a "child", and it's the head of the doubly linked list.
A "child" entry will have prev==0, but next potentially points on. The last sibling has next=0.
The type expresses Null/True/False/Number/String/Array/Object, all of which are #defined in
cJSON.h
A Number has valueint and valuedouble. If you're expecting an int, read valueint, if not read
valuedouble.
Any entry which is in the linked list which is the child of an object will have a "string"
which is the "name" of the entry. When I said "name" in the above example, that's "string".
"string" is the JSON name for the 'variable name' if you will.
Now you can trivially walk the lists, recursively, and parse as you please.
You can invoke cJSON_Parse to get cJSON to parse for you, and then you can take
the root object, and traverse the structure (which is, formally, an N-tree),
and tokenise as you please. If you wanted to build a callback style parser, this is how
you'd do it (just an example, since these things are very specific):
void parse_and_callback(cJSON *item,const char *prefix)
{
while (item)
{
char *newprefix=malloc(strlen(prefix)+strlen(item->name)+2);
sprintf(newprefix,"%s/%s",prefix,item->name);
int dorecurse=callback(newprefix, item->type, item);
if (item->child && dorecurse) parse_and_callback(item->child,newprefix);
item=item->next;
free(newprefix);
}
}
The prefix process will build you a separated list, to simplify your callback handling.
The 'dorecurse' flag would let the callback decide to handle sub-arrays on it's own, or
let you invoke it per-item. For the item above, your callback might look like this:
int callback(const char *name,int type,cJSON *item)
{
if (!strcmp(name,"name")) { /* populate name */ }
else if (!strcmp(name,"format/type") { /* handle "rect" */ }
else if (!strcmp(name,"format/width") { /* 800 */ }
else if (!strcmp(name,"format/height") { /* 600 */ }
else if (!strcmp(name,"format/interlace") { /* false */ }
else if (!strcmp(name,"format/frame rate") { /* 24 */ }
return 1;
}
Alternatively, you might like to parse iteratively.
You'd use:
void parse_object(cJSON *item)
{
int i; for (i=0;i<cJSON_GetArraySize(item);i++)
{
cJSON *subitem=cJSON_GetArrayItem(item,i);
// handle subitem.
}
}
Or, for PROPER manual mode:
void parse_object(cJSON *item)
{
cJSON *subitem=item->child;
while (subitem)
{
// handle subitem
if (subitem->child) parse_object(subitem->child);
subitem=subitem->next;
}
}
Of course, this should look familiar, since this is just a stripped-down version
of the callback-parser.
This should cover most uses you'll find for parsing. The rest should be possible
to infer.. and if in doubt, read the source! There's not a lot of it! ;)
In terms of constructing JSON data, the example code above is the right way to do it.
You can, of course, hand your sub-objects to other functions to populate.
Also, if you find a use for it, you can manually build the objects.
For instance, suppose you wanted to build an array of objects?
cJSON *objects[24];
cJSON *Create_array_of_anything(cJSON **items,int num)
{
int i;cJSON *prev, *root=cJSON_CreateArray();
for (i=0;i<24;i++)
{
if (!i) root->child=objects[i];
else prev->next=objects[i], objects[i]->prev=prev;
prev=objects[i];
}
return root;
}
and simply: Create_array_of_anything(objects,24);
cJSON doesn't make any assumptions about what order you create things in.
You can attach the objects, as above, and later add children to each
of those objects.
As soon as you call cJSON_Print, it renders the structure to text.
The test.c code shows how to handle a bunch of typical cases. If you uncomment
the code, it'll load, parse and print a bunch of test files, also from json.org,
which are more complex than I'd care to try and stash into a const char array[].
Enjoy cJSON!
- Dave Gamble, Aug 2009
-3
View File
@@ -45,8 +45,6 @@ These third party libraries can be included into the application (firmware) prod
* `FatFS`_ library, Copyright (C) 2017 ChaN, is licensed under :component_file:`a BSD-style license <fatfs/src/ff.h#L1-L18>`.
* `cJSON`_ library, Copyright (C) 2009-2017 Dave Gamble and cJSON contributors, is licensed under MIT License as described in :component_file:`LICENSE file <json/cJSON/LICENSE>`.
* `micro-ecc`_ library, Copyright (C) 2014 Kenneth MacKay, is licensed under 2-clause BSD License.
* `Mbed TLS`_ library, Copyright (C) 2006-2018 ARM Limited, is licensed under Apache License 2.0 as described in :component_file:`LICENSE file <mbedtls/mbedtls/LICENSE>`.
@@ -155,7 +153,6 @@ Copyright (C) 2011 ChaN, all right reserved.
.. _argtable3: https://github.com/argtable/argtable3
.. _linenoise: https://github.com/antirez/linenoise
.. _fatfs: http://elm-chan.org/fsw/ff/00index_e.html
.. _cJSON: https://github.com/DaveGamble/cJSON
.. _micro-ecc: https://github.com/kmackay/micro-ecc
.. _OpenBSD SD/MMC driver: https://github.com/openbsd/src/blob/f303646/sys/dev/sdmmc/sdmmc.c
.. _Mbed TLS: https://github.com/Mbed-TLS/mbedtls
+1 -1
View File
@@ -1155,7 +1155,7 @@ Here is an example minimal "pure CMake" component CMakeLists file for a componen
target_include_directories(json PUBLIC cJSON)
- This is actually an equivalent declaration to the IDF ``json`` component :idf_file:`/components/json/CMakeLists.txt`.
- This is actually an equivalent declaration to the `espressif/cjson <https://github.com/espressif/idf-extra-components/tree/master/cjson>`_ managed component.
- This file is quite simple as there are not a lot of source files. For components with a large number of files, the globbing behavior of ESP-IDF's component logic can make the component CMakeLists style simpler.)
- Any time a component adds a library target with the component name, the ESP-IDF build system will automatically add this to the build, expose public include directories, etc. If a component wants to add a library target with a different name, dependencies will need to be added manually via CMake commands.
@@ -3,6 +3,58 @@ Protocols
:link_to_translation:`zh_CN:[中文]`
JSON
----
Removed Built-in JSON Component
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The built-in ``json`` component has been removed from ESP-IDF. Users should migrate to using the ``espressif/cjson`` component from the `IDF Component Manager <https://components.espressif.com/>`_.
Migration Steps
^^^^^^^^^^^^^^^
1. **Remove json from CMakeLists.txt**
In your component's ``CMakeLists.txt``, remove ``json`` from the ``REQUIRES`` or ``PRIV_REQUIRES`` list:
.. code-block:: cmake
# Before
idf_component_register(SRCS "main.c"
PRIV_REQUIRES json esp_http_server)
# After
idf_component_register(SRCS "main.c"
PRIV_REQUIRES esp_http_server)
2. **Add espressif/cjson to idf_component.yml**
Add the ``espressif/cjson`` dependency to your component's ``idf_component.yml`` file. If this file doesn't exist, create it in your component directory (e.g., ``main/idf_component.yml``):
.. code-block:: yaml
dependencies:
espressif/cjson: "^1.7.19"
3. **No Code Changes Required**
The API remains the same. Your existing code using cJSON functions will continue to work without modifications:
.. code-block:: c
#include "cJSON.h"
// Existing code works unchanged
cJSON *root = cJSON_Parse(json_string);
cJSON *item = cJSON_GetObjectItem(root, "key");
cJSON_Delete(root);
For more information:
- `espressif/cjson component <https://components.espressif.com/components/espressif/cjson>`_
- `cJSON on GitHub <https://github.com/espressif/idf-extra-components/tree/master/cjson>`_
ESP-TLS
-------
-3
View File
@@ -45,8 +45,6 @@
* `FatFS`_ 库,版权归 2017 ChaN 所有,并根据 :component_file:`BSD 式条款 <fatfs/src/ff.h#L1-L18>` 进行许可。
* `cJSON`_ 库,版权归 2009-2017 Dave Gamble 及 cJSON 库贡献者所有,根据 :component_file:`LICENSE 文件 <json/cJSON/LICENSE>` 中描述的 MIT 许可证进行许可。
* `micro-ecc`_ 库,版权归 2014 Kenneth MacKay 所有,根据二条款 BSD 许可证进行许可。
* `Mbed TLS`_ 库,版权归 2006-2018 安谋控股公司所有,根据 :component_file:`LICENSE 文件 <mbedtls/mbedtls/LICENSE>` 中描述的 Apache License 2.0 进行许可。
@@ -155,7 +153,6 @@ TJpgDec - Tiny JPEG 解压器 R0.01 (C) 2011 ChaN,是一个用于小型嵌入
.. _argtable3: https://github.com/argtable/argtable3
.. _linenoise: https://github.com/antirez/linenoise
.. _fatfs: http://elm-chan.org/fsw/ff/00index_e.html
.. _cJSON: https://github.com/DaveGamble/cJSON
.. _micro-ecc: https://github.com/kmackay/micro-ecc
.. _OpenBSD SD/MMC 驱动程序: https://github.com/openbsd/src/blob/f303646/sys/dev/sdmmc/sdmmc.c
.. _Mbed TLS: https://github.com/Mbed-TLS/mbedtls
+1 -1
View File
@@ -1155,7 +1155,7 @@ ESP-IDF 构建系统用“组件”的概念“封装”了 CMake,并提供了
target_include_directories(json PUBLIC cJSON)
- 这实际上与 IDF 中的 :idf_file:`json 组件 </components/json/CMakeLists.txt>` 是等效的。
- 这实际上与 `espressif/cjson <https://github.com/espressif/idf-extra-components/tree/master/cjson>`_ 托管组件是等效的。
- 因为组件中的源文件不多,所以这个 CMakeLists 文件非常简单。对于具有大量源文件的组件而言,ESP-IDF 支持的组件通配符,可以简化组件 CMakeLists 的样式。
- 每当组件中新增一个与组件同名的库目标时,ESP-IDF 构建系统会自动将其添加到构建中,并公开公共的 include 目录。如果组件想要添加一个与组件不同名的库目标,就需要使用 CMake 命令手动添加依赖关系。
@@ -3,6 +3,58 @@
:link_to_translation:`en:[English]`
JSON
----
移除内置 JSON 组件
~~~~~~~~~~~~~~~~~~
ESP-IDF 已移除内置的 ``json`` 组件。用户应迁移至使用 `IDF 组件管理器 <https://components.espressif.com/>`_ 中的 ``espressif/cjson`` 组件。
迁移步骤
^^^^^^^^
1. **从 CMakeLists.txt 中移除 json**
在组件的 ``CMakeLists.txt`` 文件中,从 ``REQUIRES````PRIV_REQUIRES`` 列表中移除 ``json``
.. code-block:: cmake
# 迁移前
idf_component_register(SRCS "main.c"
PRIV_REQUIRES json esp_http_server)
# 迁移后
idf_component_register(SRCS "main.c"
PRIV_REQUIRES esp_http_server)
2. **将 espressif/cjson 添加到 idf_component.yml**
``espressif/cjson`` 依赖项添加到组件的 ``idf_component.yml`` 文件中。如果该文件不存在,请在组件目录中创建它(例如 ``main/idf_component.yml``):
.. code-block:: yaml
dependencies:
espressif/cjson: "^1.7.19"
3. **无需更改代码**
API 保持不变。现有的使用 cJSON 函数的代码无需修改即可继续工作:
.. code-block:: c
#include "cJSON.h"
// 现有代码无需更改
cJSON *root = cJSON_Parse(json_string);
cJSON *item = cJSON_GetObjectItem(root, "key");
cJSON_Delete(root);
更多信息:
- `espressif/cjson 组件 <https://components.espressif.com/components/espressif/cjson>`_
- `cJSON GitHub <https://github.com/espressif/idf-extra-components/tree/master/cjson>`_
ESP-TLS
-------
@@ -1,5 +1,5 @@
set(srcs "i2c_slave_main.c")
idf_component_register(SRCS ${srcs}
PRIV_REQUIRES esp_http_client esp_wifi nvs_flash json esp_driver_i2c
PRIV_REQUIRES esp_http_client esp_wifi nvs_flash esp_driver_i2c
INCLUDE_DIRS ".")
@@ -1,3 +1,4 @@
dependencies:
espressif/cjson: "^1.7.19"
protocol_examples_common:
path: ${IDF_PATH}/examples/common_components/protocol_examples_common
@@ -1,6 +1,6 @@
idf_component_register(SRCS "esp_rest_main.c"
"rest_server.c"
PRIV_REQUIRES esp_http_server json nvs_flash
PRIV_REQUIRES esp_http_server nvs_flash
INCLUDE_DIRS ".")
if(CONFIG_EXAMPLE_DEPLOY_WEB_PAGES)
@@ -1,5 +1,6 @@
## IDF Component Manager Manifest File
dependencies:
espressif/cjson: "^1.7.19"
espressif/mdns: "^1.8.0"
joltwallet/littlefs: "^1.20.0"
protocol_examples_common:
-2
View File
@@ -20,7 +20,6 @@ submodules:
- "/components/esp_phy/lib/"
- "/components/esp_wifi/lib/"
- "/components/heap/tlsf/"
- "/components/json/cJSON/"
- "/components/lwip/lwip/"
- "/components/mbedtls/mbedtls/"
- "/components/mqtt/esp-mqtt/"
@@ -70,7 +69,6 @@ components_not_formatted_temporary:
- "/components/heap/"
- "/components/idf_test/"
- "/components/ieee802154/"
- "/components/json/"
- "/components/lwip/"
- "/components/mbedtls/"
- "/components/mqtt/"
@@ -54,7 +54,6 @@ components/fatfs/src/ffconf.h
components/idf_test/include/idf_performance.h
components/json/cJSON/
components/spiffs/include/spiffs_config.h
-1
View File
@@ -18,7 +18,6 @@ skip:
- "components/esp_phy/lib"
- "components/esp_wifi/lib"
- "components/esp_wifi/lib_esp32"
- "components/json/cJSON"
- "components/lwip/lwip"
- "components/mbedtls/mbedtls"
- "components/mqtt/esp-mqtt"