JSON: Javascript 对象表示法( Javascript Object Notation) 。是一种轻量级的数据交换格式。 它基于ECMAscript的一个子集。许多编程语言都很容易找到JSON 解析器和 JSON 库。 JSON 文本格式在语法上与创建 Javascript 对象的代码相同。不同语言的不同json库对json标准的支持不尽相同,为了能让尽可能多的json库都能正常解析和生成json,定义JSON的规范很重要,推荐一个JSON规范《JSON风格指南》。
2. 常用C&C++ JSON库
常用且知名度较高的C&C++的JSON库有cJSON、json-c、JsonCpp等,腾讯员工开源的一个RapidJSON以高性能著称。C&C++的JSON库比较见RapidJSON作者的比较nativejson-benchmark。
3. 非常简单易用的CJsonObject我一直使用的json库是一个较老版本的cJSON,cJSON的好处是简单易用,而且只有两个文件,直接复制到自己的代码中就可以用。cJSON也有一个非常容易让初用者头痛的地方,一不小心就造成内存泄漏了。为此,我基于cJSON封装了一个C++版的CJsonObject,该库比cJSON更简单易用,且只要不是有意不释放内存就不会发生内存泄漏。用CJsonObject的好处在于完全不用文档,看完Demo马上就会用,不明白的看一下头文件就知道,所有函数都十分通俗易懂,最为关键的一点是解析JSON和生成JSON的编码效率非常高。当然,毕竟是经过cJSON封装而来,效率会略低于cJSON,cJSON不支持的CJsonObject也不支持。个人认为,既然已经选了json,那一点点的解析性能差异就不重要了,如果追求性能可以选protobuf。CJsonObject在我最近4年做过的8个项目中广泛应用,目前有超过20人在使用,至于是否有离职同事带到其他团队去了就不太清楚。CJsonObject非常简单易用,且表现稳定,2018年5月我把它开源https://github.com/Bwar/CJsonObject,并将持续维护。
好了,来看看CJsonObject是如何简单易用:
demo.cpp:
#include#include #include "../CJsonObject.hpp"int main() { int iValue; std::string strValue; neb::CJsonObject oJson("{"refresh_interval":60," ""dynamic_loading":[" "{" ""so_path":"plugins/User.so", "load":false, "version":1," ""cmd":[" "{"cmd":2001, "class":"neb::CmdUserLogin"}," "{"cmd":2003, "class":"neb::CmdUserLogout"}" "]," ""module":[" "{"path":"im/user/login", "class":"neb::ModuleLogin"}," "{"path":"im/user/logout", "class":"neb::ModuleLogout"}" "]" "}," "{" ""so_path":"plugins/ChatMsg.so", "load":false, "version":1," ""cmd":[" "{"cmd":2001, "class":"neb::CmdChat"}" "]," ""module":[]" "}" "]" "}"); std::cout << oJson.ToString() << std::endl; std::cout << "-------------------------------------------------------------------" << std::endl; std::cout << oJson["dynamic_loading"][0]["cmd"][1]("class") << std::endl; oJson["dynamic_loading"][0]["cmd"][0].Get("cmd", iValue); std::cout << "iValue = " << iValue << std::endl; oJson["dynamic_loading"][0]["module"][0].Get("path", strValue); std::cout << "strValue = " << strValue << std::endl; std::cout << "-------------------------------------------------------------------" << std::endl; oJson.AddEmptySubObject("depend"); oJson["depend"].Add("nebula", "https://github.com/Bwar/Nebula"); oJson["depend"].AddEmptySubArray("bootstrap"); oJson["depend"]["bootstrap"].Add("BEACON"); oJson["depend"]["bootstrap"].Add("LOGIC"); oJson["depend"]["bootstrap"].Add("LOGGER"); oJson["depend"]["bootstrap"].Add("INTERFACE"); oJson["depend"]["bootstrap"].Add("ACCESS"); std::cout << oJson.ToString() << std::endl; std::cout << "-------------------------------------------------------------------" << std::endl; std::cout << oJson.ToFormattedString() << std::endl; }
Demo执行结果:
?
| [bwar@nebula demo]$ ./CJsonObjectTest {"refresh_interval":60,"dynamic_loading":[{"so_path":"plugins/User.so","load":false,"version":1,"cmd":[{"cmd":2001,"class":"neb::CmdUserLogin"},{"cmd":2003,"class":"neb::CmdUserLogout"}],"module":[{"path":"im/user/login","class":"neb::ModuleLogin"},{"path":"im/user/logout","class":"neb::ModuleLogout"}]},{"so_path":"plugins/ChatMsg.so","load":false,"version":1,"cmd":[{"cmd":2001,"class":"neb::CmdChat"}],"module":[]}]}-------------------------------------------------------------------neb::CmdUserLogoutiValue = 2001strValue = im/user/login-------------------------------------------------------------------{"refresh_interval":60,"dynamic_loading":[{"so_path":"plugins/User.so","load":false,"version":1,"cmd":[{"cmd":2001,"class":"neb::CmdUserLogin"},{"cmd":2003,"class":"neb::CmdUserLogout"}],"module":[{"path":"im/user/login","class":"neb::ModuleLogin"},{"path":"im/user/logout","class":"neb::ModuleLogout"}]},{"so_path":"plugins/ChatMsg.so","load":false,"version":1,"cmd":[{"cmd":2001,"class":"neb::CmdChat"}],"module":[]}],"depend":{"nebula":"https://github.com/Bwar/Nebula","bootstrap":["BEACON","LOGIC","LOGGER","INTERFACE","ACCESS"]}}-------------------------------------------------------------------{ "refresh_interval": 60, "dynamic_loading": [{ "so_path": "plugins/User.so", "load": false, "version": 1, "cmd": [{ "cmd": 2001, "class": "neb::CmdUserLogin" }, { "cmd": 2003, "class": "neb::CmdUserLogout" }], "module": [{ "path": "im/user/login", "class": "neb::ModuleLogin" }, { "path": "im/user/logout", "class": "neb::ModuleLogout" }] }, { "so_path": "plugins/ChatMsg.so", "load": false, "version": 1, "cmd": [{ "cmd": 2001, "class": "neb::CmdChat" }], "module": [] }], "depend": { "nebula": "https://github.com/Bwar/Nebula", "bootstrap": ["BEACON", "LOGIC", "LOGGER", "INTERFACE", "ACCESS"] }} |
再来看看头文件,一看就知道如何使用:
#ifndef CJSONOBJECT_HPP_#define CJSONOBJECT_HPP_#include#include #include #include #include #include #include #include #include #include
作者:Bwar 出处:https://www.cnblogs.com/bwar/
Bwar倾力打造的高性能网络框架Nebula:https://github.com/Bwar/Nebula



