Comments (10)
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.
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.
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.
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.
Sure, that sounds great.
Can you fill a new request for that?
from arduinojson.
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.
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.
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.
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.
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)
- Adding entry in JsonObject using File object causes corrupted JSON HOT 1
- Initial serializing HOT 7
- `doc[var]` doesn't work when `var` contains a string
- JsonArray.add(JsonObject) adds partial JsonObject when the JsonDocument is full HOT 6
- ArduinoJson.h: No such file or directory HOT 3
- JsonVariant tests false for both a value that doesn't exist and for a value that is zero HOT 7
- How do i create JSON array of objects using ArduinoJson library. HOT 1
- Identifying JsonVariant type of a deserialized document HOT 4
- Example code results in compiler error: no match for 'operator+=' HOT 3
- Passing JsonDocument by reference HOT 4
- In Memory Deserialization HOT 4
- v7 equivalent of validateJson HOT 2
- conversion from ‘ArduinoJson::V704HB42::detail::enable_if<true, ArduinoJson::V704HB42::JsonVariantConst>::type’ {aka ‘ArduinoJson::V704HB42::JsonVariantConst’} to ‘ArduinoJson::V704HB42::JsonDocument’ is ambiguous HOT 1
- Assertion `poolIndex < count_' failed HOT 1
- Maximum string length of 13 characters HOT 2
- V6 Documentation ArduinoJson PubSubClient with StreamUtils HOT 1
- deserialize file from LittleFS crash HOT 6
- Trying to compile a 2021 program HOT 2
- Bring back zero-copy mode in v7 HOT 6
- MQTT Publish destroying Json HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arduinojson.