0

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?

User12547645
  • 6,955
  • 3
  • 38
  • 69
  • You can always use [this method](https://github.com/nlohmann/json#arbitrary-types-conversions) to convert any struct. How you chose to deal with the variants and pointers though is up to you. If you want to deserialize several `std::shared_ptr`s and have them refer to the same object you probably need to manually handle that in some manner. Also raw pointers. Do you want to serialize the value pointed to, or do you want to rebuild objects that are referef to by several pointers? – super Oct 14 '20 at 10:14
  • Thank you for the reply @super. The tree should have the same values. It does not need to refer to the same points in memory. – User12547645 Oct 14 '20 at 12:08

0 Answers0