I\'d like to create a JSON string containing the instance variables of my class.
For example,
class Example {
std::string string;
std::ma
Any good C++ JSON library should do this and it is sad to see that they don't -- with the exception of ThorsSerializer and apparently Nosjob as mentioned in this question.
Of course, C++ does not have reflection like Java, so you have to explicitly annotate your types:
(copied from the ThorsSerializer documentation)
#include "ThorSerialize/JsonThor.h"
#include "ThorSerialize/SerUtil.h"
#include
Example Usage:
int main()
{
using ThorsAnvil::Serialize::jsonExport;
using ThorsAnvil::Serialize::jsonImport;
Example e1 {"Some Text", {{"ace", "the best"}, {"king", "second best"}}, {1 ,2 ,3, 4}};
// Simply serialize object to json using a stream.
std::cout << jsonExport(e1) << "\n";
// Deserialize json text from a stream into object.
std::cin >> jsonImport(e1);
}
Running:
{
"string": "Some Text",
"map":
{
"ace": "the best",
"king": "second best"
},
"vector": [ 1, 2, 3, 4]
}
You cannot do better than this in C++.