A better C ++ serialization/deserialization library Kapok

Source: Internet
Author: User

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.

 
 
  1. // Serialization
  2. Serializer sr;
  3. Auto tp = std: make_tuple (10, 12, string ("test "));
  4. Sr. Serialize (tp, "tuple ");
  5.  
  6. // Deserialization
  7. DeSerializer dr;
  8. Std: tuple <int, int, string> p;
  9. Dr. Parse (sr. GetString ());
  10. Dr. Deserialize (p, "tuple ");

Does it look very simple!

Let's look at another example of serializing a custom object.

 
 
  1. Struct Person
  2. {
  3. Int age;
  4. String name;
  5. String city;
  6. META (age, name, city)
  7. };
  8. Person p = {18, "bb", "aa "};
  9. // Serialization
  10. Serializer sr;
  11. Sr. Serialize (p, "Person ");
  12. // Deserialization
  13. DeSerializer dr;
  14. Person person;
  15. Dr. Parse (sr. GetString ());
  16. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.