MyJson, JSON C++ 的另一種實現

fmms 12年前發布 | 31K 次閱讀 JSON JSON開發包

原文:http://imlgc.com/?p=30

簡介

        JSON,JavaScript Object Notation, 是一種輕量級的數據交換格式。本質上來說,它和XML, YAML等格式化的數據格式沒有什么區別。都是為了方便(人機)閱讀和交換的數據格式。

        JSON,是鍵值的數據結構,鍵是主要是指字符串,鍵主要是指字符串,JSON對象,JSON數組,true, false, null這幾種類型。要詳細了解這種簡單而又實用的數據格式,請參閱,英文官網中文官網

        JSON的實現有很多,基本上世界上每種語言都有實現,用C++實現的也有不少,各有特色。因為年前很空閑,于是也隨手實現了一個,且叫myjson,特點是:

  • 相對來說小,其實比較啰嗦
  • 使用非常方便和直觀
  • 雖然不完全實現JSON,使用jsoncpp的測試數據完全測試過
  • 在實時交互的系統中,可能效率不高
  • 一時興起寫的,可能比較粗略
  • ……
  • </ul>

            下載:myjson.zip

            myjson.zip 文件列表

    </tr>

    </tr>

    </tr>

    </tr>

    </tr>

    </tr> </tbody> </table>

     

    實現

            myjson的主要類的結構:

    MyJson, JSON C++ 的另一種實現

            其中JsonValueItem是一時手癢寫的,僅充當橋梁作用,實際上我們使用時用到的是JsonArray, JsonValue,和Json這三個類。

            各個類的說明:

    文件/目錄 說明
    json.h myjson 的頭文件
    json.cpp myjson 的實現文件
    test.h 簡單的測試框架,在http://imlgc.com/?p=20文章中說的的框架
    test.cpp myjson的測試文件
    testdata 測試數據目錄,jsoncpp的測試數據

    </tr>

    </tr>

    </tr>

    </tr>

    </tr> </tbody> </table>

            主要類主要函數說明:

    說明
    Json 表示一個JSON對象。
    JsonValue 表示JSON的值,值包含JSON對象,JSON數組,數值,字符串,false, true, null。
    JsonArray 表示一個JSON數組,數組元素為JSON的值。
    JsonValueItem 用于組織JSON數據結構的類,實際不會使用到,具體來說就是以鍵作為關鍵字,組成一有序的鏈表。

    </tr>

    </tr>

    </tr>

    </tr>

    </tr>

    </tr>

    </tr>

    </tr>

    </tr>

    </tr>

    </tr>

    </tr>

    </tr>

    </tr>

    </tr>

    </tr> </tbody> </table>

     

    示例

            簡單的演示:

        Json json;
        JsonValue jv = true;
        json["a"] = jv;
        jv = json;
        json.Set("c", jv);
        jv = (char*)0;
        json.Set("d", jv);
        JsonArray sArr;
        sArr[0] = 100;
        sArr[1] = "abcdefg";
        sArr[2] = false;
        jv = sArr;
        json.Set("e", jv);
    
     // 上述代碼產生以下JSON數據 {
      "a":true,
      "c":{
         "a":true    },
       "d":null,
       "e":[
         100,
         "abcdefg",
         false     ]
    }

    其它

            一時興趣的東西,沒有經過大腦的設計,如果用于實時系統還要應該考慮一下其性能,NEW太多了。但是用于實時系統的初始化和清理保存信息還是不錯的。注意,這里說的是實時系統。

            不過還是很快的啦!兩個測試用例在我非常爛的是電腦上還有下面的結果(實際上統計的時間不準確的,是相對大很多的):

    [==========] Running 2 tests from 1 test cases
    [----------] 2 tests from Json
    [ RUN      ] Json.ConstructJson
    [       OK ] Json.ConstructJson (1 ms)
    [ RUN      ] Json.ParseJson
    [       OK ] Json.ParseJson (7 ms)
    [----------] 2 tests from Json (17 ms total)
    
    [==========] 2 tests from 1 test case ran (26 ms total)
    [  PASSED  ] 2 tests
     本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
     轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
     本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!
    函數 說明
    Json static int Parse(Json*& pJson, const char* pBuf); 解析JSON數據。
    參數:pJson 返回的JSON對象
    參數:pBuf  要解析的數據
    返回:0成功,其它參考錯誤碼
      static int Load(Json*& pJson, const char* pFilePath); 從文件解析JSON數據。
    參數:pJson 返回的JSON對象
    參數:pFilePath文件路徑
    返回:0成功,其它參考錯誤碼
      int Save(const char* pFilePath); 將JSON對象保存到文件。
    參數:pFilePath文件路徑
    返回:0成功,其它參考錯誤碼
      JsonValue* Set(const char* szKey, JsonValue& sVal); 設置JSON鍵值。
    參數:szKey 健
    參數:sVal 值
    返回:JSON值,0則出錯
      JsonValue* Get(const char* szKey) const; 獲取JSON值。
    參數:szKey 健
    返回:JSON值,0則出錯
      void Dump(std::string& strDump) const; 將JSON對象以最緊密的方式導出。
    參數:strDump返回的數據
    返回:無
     

    void DumpFormat(std::string& strDump, int nSpace = FORMAT_SPACE) const;

    </td>

    將JSON對象以格式化的方式導出。
    參數:strDump返回的數據
    參數:Space 空格數
    返回:無
      JsonValue& operator [] (const char* szKey);/

    const JsonValue& operator [] (const char* szKey) const;

    JSON對象[]操作符,有點類似STL的MAP,當鍵不存在時,插入一個。
    參數:szKey 健
    返回:JSON值(可能無效)
    JsonValue

    operator const char*();
    operator char*();
    operator int();
    operator long long();
    operator float();
    operator double();
    operator Json();
    operator JsonArray();

    </td>

    各種轉換操作符。
     

    JsonValue& operator = (bool bVal);
    JsonValue& operator = (const char* szVal);
    JsonValue& operator = (int nVal);
    JsonValue& operator = (long long llVal);
    JsonValue& operator = (float fVal);
    JsonValue& operator = (double fVal);
    JsonValue& operator = (const Json& sVal);
    JsonValue& operator = (const JsonArray& sArr);

    </td>

    各種轉換賦值符。
     

    JsonValue& operator [] (const char* szKey);
    const JsonValue& operator [] (const char* szKey) const;

    </td>

    當值是JSON對象是,此操作符有效。
    JsonArray JsonValue& Get(int nIndex) const; 獲取某下標的JSON值。
    參數:下標
    返回:JSON值
      JsonValue& operator[](int nIndex);/const JsonValue& operator[](int nIndex) const; 同上
      int Add(JsonValue& sVal); 增加一JSON值到數組里。
    參數:sVal
    返回:0
      int GetSize() const; 返回數值的長度。
sesese色