Replies: 1 comment
-
The canonic approach that this library offers is to define a The effect that you describe can be explained by the fact that you serialize JSON values to a string, but then store these values as strings in another JSON value - and quotes are then escaped in the second serialization step. You could "fix" this by deserializing the value again: std::string serialize() {
nlohmann::json j;
for (const auto& s : _structs) {
j[s.name] = json::parse(s.serialize()); // added parse call here
}
return j.dump(4);
} |
Beta Was this translation helpful? Give feedback.
-
Thanks for the awesome library, its highly appreciated. I've read through the examples but could not find exactly what I need.
I'd like to have structs with a pure stl interface, so methods
serialize
anddeserialize
should not expose the classnlohmann::json
. The structs will be stored in a nested data structure (no loops). Is it possible to serialize this recursively into a std::string, with a pure stl interface? I do not necessarily need the interface to returnstd::string
, any pure stl interface (for example streams, or return by argument) is fine for me.I've tried with something like
but when I use this, the json in the array values seems fully escaped, not nicely formatted. Here is an example how the nested structures will get formatted with this approach:
I have tested that this would work much better recursive if
serialize()
returns an object of typenlohmann::json
, so I guess that is the standard approach? But is it also possible to hide the json library from the interface?Beta Was this translation helpful? Give feedback.
All reactions