C++JSON库CJsonObject详解(轻量简单好用)
- 作者: 过_过
- 来源: 51数据库
- 2021-06-24
1. json概述
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、、jsoncpp等,腾讯员工开源的一个rapidjson以高性能著称。c&c++的json库比较见rapidjson作者的比较。
3. 非常简单易用的cjsonobject
cjsonobject是基于cjson全新开发一个c++版的json库,cjsonobject的最大优势是轻量(只有4个文件,拷贝到自己代码里即可,无须编译成库,且跨平台和编译器)、简单好用,开发效率极高,对多层嵌套json的读取和生成使用非常简单(大部分json解析库如果要访问多层嵌套json的最里层非常麻烦)。我一直使用的json库是一个较老版本的cjson,cjson的好处是简单易用,而且只有两个文件,直接复制到自己的代码中就可以用。cjson也有一个非常容易让初用者头痛的地方,一不小心就造成内存泄漏了。为此,我基于cjson封装了一个c++版的cjsonobject,该库比cjson更简单易用,且只要不是有意不释放内存就不会发生内存泄漏。用cjsonobject的好处在于完全不用文档,看完demo马上就会用,不明白的看一下头文件就知道,所有函数都十分通俗易懂,最为关键的一点是解析json和生成json的编码效率非常高。当然,毕竟是经过cjson封装而来,效率会略低于cjson,cjson不支持的cjsonobject也不支持。个人认为,既然已经选了json,那一点点的解析性能差异就不重要了,如果追求性能可以选protobuf。cjsonobject在我最近5年做过的8个项目中广泛应用。cjsonobject非常简单易用,且表现稳定,2018年5月我把它开源https://github.com/bwar/cjsonobject,并将持续维护。
来看看cjsonobject是如何简单易用:
demo.cpp:
#include <string>
#include <iostream>
#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", "http://www.51sjk.com/Upload/Articles/1/0/245/245776_20210618000515689.jpg");
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::cmduserlogout
ivalue = 2001
strvalue = 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":"http://www.51sjk.com/Upload/Articles/1/0/245/245776_20210618000515689.jpg","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": "http://www.51sjk.com/Upload/Articles/1/0/245/245776_20210618000515689.jpg",
"bootstrap": ["beacon", "logic", "logger", "interface", "access"]
}
}
再来看看头文件,一看就知道如何使用:
/*******************************************************************************
* project: neb
* @file cjsonobject.hpp
* @brief json
* @author bwarliao
* @date: 2014-7-16
* @note
* modify history:
******************************************************************************/
#ifndef cjsonobject_hpp_
#define cjsonobject_hpp_
#include <stdio.h>
#include <stddef.h>
#include <malloc.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>
#include <math.h>
#include <float.h>
#include <string>
#include <map>
#include "cjson.h"
namespace neb
{
class cjsonobject
{
public: // method of ordinary json object or json array
cjsonobject();
cjsonobject(const std::string& strjson);
cjsonobject(const cjsonobject* pjsonobject);
cjsonobject(const cjsonobject& ojsonobject);
virtual ~cjsonobject();
cjsonobject& operator=(const cjsonobject& ojsonobject);
bool operator==(const cjsonobject& ojsonobject) const;
bool parse(const std::string& strjson);
void clear();
bool isempty() const;
bool isarray() const;
std::string tostring() const;
std::string toformattedstring() const;
const std::string& geterrmsg() const
{
return(m_strerrmsg);
}
public: // method of ordinary json object
bool addemptysubobject(const std::string& strkey);
bool addemptysubarray(const std::string& strkey);
cjsonobject& operator[](const std::string& strkey);
std::string operator()(const std::string& strkey) const;
bool get(const std::string& strkey, cjsonobject& ojsonobject) const;
bool get(const std::string& strkey, std::string& strvalue) const;
bool get(const std::string& strkey, int32& ivalue) const;
bool get(const std::string& strkey, uint32& uivalue) const;
bool get(const std::string& strkey, int64& llvalue) const;
bool get(const std::string& strkey, uint64& ullvalue) const;
bool get(const std::string& strkey, bool& bvalue) const;
bool get(const std::string& strkey, float& fvalue) const;
bool get(const std::string& strkey, double& dvalue) const;
bool add(const std::string& strkey, const cjsonobject& ojsonobject);
bool add(const std::string& strkey, const std::string& strvalue);
bool add(const std::string& strkey, int32 ivalue);
bool add(const std::string& strkey, uint32 uivalue);
bool add(const std::string& strkey, int64 llvalue);
bool add(const std::string& strkey, uint64 ullvalue);
bool add(const std::string& strkey, bool bvalue, bool bvalueagain);
bool add(const std::string& strkey, float fvalue);
bool add(const std::string& strkey, double dvalue);
bool delete(const std::string& strkey);
bool replace(const std::string& strkey, const cjsonobject& ojsonobject);
bool replace(const std::string& strkey, const std::string& strvalue);
bool replace(const std::string& strkey, int32 ivalue);
bool replace(const std::string& strkey, uint32 uivalue);
bool replace(const std::string& strkey, int64 llvalue);
bool replace(const std::string& strkey, uint64 ullvalue);
bool replace(const std::string& strkey, bool bvalue, bool bvalueagain);
bool replace(const std::string& strkey, float fvalue);
bool replace(const std::string& strkey, double dvalue);
public: // method of json array
int getarraysize();
cjsonobject& operator[](unsigned int uiwhich);
std::string operator()(unsigned int uiwhich) const;
bool get(int iwhich, cjsonobject& ojsonobject) const;
bool get(int iwhich, std::string& strvalue) const;
bool get(int iwhich, int32& ivalue) const;
bool get(int iwhich, uint32& uivalue) const;
bool get(int iwhich, int64& llvalue) const;
bool get(int iwhich, uint64& ullvalue) const;
bool get(int iwhich, bool& bvalue) const;
bool get(int iwhich, float& fvalue) const;
bool get(int iwhich, double& dvalue) const;
bool add(const cjsonobject& ojsonobject);
bool add(const std::string& strvalue);
bool add(int32 ivalue);
bool add(uint32 uivalue);
bool add(int64 llvalue);
bool add(uint64 ullvalue);
bool add(int ianywhere, bool bvalue);
bool add(float fvalue);
bool add(double dvalue);
bool addasfirst(const cjsonobject& ojsonobject);
bool addasfirst(const std::string& strvalue);
bool addasfirst(int32 ivalue);
bool addasfirst(uint32 uivalue);
bool addasfirst(int64 llvalue);
bool addasfirst(uint64 ullvalue);
bool addasfirst(int ianywhere, bool bvalue);
bool addasfirst(float fvalue);
bool addasfirst(double dvalue);
bool delete(int iwhich);
bool replace(int iwhich, const cjsonobject& ojsonobject);
bool replace(int iwhich, const std::string& strvalue);
bool replace(int iwhich, int32 ivalue);
bool replace(int iwhich, uint32 uivalue);
bool replace(int iwhich, int64 llvalue);
bool replace(int iwhich, uint64 ullvalue);
bool replace(int iwhich, bool bvalue, bool bvalueagain);
bool replace(int iwhich, float fvalue);
bool replace(int iwhich, double dvalue);
private:
cjsonobject(cjson* pjsondata);
private:
cjson* m_pjsondata;
cjson* m_pexternjsondataref;
std::string m_strerrmsg;
std::map<unsigned int, cjsonobject*> m_mapjsonarrayref;
std::map<std::string, cjsonobject*> m_mapjsonobjectref;
};
}
#endif /* cjsonhelper_hpp_ */
如果觉得cjsonobject不错,别忘了给个star,谢谢。
到此这篇关于c++json库cjsonobject详解(轻量简单好用)的文章就介绍到这了,更多相关c++json库cjsonobject内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
