Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with JSON_OBJECT_SIZE on ESP8266 #151

Closed
marvinroger opened this issue Nov 6, 2015 · 3 comments
Closed

Issue with JSON_OBJECT_SIZE on ESP8266 #151

marvinroger opened this issue Nov 6, 2015 · 3 comments
Labels
question v5 ArduinoJson 5

Comments

@marvinroger
Copy link
Contributor

marvinroger commented Nov 6, 2015

Here is a problematic code:

StaticJsonBuffer<JSON_OBJECT_SIZE(2)> json_buffer;
String json = String("{\"test1\":\"test\",\"test2\":\"test\"}");

// 1st attempt
JsonObject& parsed_json = json_buffer.parseObject(json); // parsed_json.success() -> false

// 2nd attempt
JsonObject& parsed_json = json_buffer.parseObject(json.c_str()); // parsed_json.success() -> false

// 3rd attempt
JsonObject& parsed_json = json_buffer.parseObject((char*)json.c_str()); // parsed_json.success() -> true

As explained here:

parseObject() takes a char* as a parameter. Be careful, it's not a const char*, the memory must be writable.

But as stated here, the function can also take a String or const char* but will make a copy of this string.

So all 3 should work, I guess... What's wrong?

@bblanchon
Copy link
Owner

There is not enough room in json_buffer to store the copy of the string.
Therefore, only the overload that makes no copy can work.

To make them work, you need to increase the size of the StatisJsonBuffer of the size of the string (plus some bytes for the alignment and the terminator).

#define MAX_INPUT_LENGTH 40
StaticJsonBuffer<JSON_OBJECT_SIZE(2)+MAX_INPUT_LENGTH> json_buffer;

@marvinroger
Copy link
Contributor Author

Oh, okay thanks for the information. This would deserve to be in the Wiki, don't you think?
Feel free to close.

@bblanchon
Copy link
Owner

I started an API reference section, this will eventually go there.

Repository owner locked and limited conversation to collaborators Sep 21, 2018
@bblanchon bblanchon added the v5 ArduinoJson 5 label Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question v5 ArduinoJson 5
Projects
None yet
Development

No branches or pull requests

2 participants