Converting any struct to json automatic in C++

Viewed 1308

I am willing to convert any struct to json automatic without repeating code or variables in C++.

Desired result:

typedef JsonStruct<string, name, int, age> Person;  // not have to be template...

Person person;
person.name = "Jacob";
person.age = 16;

json personAsJson = person.toJson();
Person newPerson = Person::fromJson(personAsJson);
assert(newPerson == person);

I found this answer: https://stackoverflow.com/a/34165367/13277578

Which is really cool! but you still have to repeat the variable declaration... (struct fields & properties)

I tried so many things for so many times, but I still not even close to the desired result.

disclaimer: I am using Niels Lohmann JSON

Is it even possible to preprocess the fildes (or the properties) in the answer code to prevent repeating yourself in C++? Thanks!

0 Answers
Related