Code Monkey home page Code Monkey logo

Comments (4)

highfield avatar highfield commented on May 11, 2024

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.

from arduinojson.

bblanchon avatar bblanchon commented on May 11, 2024

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.

from arduinojson.

highfield avatar highfield commented on May 11, 2024

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/

from arduinojson.

bblanchon avatar bblanchon commented on May 11, 2024

Hey hey.
Congratulations for your project 👍

from arduinojson.

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.