You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I need to serialize a vector composed by derived classes. I have a Container class which holds two variables, one of them being a std::vector<Base>. When dumping the contents of the vector, only the members of the Base class are detected, and the members of the Derived classes are omitted.
I did the changes , but the issue persists. It seems like the to_json() function in the derived class is never called.
That's correct, you have shared_ptr<Base>, so that's what's called. You have to do all the work there to handle the derived class, such as calling a virtual function in Base. That includes doing something like writing out a type identifier string or enum value so you know what type to instantiate in from_json<Base>.
I believe that there's an example of this in the docs.
Description
I need to serialize a vector composed by derived classes. I have a
Container
class which holds two variables, one of them being astd::vector<Base>
. When dumping the contents of the vector, only the members of theBase
class are detected, and the members of the Derived classes are omitted.Reproduction steps
Just execute the example provided.
https://godbolt.org/z/KbxKs688Y
Expected vs. actual results
Actual Result
{ "Container": { "a": 2, "data": [ { "b": 3 }, { "b": 5 } ] } }
Expected Result
{ "Container": { "a": 2, "data": [ { "b": 3, "d": 4 }, { "b": 5, "d": 6 } ] } }
Minimal code example
Compiler and operating system
Arch Linux, GCC 14.2.1 and CLANG 19.1.0
Library version
b36f4c4
Validation
develop
branch is used.The text was updated successfully, but these errors were encountered: