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

The serialization results incorrect #41

Closed
highfield opened this issue Dec 28, 2014 · 4 comments
Closed

The serialization results incorrect #41

highfield opened this issue Dec 28, 2014 · 4 comments
Labels
v5 ArduinoJson 5

Comments

@highfield
Copy link

I experience a wrong serialization by using such a snippet:

//
// Step 1: Reserve memory space
//
StaticJsonBuffer<200> jsonBuffer;

//
// Step 2: Build object tree in memory
//
JsonObject& root = jsonBuffer.createObject();
root["sensor"] = "gps";
root["time"] = 1351824120;

JsonArray& data = root.createNestedArray("data");
//data.add(48.756080, 6);  // 6 is the number of decimals to print
//data.add(2.302038, 6);   // if not specified, 2 digits are printed
{
  JsonObject& jsens = data.createNestedObject();
  jsens["name"] = "tizio";
  jsens["value"] = true;
}
{
  JsonObject& jsens = data.createNestedObject();
  jsens["name"] = "caio";
  jsens["value"] = 3.141519;
}

//
// Step 3: Generate the JSON string
//
root.printTo(Serial);

The resulting JSON is the following:

{"sensor":"gps","time":1351824120,"data":[{"name":"tizio"}]}

Is it me?

@highfield
Copy link
Author

UPDATE.
I figured out the reason, and it seems on the (huge) memory usage. I'm using a 32-bit board and the buffer must be sized way longer than expected.
By running this snippet (notice the buffer size!)...

//
// Step 1: Reserve memory space
//
StaticJsonBuffer<1024> jsonBuffer;

//
// Step 2: Build object tree in memory
//
JsonObject& root = jsonBuffer.createObject();
root["sensor"] = "gps";
root["time"] = 1351824120;

JsonArray& data = root.createNestedArray("data");
//data.add(48.756080, 6);  // 6 is the number of decimals to print
//data.add(2.302038, 6);   // if not specified, 2 digits are printed

for (int i=0; i<100; i++)
{
  JsonObject& jsens = data.createNestedObject();
  jsens.add("name", "tizio");
  jsens.add("value", i);
}

//
// Step 3: Generate the JSON string
//
root.printTo(Serial);

...the result is the following (290 chars):

{"sensor":"gps","time":1351824120,"data":[{"name":"tizio","value":0},{"name":"tizio","value":1},{"name":"tizio","value":2},{"name":"tizio","value":3},{"name":"tizio","value":4},{"name":"tizio","value":5},{"name":"tizio","value":6},{"name":"tizio","value":7},{"name":"tizio","value":8},{}]}

I simply underestimated the actual usage of ram.

@bblanchon
Copy link
Owner

Yes, memory usage is almost 4 times bigger in 32-bit than in 8-bit.
To write platform independent code, I recommend using the following macros:

  • JSON_ARRAY_SIZE(N) which returns the size of an array of N elements
  • JSON_OBJECT_SIZE(N) which returns the size of an object of N key-value pairs

In your second last example, it could be:

JSON_OBJECT_SIZE(3)+JSON_ARRAY_SIZE(100)+100*JSON_OBJECT_SIZE(2)

Also, if you're not working in an embedded environment, you can switch to DynamicJsonBuffer which will automatically grow.

@highfield
Copy link
Author

I'll bear in mind, thank you very much for the help.

Oh, you were mentioned:
https://highfieldtales.wordpress.com/2014/12/29/azure-veneziano-linkit-one/

@bblanchon
Copy link
Owner

Hey hey.
Congratulations for your project 👍

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
v5 ArduinoJson 5
Projects
None yet
Development

No branches or pull requests

2 participants