I'm using cloud-to-device ledger to configure my particle boards.
Is there an easy way for me to convert variants (Ledger data) to custom config objects? I would like to pass the config between classes. (I'm also very new to variants.)
Is there a library that I can use or do I have to manually convert them?
Thanks!
I would just leave it as a Variant. There are methods for reading and writing values, objects, and arrays in a Variant.
When used for configuration, Variant is basically JSON, and can be converted to and from that format. But there isn't really an advantage of converting to JSON, then parsing the JSON, because the Variant is already parsed.
See Variant in typed publish.
Thank you for the quick response!
I was however thinking along the lines of converting it to like a strict object instead of a JSON and accessing it through member variables.
For example, I find
_intervalTime = config.intervalTime;
easier than
Variant variant = ledgerData.get("intervalTime");
_intervalTime = variant.toInt();
Would you still suggest keeping it as a variant?
If you wanted to, you could copy each value out using the accessors and store them in a struct. This is simple enough if there are only primitive types, but gets far more complicated when you have nested objects and arrays.
What I recommend doing is keeping it as a Variant and saving the ledgerData as a global variable. Then you just use ledgerData.get("intervalTime").toInt()
. There's an example in ledger configuration.
But storing it in a struct does work if you prefer to do it that way.
1 Like