Comments (5)
What version of cJSON are you using?
When you say "crashes" do you mean that cJSON_Parse
returned NULL or did it actually crash?
Is the JSON you are showing exactly what was passed to the parser?
I tried your JSON object as shown and cJSON_Parse
in cJSON version 1.7.15 and version 1.7.17 and parsed it fine.
from cjson.
I am using ESP-IDFs cJSON, so I guess it is the last version? I don't know, and it seems I can't check it either.
When I say it "crashes", it means that because the root object is NULL, it panics the core, rebooting the ESP32 (Btw, it's a ESP32-S3). The JSON I showed is exactly the same my MQTT broker is sending to the ESP32.
EDIT: I tried with a much simpler script and JSON, and yet again the root object is null.
void parseJSON(char *json)
{
cJSON *root = cJSON_Parse(json);
if (root == NULL)
{
const char *error_ptr = cJSON_GetErrorPtr();
printf("Error: %s\n", error_ptr);
}
char *value = cJSON_GetObjectItemCaseSensitive(root, "key")->valuestring;
printf("JSON KEY: %s\n", value);
}
{"key": "value"}
The cJSON_GetErrorPtr() output is:
Error: key': 'value'}���?
from cjson.
It's interesting you are seeing a "core panic" since you are able to print an error message after the parser returns NULL. If you are trying to use the NULL value later, that could be reason for the core panic.
I was hoping you could also show the actual string you are passing to the JSON parser. printf
it out so it can all be seen. I'm currently suspicious that you have bad characters in the input string which causes the parser to reject the string and return null. Check that the string is properly terminated (your cJSON_GetErrorPtr()
output seems to indicate there may be some junk at the end of the string.
from cjson.
I think the core panic occurs when calling the cJSON_GetObjectItemCaseSensitive(root,"key")->valuestring;
function, since the root object is NULL, so, that makes it read from an empty memory address. Anyway, indeed, it seems to be some junk at the end of the string, how can I remove it?
void parseJSON(char *json)
{
printf("%s\n", json); // Prints {'key': 'value'}␙�␑
cJSON *root = cJSON_Parse(json);
if (root == NULL)
{
const char *error_ptr = cJSON_GetErrorPtr();
printf("Error: %s\n", error_ptr); // Prints Error: key': 'value'}␙�␑
return;
}
char *value = cJSON_GetObjectItemCaseSensitive(root, "key")->valuestring;
printf("JSON KEY: %s\n", value);
}
EDIT: I tried by embedding the JSON inside the code and it works! I think I figured out what the problem is.
void parseJSON(char *json)
{
char *json2 = "{\"key\":\"value\"}";
printf("%s\n", json2); // Prints {"key":"value"}
cJSON *root = cJSON_Parse(json2);
if (root == NULL)
{
const char *error_ptr = cJSON_GetErrorPtr();
printf("Error: %s\n", error_ptr);
return;
}
char *value = cJSON_GetObjectItemCaseSensitive(root, "key")->valuestring;
printf("JSON KEY: %s\n", value); // Prints JSON KEY: value
}
So apparently, the JSON is sent with single quotes {'key': 'value'}
by the MQTT broker, and the parser does not recognize it as a string, because C takes single quotes strings as ints.
from cjson.
So apparently, the JSON is sent with single quotes
{'key': 'value'}
by the MQTT broker, and the parser does not recognize it as a string, because C takes single quotes strings as ints.
Yes, specifically C uses single quotes for characters or extended characters, which internally are stored as int
.
It's odd that the MQTT broker is sending JSON using single quotes around strings. According to the syntax description at JSON.org, the strings are to be delineated with double quotes.
from cjson.
Related Issues (20)
- question on vxworks 6.8 HOT 3
- cJSON_DeleteItemFromArray considers dicts and arrays equally HOT 1
- Add a function cJSON_IsEmpty() HOT 4
- JSON Parse Error HOT 11
- Always provide valuestring
- Double Free Vulnerability Discovered in cJSON_Delete Function Through Fuzzing HOT 6
- Fix cmake export to allow cross-compilation with sysroot HOT 1
- Regression on double precision in print_number() when fix a clang compile warning
- A segmentation fault in cJSON_SetValuestring HOT 2
- Can't Install it on PI HOT 2
- cJSON_CreateStringArray and NULL items HOT 4
- Weird Commits HOT 3
- Question: JSON Minifier
- print_number() non guaranteed to be thread safe with floating point numbers
- sorry,delete it
- misc_tests fails on 1.7.18 under Windows HOT 3
- Memory Leak in the cJSON_Free HOT 2
- Bug in print_string_ptr() increment is 4 instead of 5 HOT 2
- Unable to Specify macOS min version
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cjson.