A better C ++ serialization/deserialization library Kapok
1. Features of Kapok
Simple, easy to use, header-only, only need to reference Kapok. hpp; efficient, preliminary testing is equivalent to messagepack.
It is implemented in pure c ++ 11, so it must support the C ++ 11 compiler.
2. Main Functions
Automatic serialization and deserialization of objects are very easy to use. Let's look at a serialization/deserialization example of a tuple.
- // Serialization
- Serializer sr;
- Auto tp = std: make_tuple (10, 12, string ("test "));
- Sr. Serialize (tp, "tuple ");
-
- // Deserialization
- DeSerializer dr;
- Std: tuple <int, int, string> p;
- Dr. Parse (sr. GetString ());
- Dr. Deserialize (p, "tuple ");
Does it look very simple!
Let's look at another example of serializing a custom object.
- Struct Person
- {
- Int age;
- String name;
- String city;
- META (age, name, city)
- };
- Person p = {18, "bb", "aa "};
- // Serialization
- Serializer sr;
- Sr. Serialize (p, "Person ");
- // Deserialization
- DeSerializer dr;
- Person person;
- Dr. Parse (sr. GetString ());
- Dr. Deserialize (person, "Person ");
The same is very simple. Here we need a macro-defined META, which is used to obtain the object metadata. With this META information, we can easily implement serialization and deserialization.
3. Application scenarios
In addition to not supporting pointers, Kapok supports all objects and supports the infinite nesting of structs. nested structs must also define META macros ). Here we will explain why pointer is not supported, because there are two problems in the object: 1. if the pointer is a dynamic array, the length of the array cannot be obtained in c ++; 2. pointers also involve memory management. I want Kapok To Focus On serialization and/or deserialization, but not on memory management for the moment.
4. the struct must have a macro definition. Is it invasive?
It seems that every serialized/deserialized object must contain a macro definition, which seems to be more invasive, but this type of intrusion is completely harmless because it only defines an additional function, this function will only be used during serialization/deserialization and will not affect the current object. Another reason is that c ++ has no reflection, some methods must be used to obtain the object metadata. All current serialization schemes are the most concise and users can do the least, this is why I chose this method.
5. How does Kapok implement serialization/deserialization?
The bottom layer of Kapok is rapidjson, which is used to serialize basic types, and a simple encapsulation of it for upper-layer use, the above layer is the serialization/deserialization implementation layer, which is mainly used to parse the metadata and automatically package and package. Below is a serialization of Kapok:
6. What is the performance of Kapok?
A preliminary test was conducted to serialize/deserialize a tuple for 10 thousand times. It was found that the time consumed by Kapok was the same as that of messagepack.
7. Does Kapok support multiple languages?
Currently, c ++ is not supported. If you want to support multiple languages, you need to rewrite them in other languages.