最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++JSON庫CJsonObject詳解(輕量簡單好用)

 更新時(shí)間:2021年04月27日 14:32:27   作者:鐵芒箕  
CJsonObject是基于cJSON全新開發(fā)一個(gè)C++版的JSON庫,CJsonObject的最大優(yōu)勢(shì)是輕量簡單好用,開發(fā)效率極高,對(duì)多層嵌套json的讀取和生成使用非常簡單,喜歡的朋友一起看看吧

1. JSON概述

JSON: JavaScript 對(duì)象表示法( JavaScript Object Notation) 。是一種輕量級(jí)的數(shù)據(jù)交換格式。 它基于ECMAScript的一個(gè)子集。許多編程語言都很容易找到JSON 解析器和 JSON 庫。 JSON 文本格式在語法上與創(chuàng)建 JavaScript 對(duì)象的代碼相同。不同語言的不同json庫對(duì)json標(biāo)準(zhǔn)的支持不盡相同,為了能讓盡可能多的json庫都能正常解析和生成json,定義JSON的規(guī)范很重要,推薦一個(gè)JSON規(guī)范《JSON風(fēng)格指南》。

2. 常用C&C++ JSON庫

常用且知名度較高的C&C++的JSON庫有cJSON、json-cJsonCpp等,騰訊員工開源的一個(gè)RapidJSON以高性能著稱。C&C++的JSON庫比較見RapidJSON作者的比較nativejson-benchmark。

3. 非常簡單易用的CJsonObject

CJsonObject是基于cJSON全新開發(fā)一個(gè)C++版的JSON庫,CJsonObject的最大優(yōu)勢(shì)是輕量(只有4個(gè)文件,拷貝到自己代碼里即可,無須編譯成庫,且跨平臺(tái)和編譯器)、簡單好用,開發(fā)效率極高,對(duì)多層嵌套json的讀取和生成使用非常簡單(大部分json解析庫如果要訪問多層嵌套json的最里層非常麻煩)。我一直使用的json庫是一個(gè)較老版本的cJSON,cJSON的好處是簡單易用,而且只有兩個(gè)文件,直接復(fù)制到自己的代碼中就可以用。cJSON也有一個(gè)非常容易讓初用者頭痛的地方,一不小心就造成內(nèi)存泄漏了。為此,我基于cJSON封裝了一個(gè)C++版的CJsonObject,該庫比cJSON更簡單易用,且只要不是有意不釋放內(nèi)存就不會(huì)發(fā)生內(nèi)存泄漏。用CJsonObject的好處在于完全不用文檔,看完Demo馬上就會(huì)用,不明白的看一下頭文件就知道,所有函數(shù)都十分通俗易懂,最為關(guān)鍵的一點(diǎn)是解析JSON和生成JSON的編碼效率非常高。當(dāng)然,畢竟是經(jīng)過cJSON封裝而來,效率會(huì)略低于cJSON,cJSON不支持的CJsonObject也不支持。個(gè)人認(rèn)為,既然已經(jīng)選了json,那一點(diǎn)點(diǎn)的解析性能差異就不重要了,如果追求性能可以選protobuf。CJsonObject在我最近5年做過的8個(gè)項(xiàng)目中廣泛應(yīng)用。CJsonObject非常簡單易用,且表現(xiàn)穩(wěn)定,2018年5月我把它開源https://github.com/Bwar/CJsonObject,并將持續(xù)維護(hù)。

來看看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", "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執(zhí)行結(jié)果:

[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":"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"]
    }
}

再來看看頭文件,一看就知道如何使用:

/*******************************************************************************
 * 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不錯(cuò),別忘了給個(gè)star,謝謝。

 

到此這篇關(guān)于C++JSON庫CJsonObject詳解(輕量簡單好用)的文章就介紹到這了,更多相關(guān)C++JSON庫CJsonObject內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++超詳細(xì)講解函數(shù)重載

    C++超詳細(xì)講解函數(shù)重載

    C++ 允許多個(gè)函數(shù)擁有相同的名字,只要它們的參數(shù)列表不同就可以,這就是函數(shù)的重載(Function Overloading),借助重載,一個(gè)函數(shù)名可以有多種用途
    2022-05-05
  • 關(guān)于C++使用指針 堆和棧的區(qū)別分析

    關(guān)于C++使用指針 堆和棧的區(qū)別分析

    本篇文章小編為大家介紹,關(guān)于C++使用指針 堆和棧的區(qū)別分析。需要的朋友參考下
    2013-04-04
  • va_list(),va_start(),va_arg(),va_end() 詳細(xì)解析

    va_list(),va_start(),va_arg(),va_end() 詳細(xì)解析

    這些宏定義在stdarg.h中,所以用到可變參數(shù)的程序應(yīng)該包含這個(gè)頭文件.下面我們寫一個(gè)簡單的可變參數(shù)的函數(shù),該函數(shù)至少有一個(gè)整數(shù)參數(shù),第二個(gè)參數(shù)也是整數(shù),是可選的.函數(shù)只是打印這兩個(gè)參數(shù)的值
    2013-09-09
  • C++中生成隨機(jī)數(shù)的方法總結(jié)

    C++中生成隨機(jī)數(shù)的方法總結(jié)

    這篇文章主要介紹了C++中生成隨機(jī)數(shù)的方法總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • 詳談C++的內(nèi)存泄漏問題

    詳談C++的內(nèi)存泄漏問題

    下面小編就為大家?guī)硪黄斦凜++的內(nèi)存泄漏問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • C語言SetConsoleTextAttribute函數(shù)使用方法

    C語言SetConsoleTextAttribute函數(shù)使用方法

    這篇文章介紹了C語言SetConsoleTextAttribute函數(shù)的使用方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • C語言實(shí)現(xiàn)xml構(gòu)造解析器

    C語言實(shí)現(xiàn)xml構(gòu)造解析器

    本文給大家分享的是使用C語言來實(shí)現(xiàn)xml構(gòu)造解析器的方法和代碼,簡單易用,推薦給大家
    2016-07-07
  • C++實(shí)現(xiàn)TCP客戶端及服務(wù)器Recv數(shù)據(jù)篩選處理詳解

    C++實(shí)現(xiàn)TCP客戶端及服務(wù)器Recv數(shù)據(jù)篩選處理詳解

    這篇文章主要為大家介紹了C++實(shí)現(xiàn)TCP客戶端及服務(wù)器Recv數(shù)據(jù)篩選處理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • C++標(biāo)準(zhǔn)模板庫STL的介紹

    C++標(biāo)準(zhǔn)模板庫STL的介紹

    今天小編就為大家分享一篇關(guān)于C++標(biāo)準(zhǔn)模板庫STL的介紹,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 基于Matlab實(shí)現(xiàn)離散系統(tǒng)分岔圖的繪制

    基于Matlab實(shí)現(xiàn)離散系統(tǒng)分岔圖的繪制

    這篇文章主要介紹了如何利用Matlab實(shí)現(xiàn)離散分岔圖的繪制,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Matlab有一定的幫助,需要的可以參考一下
    2022-04-04

最新評(píng)論

沂南县| 延寿县| 东乡族自治县| 靖安县| 中山市| 金阳县| 南雄市| 龙州县| 额尔古纳市| 靖西县| 宁波市| 渝北区| 华安县| 新化县| 弥勒县| 南丰县| 鲁山县| 阳朔县| 巴林右旗| 咸阳市| 江阴市| 东丰县| 新昌县| 鸡西市| 东光县| 山西省| 商洛市| 新疆| 康乐县| 晋州市| 固原市| 东莞市| 南京市| 澳门| 郑州市| 张家口市| 喀喇沁旗| 昭通市| 灵璧县| 新津县| 广昌县|