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

Implement JSON encoding #2

Closed
shreyasbharath opened this issue Jun 23, 2014 · 10 comments
Closed

Implement JSON encoding #2

shreyasbharath opened this issue Jun 23, 2014 · 10 comments
Assignees
Milestone

Comments

@shreyasbharath
Copy link

I have just started using your library (great, easy-to-use library by the way!) and I was wondering if it would be possible to implement JSON encoding as well.

At the moment, we have to hand-code outbound JSON (which is slightly painful and error-prone).

@bblanchon
Copy link
Owner

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?

@shreyasbharath
Copy link
Author

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?

@bblanchon
Copy link
Owner

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

@shreyasbharath
Copy link
Author

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;
}

@bblanchon
Copy link
Owner

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

@bblanchon bblanchon added this to the 2.0 milestone Jun 26, 2014
@bblanchon bblanchon self-assigned this Jun 26, 2014
@bblanchon
Copy link
Owner

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?

@shreyasbharath
Copy link
Author

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.

@shreyasbharath
Copy link
Author

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.

@bblanchon
Copy link
Owner

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.

@bblanchon bblanchon modified the milestone: 2.0 Jul 9, 2014
@shreyasbharath
Copy link
Author

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants