Arduinojson: Suggestion for an example.

Created on 2 Sep 2017  路  5Comments  路  Source: bblanchon/ArduinoJson

I know that you dislike global jsonbuffer/object but i think that many ppl just want to have set of seettings available with boot time and most of them should be unchanged as long as device is on.

IMHO that would be often used pattern.

question

All 5 comments

In that situation, I use custom data structures as suggested here in FAQ: What's the best way to use the library?

This is a win in three levels:

  1. Faster code (the object tree is walked only once at boot time)
  2. Smaller RAM footprint (JsonObject, JsonVariant... have significant overhead)
  3. Smaller code

Reducing the size of global variables is crucial as they reduce the RAM remaining for the rest.
Moreover, the JSON object tree consumes memory even if the program doesn't use it.
For example, if someone adds values in the JSON settings that are not actually used by the program, the memory will be used anyway.
One could even get to a situation where the program is unable to run because all the memory will be consumed by the settings.

So I see this more as an anti-pattern, but I'd like to get you opinion @nardev...
What is the benefit compare to custom data structures?

PS: I forgot another strong advantage of custom structures:

  1. It decouples the memory representation from the file format,
    which will be very handy when the file format evolves

Didn't notice this one in the F.A.Q. thnx.

p.s. :D
in my case the web HTML/JS gives me json on it's own way, which is than easy to parse and populate HTML form again in case of edit. So i didn't want to touch that.

On the other hand, since it's a list of mqtt topics with device and codes data (for ir/rc transmitters), i just made another json object with full topics path like:

{
聽聽"my_device_name/rc-switch/socket_main_light0":聽{
聽聽聽聽"code":聽"654321",
聽聽聽聽"type":聽"rc-switch"
聽聽},
聽聽"my_device_name/relay-pin/socket_main_light2":聽{
聽聽聽聽"code":聽"13",
聽聽聽聽"type":聽"relay-pin"
聽聽},
聽聽"my_device_name/ir-switch/socket_main_light":聽{
聽聽聽聽"code":聽"{get_from_config_file_no_31}",
聽聽聽聽"type":聽"ir-switch"
聽聽}
}

So, since it's safe to read this object always, i just check if the topics exists, and if it does, just do what ever i need to do...

Should we close this..??

This is a question that pops up very often, let's keep this open for a while and see if we get some reactions.

Then, I'll add my answer to the FAQ, where it belongs, and close this issue.

Thank you very much for asking this @nardev, I'm sure this will help a lot of users.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kylegordon picture kylegordon  路  6Comments

renno-bih picture renno-bih  路  5Comments

zixzixzix picture zixzixzix  路  5Comments

loewekordel picture loewekordel  路  4Comments

ghost picture ghost  路  7Comments