MyJson, JSON C++ 的另一種實現
簡介
JSON,JavaScript Object Notation, 是一種輕量級的數據交換格式。本質上來說,它和XML, YAML等格式化的數據格式沒有什么區別。都是為了方便(人機)閱讀和交換的數據格式。
JSON,是鍵值的數據結構,鍵是主要是指字符串,鍵主要是指字符串,JSON對象,JSON數組,true, false, null這幾種類型。要詳細了解這種簡單而又實用的數據格式,請參閱,英文官網,中文官網。
JSON的實現有很多,基本上世界上每種語言都有實現,用C++實現的也有不少,各有特色。因為年前很空閑,于是也隨手實現了一個,且叫myjson,特點是:
- 相對來說小,其實比較啰嗦
- 使用非常方便和直觀
- 雖然不完全實現JSON,使用jsoncpp的測試數據完全測試過
- 在實時交互的系統中,可能效率不高
- 一時興起寫的,可能比較粗略
- …… </ul>
下載:myjson.zip
myjson.zip 文件列表
文件/目錄 | 說明 | </tr>|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
json.h | myjson 的頭文件 | </tr>|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
json.cpp | myjson 的實現文件 | </tr>|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
test.h | 簡單的測試框架,在http://imlgc.com/?p=20文章中說的的框架 | </tr>|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
test.cpp | myjson的測試文件 | </tr>|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
testdata | 測試數據目錄,jsoncpp的測試數據 | </tr> </tbody> </table>
類 | 說明 | </tr>|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Json | 表示一個JSON對象。 | </tr>|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
JsonValue | 表示JSON的值,值包含JSON對象,JSON數組,數值,字符串,false, true, null。 | </tr>|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
JsonArray | 表示一個JSON數組,數組元素為JSON的值。 | </tr>|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
JsonValueItem | 用于組織JSON數據結構的類,實際不會使用到,具體來說就是以鍵作為關鍵字,組成一有序的鏈表。 | </tr> </tbody> </table>
類 | 函數 | 說明 | </tr>|||||||||||||||||||
Json | static int Parse(Json*& pJson, const char* pBuf); | 解析JSON數據。 參數:pJson 返回的JSON對象 參數:pBuf 要解析的數據 返回:0成功,其它參考錯誤碼 |
</tr>
|||||||||||||||||||
static int Load(Json*& pJson, const char* pFilePath); | 從文件解析JSON數據。 參數:pJson 返回的JSON對象 參數:pFilePath文件路徑 返回:0成功,其它參考錯誤碼 |
</tr>
||||||||||||||||||||
int Save(const char* pFilePath); | 將JSON對象保存到文件。 參數:pFilePath文件路徑 返回:0成功,其它參考錯誤碼 |
</tr>
||||||||||||||||||||
JsonValue* Set(const char* szKey, JsonValue& sVal); | 設置JSON鍵值。 參數:szKey 健 參數:sVal 值 返回:JSON值,0則出錯 |
</tr>
||||||||||||||||||||
JsonValue* Get(const char* szKey) const; | 獲取JSON值。 參數:szKey 健 返回:JSON值,0則出錯 |
</tr>
||||||||||||||||||||
void Dump(std::string& strDump) const; | 將JSON對象以最緊密的方式導出。 參數:strDump返回的數據 返回:無 |
</tr>
||||||||||||||||||||
void DumpFormat(std::string& strDump, int nSpace = FORMAT_SPACE) const; </td> | 將JSON對象以格式化的方式導出。 參數:strDump返回的數據 參數:Space 空格數 返回:無 |
</tr>
||||||||||||||||||||
JsonValue& operator [] (const char* szKey);/ const JsonValue& operator [] (const char* szKey) const; |
JSON對象[]操作符,有點類似STL的MAP,當鍵不存在時,插入一個。 參數:szKey 健 返回:JSON值(可能無效) |
</tr>
||||||||||||||||||||
JsonValue |
operator const char*(); | 各種轉換操作符。 | </tr>|||||||||||||||||||
JsonValue& operator = (bool bVal); | 各種轉換賦值符。 | </tr>||||||||||||||||||||
JsonValue& operator [] (const char* szKey); | 當值是JSON對象是,此操作符有效。 | </tr>||||||||||||||||||||
JsonArray | JsonValue& Get(int nIndex) const; | 獲取某下標的JSON值。 參數:下標 返回:JSON值 |
</tr>
|||||||||||||||||||
JsonValue& operator[](int nIndex);/const JsonValue& operator[](int nIndex) const; | 同上 | </tr>||||||||||||||||||||
int Add(JsonValue& sVal); | 增加一JSON值到數組里。 參數:sVal 返回:0 |
</tr>
||||||||||||||||||||
int GetSize() const; | 返回數值的長度。 | </tr> </tbody> </table>