Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement lightweight object JSON Serialization by C++

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report