Giter Club home page Giter Club logo

Comments (5)

mbratch avatar mbratch commented on June 13, 2024

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.

Gtadictos21 avatar Gtadictos21 commented on June 13, 2024

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.

mbratch avatar mbratch commented on June 13, 2024

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.

Gtadictos21 avatar Gtadictos21 commented on June 13, 2024

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.

mbratch avatar mbratch commented on June 13, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.