-
If I have many derived classes which inherits the same parent class, how to simplify tasks of write to_json code for parent class? class Base {};
void to_json(json& j, const Base& obj);
class Derived1 : public Base {}
void to_json(json& j, const Derived1& obj);
class Derived2 : public Base {}
void to_json(json& j, const Derived2& obj); |
Beta Was this translation helpful? Give feedback.
Answered by
nlohmann
Jun 27, 2020
Replies: 2 comments
-
If some of the classes share code, you can call nlohmann::to_json(j, static_cast<Base&>(obj)); Further note there is an A related issue: #2199 |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
gzliudan
-
Thank you very much! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If some of the classes share code, you can call
to_json
of the base class explicitly, e.g. byFurther note there is an
update
function to merge/update objects.A related issue: #2199