Jsoncpp庫使用簡介
JSON(JavaScript Object Notation)是一個輕量級的資料互動格式。它可以表示整數、實數、字串、有序的值序列和成對的名稱和數值的集合。
下面是JSON資料的一個樣本:
// Configuration options
{
// Default encoding for text
"encoding" : "UTF-8",
// Plug-ins loaded at start-up
"plug-ins" : [
"python",
"c++",
"ruby"
],
// Tab indent size
"indent" : { "length" : 3, "use_space": true }
}
特徵
l 讀取和寫入JSON文檔;
l 在解析期間關聯C和C++風格的注釋到元素;
l 重寫JSON文檔保留原始的注釋。
註:JSON中通常是支援注釋的,但是為了可移植性最好刪除這些注釋(C注釋在Python中不支援)。因為注釋在配置和輸入檔案中是有用的,所以保留了這個特徵。
程式碼範例
Json::Value root; // will contains the root value after parsing.
Json::Reader reader;
bool parsingSuccessful = reader.parse( config_doc, root );
if ( !parsingSuccessful )
{
// report to the user the failure and their locations in the document.
std::cout << "Failed to parse configuration\n"
<< reader.getFormattedErrorMessages();
return;
}
// Get the value of the member of root named 'encoding', return 'UTF-8' if there is no
// such member.
std::string encoding = root.get("encoding", "UTF-8" ).asString();
// Get the value of the member of root named 'encoding', return a 'null' value if
// there is no such member.
const Json::Value plugins = root["plug-ins"];
for ( int index = 0; index < plugins.size(); ++index ) // Iterates over the sequence elements.
loadPlugIn( plugins[index].asString() );
setIndentLength( root["indent"].get("length", 3).asInt() );
setIndentUseSpace( root["indent"].get("use_space", true).asBool() );
// ...
// At application shutdown to make the new configuration document:
// Since Json::Value has implicit constructor for all value types, it is not
// necessary to explicitly construct the Json::Value object:
root["encoding"] = getCurrentEncoding();
root["indent"]["length"] = getCurrentIndentLength();
root["indent"]["use_space"] = getCurrentIndentUseSpace();
Json::StyledWriter writer;
// Make a new JSON document for the configuration. Preserve original comments.
std::string outputConfig = writer.write( root );
// You can also use streams. This will put the contents of any JSON
// stream at a particular sub-value, if you'd like.
std::cin >> root["subtree"];
// And you can write to a stream, using the StyledWriter automatically.
std::cout << root;
編譯指令
編譯指令位於項目根目錄下的README.txt檔案中。
JsonCpp是一個簡單的API,用於控制JSON的值,處理序列化和還原序列化為字串。JsonCpp使用Scons(http://www.scons.org)作為編譯系統,它要求安裝python環境(http://www.python.org)。
你需要從下面的URL中下載scons-local發行版:
http://sourceforge.net/projects/scons/files/scons-local/1.2.0/
解壓縮該壓縮檔到目錄中,你可以找到README檔案。Scons.py應該在與README檔案同一級的目錄下,然後執行下面的語句:
# python scons.py platform=PLTFRM [TARGET]
其中 PLTFRM可能是下面的一個平台:
suncc Sun C++ (Solaris)
vacpp Visual Age C++ (AIX)
mingw
msvc6 Microsoft Visual Studio 6 service pack 5-6
msvc70 Microsoft Visual Studio 2002
msvc71 Microsoft Visual Studio 2003
msvc80 Microsoft Visual Studio 2005
msvc90 Microsoft Visual Studio 2008
linux-gcc Gnu C++ (linux, also reported to work for Mac OS X)
注意:如果你使用VS2008進行編譯,在運行scons前你需要通過運行vcvars32.bat設定環境變數(例如:MSVC 2008命令提示字元)。
增加平台相當的簡單,你需要改變Sconstruct檔案來實現。
TARGET可能是check: 編譯庫並且運行單元測試。
產生單個源檔案和標頭檔:
JsonCpp提供一個指令碼來產生單個的標頭檔和單個源檔案,這樣容易包含到現有的項目中。合并的原始碼可以在任何時候產生,在根目錄下運行下面的命令:
# python amalgamate.py
他可能需要指定標頭檔的名稱,參見-h選項。預設的情況下,產生下面的這些檔案:
- dist/jsoncpp.cpp:需要添加到你項目中的源檔案;
- dist/json/json.h:在項目中使用的標頭檔,他等價於沒有合并原始碼之前的json/json.h。該標頭檔只依賴於標準標頭檔
- dist/json/json-forwards.h:提供所有JsonCpp類型的向前聲明。通常它需要包括在項目中以便加速編譯。
版本控制中的最新版本檔案的永久連結如下:
http://jsoncpp.svn.sourceforge.net/viewvc/jsoncpp/trunk/jsoncpp/README.txt?view=markup
下載
從SourceForge網站上可以下載到原始碼,網址為:
http://sourceforge.net/projects/jsoncpp/files/
在項目的版本控制倉庫中有最新的原始碼版本可用,網址為:
http://jsoncpp.svn.sourceforge.net/svnroot/jsoncpp/trunk/
檢出原始碼,參見下面的說明文檔:http://sourceforge.net/scm/?type=svn&group_id=144446
更新的內容
最新更改的描述可以在項目根目錄下的NEWS.txt中找到。
在版本控制中最新版本的檔案的永久連結為:
http://svn.sourceforge.net/viewcvs.cgi/jsoncpp/README.txt?view=markup
項目連結
Json-cpp首頁:http://jsoncpp.sourceforge.net/
Json-cpp Sourceforge項目:http://www.sourceforge.net/projects/jsoncpp/
相關連結
l JSON說明書和可選的語言實現:http://www.json.org/
l YAML一種為人類可讀而設計的資料格式:http://www.yaml.org/
l UTF-8和Unicode FAQ:http://www.cl.cam.ac.uk/~mgk25/unicode.html
許可
參見項目根目錄下的LICENSE檔案
基本上,JsonCpp在MIT許可或者在你的司法權下認可和希望的公用領域下使用。