Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement lightweight object JSON Serialization by C++

Shulou Source: shulou.com Published: 2022-06-03 08:33:16 09月10日 Update

This article is about how C++ implements JSON serialization of lightweight objects. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Design ideas

Taking unmarshal as an example, our final function is to create two such template functions:

One deserializes the object directly from string's josn, and one deserializes the object from the jsoncpp library's json object.

Template bool Unmarshal (T & obj,const string& json_str); template bool Unmarshal (T & obj,const Json::Value& json_obj_root)

Because json has a self-recursive structure, we should also solve complex composite classes recursively at design time. We can simply divide the variables in the program into the following categories:

In this way, we only need to implement the Unmarshal of these scenarios, and the overall Unmarshal will be implemented. The class diagram of the template design should correspond to our classification:

The following points should be noted in the implementation:

The Unmarshal template for each classification is exclusive, that is, the basic type can only match the template that parses the basic type during compilation.

Because the guarantee of 1 and the names of these template functions are Unmarshal, they can nest calls to each other.

Pointers, and native arrays will involve space allocation and length detection, which complicates the situation. The scope of support for pointers and native arrays is not included in this design.

2. Unmarshal templates that match basic types / / can only parse a set of templates of basic type int long bool float double string / * * can actually be declared as overloaded functions, declared as templates only so that they can be placed in the header file * cannot be declared as template bool Unmarshal (int& obj,const Json::Value & root); because * the compiler cannot infer the type of T at compile time, resulting in compilation failure. So setting the list of template types to template (Nontype * Parameters) where int doesn't make sense * / template inline bool Unmarshal (int& obj,const Json::Value & root) {if (! root.isIntegral ()) return false; obj = root.asInt (); return true } template inline bool Unmarshal (long& obj,const Json::Value& root) .3. Unmarshal templates that match stl containers / other third-party class libraries / / can only match a set of templates / / vectortemplate bool Unmarshal (vector& obj,const Json::Value& root) {if (! root.isArray ()) return false; obj.clear () of vector map map map; bool ret = true; for (int iTuno _ I

Tags: Functions objects templates sequences versions types compilers designs parameters methods structures cymbals actions variables errors generation two code criteria compiler Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Linux Xiaomi Redmi Shulou Information