Closed ecorm closed 9 years ago
Unless someone convinces me otherwise, I'm going with the "manual" approach for this feature. I'm either going to use the Cereal library (and implement a custom archive), or will implement something from scratch along the same lines as Cereal.
I have experimented with Protocol Buffers before, and I don't like the IDL approach for the following reasons:
serialize
function for your serializable object.struct
that protobuf provides. You either have to copy values from your domain object to the protobuf struct, or embed the protobuf struct within your domain object. I find that a serialize
function is less intrusive than the protobuf struct.Note that this decision doesn't prevent anyone else from writing an IDL compiler that converts to wamp::Variant
, if that's how they prefer to work. An IDL compiler could even make use of the manual conversion infrastructure.
I've taken a closer look at Cereal, and I won't be using it for the following reasons:
std::map<std::string, T>
into normal JSON objects. It instead serializes it into an array of key-value pairs.That being said, Cereal seems like an excellent library for saving object graphs to files. I especially like its API, and will take inspiration from it when designing my own conversion utilities.
I might have dismissed Cereal too quickly. It turns out that Cereal can do a lot of work for me:
cereal/include/cereal/types/
for handling standard library and Boost types.The points I raised in the above post can be mitigated as follows:
std::stack
object. Since a std::map
has to be constructed anyway for a new wamp::Object
, the extra overhead is not that significant.std::map<std::string, T>
for my DTO archive. I would also specialize std::array<T>
so that it's serialized as a normal array.Another argument for Cereal is that if the user already uses it (say, for file I/O), then they can reuse their same serialization functions for converting their objects to wamp::Object
.
Provide facilities for converting user DTOs (Data Transfer Objects) to/from
Variant
objects.For example, this C++ struct:
would be converted to the following Variant object (shown in JSON representation):
There are two possible approaches:
The "manual" approach makes the user responsible for defining how her objects are serialized. It typically involves providing a
serialize
function that takes an "archive", and then inserting/extracting each object member to/from the archive. This is the approach used by the Boost.Serialization and Cereal libraries.The IDL approach involves defining the schema of an object in an Interface Description Language. A special compiler looks at the IDL definition and generates a struct (or class) that is used to store the data. This generated struct "knows" how to serialize/deserialize itself to/from the archive. This is the approach used by Protocol Buffers.
The advantages of the manual approach are:
Variant
, without involving a generated struct.The advantages of the IDL approach are: