-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Create JsonObject subclass #2056
Comments
Hi @ewoudwijma,
I should probably add Best regards, |
`final` breaks ThingsBoard ಠ_ಠ https://github.com/thingsboard/thingsboard-client-sdk/blob/v0.12.2/src/RPC_Response.h#L10 See #2056. This reverts commit 483a2c9.
`final` breaks ThingsBoard ಠ_ಠ https://github.com/thingsboard/thingsboard-client-sdk/blob/v0.12.2/src/RPC_Response.h#L10 See #2056. This reverts commit 5ed9daa.
I must postpone Nevertheless, I stand with what I said: class VarObject: public JsonObject {
- const char * getID() {return this["id"];}
+ const char * getID() {return (*this)["id"];}
// more functions doing specific things with the object elements
}; |
I have to balance a few arguments in my head: a) I don't want to go against your recommendation. Although except from one compile error it works:
results in:
I think It might be because VarObject is not considered a JsonVariant ... ??? b) I use JsonObjects all over the place and don't want to change the way they are used: the normal ArduinoJson way will change in case of composition in varObject.var["one"] = 2 etc. and I really prefer varObject["one"] = 2. c) if it will be final in 8, I have a problem then d) alternatively I can make 'ugly' global functions like
So I guess I should choose d), unless you give me a hint to resolve a) in case I will doubt between a) and d) ;-) So I don't expect you to answer, just shared my thoughts ;-) Remark: since the creation of this issue(2 days ago) I changed from ArduinoJson v6 to v7 (1 day ago) |
Indeed, the function is
You could override I recommend using custom data types to store the state of the application, as shown in |
You could override operator[] and delegate to the JsonObject, but it might be complicated. Indeed too complicated as this was only one example, I need about all the functions 🙂. So I decided to go for option B, it’s not the most beautiful but the most pragmatic option. I recommend using custom data types to store the state of the application, as shown in JsonConfigFile.ino. You can use converters to ease the conversion between custom types and JsonDocument. See also: Why should I create a separate config object? Yeah, this is I think about the biggest challenge when using ArduinoJson. It is so beautiful but advice is to only use it to transfer non json data structure to json and vice versa. This is the UI and most of the elements you see here can be added, removed or changed depending on what the user does / the values of the model in json format. So maybe C(++) is not the right choice for me but I a have most experience in c++ and previous tests with python were not satisfying and hey, I am still building for esp32 so most code and libraries and performance can be found in c++. So I use ArduinoJson full blown. The app model is present infinitely. And it works! With AJ v6 model size grew slow enough to never hit boundaries and with v7 the heap even gets cleaned up👍 I manipulate AJ data everywhere except in loop (with a few read exceptions I still need to optimize but even that runs blazingly fast). For loop c data structs are used which are filled by AJ. Another advantage of AJ is the support for custom data types. My most used class is called Coord3D (x,y,z) and besides ordinary functions it has operator functions plus implements your converter stuff and it is extremely powerful to use it this way. So yeah, I am a bit depending on AJ and hope it will not disappear from the world 🙂. I am one of the more active developers of WLED, especially the WLED MoonModules fork so learned using AJ there. Currently I am finishing a new application, see above screenshot. Kind regards, |
Hi Ewoud, Like any programming advice, my recommendations are only valid in certain contexts. Therefore, I can easily understand why you'd use Anyway, thank you very much for the kind words about the library. I love reading users' testimonies, but they are rare, unfortunately. ArduinoJson should not disappear anytime soon, but you can ensure the durability of the library by sponsoring the project 😉 Your project looks fantastic. I hope you can show me a demo someday. Best regards, |
Hi Benoit, Oh and I bought your book earlier this week as a way of sponsoring 🙂 |
Description
I want to create a subclass of JsonObject and tried this:
class VarObject: public JsonObject {
const char * getID() {return this[“id”];}
// more functions doing specific things with the object elements
}
But I get compile errors eg converter related.
Probably it’s just a generic c++ question but can this be done, eg using friend, or defining other functions calling superclass functions ?
Troubleshooter's report
Environment
Reproduction code
The text was updated successfully, but these errors were encountered: