Is it possible to serialize nested/complex classes with nlohmann/json?

1.6k Views Asked by At

I would like to use nlohmann/json to serialize a tree, but I am unsure if nlohmann/json provides the necessary features.

Features about which I am unsure:

  • Serialization of nested types. I am serializing a tree after all...

  • Serialization of pointers to abstract types. Figure a class hierarchy. A node has one or more pointers to child nodes. These pointers are of type AbstractClass*. The serializer would need to convert AbstractClass* to a concrete class pointer.

  • Serialization of std::variant

  • Serialization of "custom strings", e.g. strings which use a polymorphic allocator. My assumption is that this should not be an issue.

  • Smart pointers.

A class could look like this:

class some_class {
std::vector<std::shared_ptr<const AbstractClass>> child_nodes;
std::variant<int, double, AbstractClass> some_variant;
// more member variables

public:
   some_class() = delete;
   // non-default constructors here
};

The documentation provides an example for the serialization of a custom data source. Would such an interface be sufficient for my use-case? Does anyone have experience with the above scenario?

0

There are 0 best solutions below