JsonCpp庫使用

來源:互聯網
上載者:User

 

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許可或者在你的司法權下認可和希望的公用領域下使用。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.