Skip to content

[Request] Macro generating from_json() and to_json() #895

@unrealmistake

Description

@unrealmistake

Making to_json() and from_json() for all classes in my object model is a chore.
I want to create a macro that would generate bodies for these functions. I saw some clues how to make it, but my knowledge of C++ is not good enough to implement it.
I want to be able to write something like this:

class MyClass
{
...
    JsonVar(int, intvar)
    JsonVar(string, stingvar)
...
}
void to_json(json& j, const MyClass& p) {
JsonTo(MyClass)
... //some extra serialization
}
void from_json(const json& j, MyClass& p) {
JsonFrom(MyClass)
... //some extra deserialization
}

and get

class MyClass
{
...
    int intvar;
    string stringvar;
...
}
void to_json(json& j, const MyClass& p) {
j = json{ { "intvar", p.intvar},
			{ "stringvar", p.stringvar} };
... //some extra serialization
}
void from_json(const json& j, MyClass& p) {
p.intvar= j.at("intvar").get<int>();
p.stringvar= j.at("stringvar").get<string>();
... //some extra deserialization
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    state: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updated

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions