More complete examples for _creating_ JSON elements? #2886
madscientist
started this conversation in
General
Replies: 1 comment 8 replies
-
There is no explicit documentation for how code inside a |
Beta Was this translation helpful? Give feedback.
8 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is there somewhere to go to see more comprehensive documentation on how to use this library to create JSON? For example writing
to_json()
methods for more complex situations?In particular I'm looking for best practice advice and performance pitfalls to avoid when generating
to_json()
: for example how to avoid making multiple copies of data (copying one time is fine with me: I'm not trying to avoid all copies, just the redundant ones).The examples in the documentation show parsing a static string into a JSON object, which is neat, but I suspect most people don't have a string containing all the data to be converted. Instead, they need to build up a complex structure of JSON from another complex structure of C++ content, in a way that avoids recopying things multiple times.
For example I have a to_json() method I'm writing for a data structure and it involves returning an array of objects, where each object contains a mix of simple values as well as more complex values (arrays and objects), etc. At no point do I ever have a full structure (like a vector or a map) that I can just pass to the JSON constructor (at least not without copying all my data into a separate structure which I obviously don't want to do). I need to build up these individual JSON elements and add them into the top-level element incrementally.
Is the best practice to create a stand-alone json object, then add it into a higher level json object via
std::move()
? Something like:Is this enough to prevent a lot of extra copying? Or, are there other ways to do this that are better?
I think the docs spend a lot of time discussing various ways to parse JSON but don't have a lot of examples or things to say about ways to create JSON. But maybe I'm missing it?
Beta Was this translation helpful? Give feedback.
All reactions