Page 1 of 1

How to include cJSON?

Posted: Thu Aug 27, 2020 7:06 am
by ryanf55
Hello,

I am using ESPIDFv4.0 and would like to include cJSON. None of the following work:

#include <cJSON/cJSON.h>
#include <cJSON.h>
#include <json/cJSON/cJSON.h>

What do I need to use in order to use cJSON in my code?

Re: How to include cJSON?

Posted: Thu Aug 27, 2020 7:27 am
by ESP-Marius
#include <cJSON.h> should work. Remember to also include

Code: Select all

REQUIRES json
in your CMakeList.txt if you are trying to use it outside your main component.

Re: How to include cJSON?

Posted: Thu Aug 27, 2020 5:10 pm
by ryanf55
Thank you, yes have a few of my own components that are needing cJSON. Your suggestions worked.

For anyone else needing assistance, here's the contents of the cv_server CMakeLists.txt, noting the addition of requiring json.

Code: Select all

idf_component_register(SRCS "cv_server.c"
                       INCLUDE_DIRS "include"
                       REQUIRES esp_http_server cv_mqtt cv_ota json
                       EMBED_TXTFILES 
                           WebsiteFiles/settings.html 
                           WebsiteFiles/httpDocBegin.html
                           WebsiteFiles/httpDocConclude.html
                           WebsiteFiles/subtitle.html
                           WebsiteFiles/title.html
                           WebsiteFiles/wifiSettings.html
                           WebsiteFiles/test.html
                           WebsiteFiles/menuBar.html
                           WebsiteFiles/menuBarTest.html
                       )
                   
Here's the abbreviated folder structure. I bolded the files that are changed to use json.

ryan@ryan-Precision-5820-Tower:~/Development/personal/cv/clearview_interface_public/src/cvesp32$ tree -L 4
.
├── CMakeLists.txt
├── components
│ ├── cv_server
│ │ ├── CMakeLists.txt
│ │ ├── cv_server.c
│ │ ├── include
│ │ │ └── cv_server.h
│ │ └── WebsiteFiles
│ │ ├── menuBar.html
│ │ ├── settings.html
├── main
│ ├── CMakeLists.txt
│ ├── Kconfig.projbuild
│ └── main.c
├── sdkconfig
├── sdkconfig.defaults
└── version.txt

I was under the impression that any of the components in ESPIDF did not need to be required, but I was wrong.
Thanks!

Re: How to include cJSON?

Posted: Sun Oct 17, 2021 12:00 pm
by eggnogg
For IDF 4.3.1 i had to add
set(REQUIRES json)

to CMakeLists.txt of my library

I thought it was working but it didn't finish building

this is the correct way

Code: Select all

set(COMPONENT_REQUIRES "json")