GithubHelp home page GithubHelp logo

Comments (10)

bblanchon avatar bblanchon commented on June 24, 2024

I don't think it's possible to make a JSON generator in this library.

If you look at the declarations of JsonArray and JsonHashTable, you'll see that they are really designed to leverage the use of the jsmn library. It would make no sense to change these classes to add JSON generation. In other words, it doesn’t seem to be a good idea to reuse JsonArray and JsonHashTable for a JSON encoder.

However, I think it’s possible to write another library that would have the same spirit as this one: constant memory allocation and minimal footprint.

I gave a lot of thoughts to this idea, and I came with the following API:

#include <JsonGenerator.h>

using namespace ArduinoJson::Generator;

JsonArray<2> arr;
arr.add(1);
arr.add("Hi!");

JsonHashTable<3> hash;
hash.add("key1", 1);
hash.add("key2", "Hello!");
hash.add("key3", arr);

char buffer[256];
hash.writeTo(buffer, sizeof(buffer));

As you see, you'd need to specify the maximum size of JsonArray and JsonHashTable with the template parameter; kind of what we already do with JsonParser.

In my projects, I've always use sprintf to generate the JSON strings, but it’s true that it not possible if the structure is dynamic or if there are nested elements.
So I think I could be a user of a library like that.

What do you think? Do you think you’d use such a library in you project?

from arduinojson.

shreyasbharath avatar shreyasbharath commented on June 24, 2024

That sounds great!

Yes we would definitely use it in our project. All we are after is simplicity in terms of the API. Yours was by far one of the easiest to use when we were evaluating all our JSON library options.

Would it be hard to implement?

from arduinojson.

bblanchon avatar bblanchon commented on June 24, 2024

It doesn't seem too hard.
I'm just worried about the corner cases, like quotes in strings.
Anyway, I started to write it, we'll see were it goes.

You can see the progress in the "add-generator" branch:
https://github.com/bblanchon/ArduinoJsonParser/tree/add-generator

from arduinojson.

shreyasbharath avatar shreyasbharath commented on June 24, 2024

Great, I'll keep an eye on it.

One other thing that would be nice to implement is some sort of iterator-like interface on the JsonHashTable class. At the moment, you have to manually go over each key, which is a bit painful.

So we could do something like the std::map class of C++.

for ( auto i = jsonHashTable.begin(), i != jsonHashTable.end(); i++ ) {
cout << "Key " << i->first;
cout << "Value" << i->second;
}

from arduinojson.

bblanchon avatar bblanchon commented on June 24, 2024

Sure, that sounds great.
Can you fill a new request for that?

from arduinojson.

bblanchon avatar bblanchon commented on June 24, 2024

The work on the JSON encoding is almost completed.
https://github.com/bblanchon/ArduinoJsonParser/tree/add-generator

  • the library is ready
  • the tests are all green
  • the example has been tested
  • I still have to update the documentation.

Can you please try it and give me your comments?

from arduinojson.

shreyasbharath avatar shreyasbharath commented on June 24, 2024

Perfect timing! I should be able to give this a try early next week or maybe even today! Thanks for your work.

I'll let you know how it goes.

from arduinojson.

shreyasbharath avatar shreyasbharath commented on June 24, 2024

I have raised another ticket for implementing the iterator, that would be icing on the cake for this library.

At the moment you have to know the names of the keys inside a hashtable, but with the iterator you don't have to care.

from arduinojson.

bblanchon avatar bblanchon commented on June 24, 2024

I managed to reduce the size of the code and the memory usage on this branch:
https://github.com/bblanchon/ArduinoJsonParser/tree/add-generator-3
Please give it a try.

BTW, do you have any feedback right now?
I'm eager to release a public v2.0.

from arduinojson.

shreyasbharath avatar shreyasbharath commented on June 24, 2024

Hi again, sorry about the late reply! Been a tad busy at work, so haven't had a chance to test it out yet. I plan to early next week, I'll post my feedback then.

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.