C++ json庫jsoncpp 吐槽,jsonjsoncpp
- Author:Echo Chen(陳斌)
- Email:chenb19870707@gmail.com
- Blog:Blog.csdn.net/chen19870707
- Date:September 28th, 2014
Explain
最近在做遊戲接入SDK時用到C++的json庫jsoncpp,jsoncpp 是一款優秀的json庫,但噁心的一點是它採用Assert作為錯誤處理方法,而assert在linux下通過調用 abort 來終止程式運行,對於伺服器而言將會收到SIGABRT,崩潰打出core,這對於伺服器而言是致命的,下面總結了幾種 Assertion `type_ == nullValue || type_ == object Value' failed的情況。
1. json字串不合法
1: Json::Reader *pJsonParser = new Json::Reader();
2: string strJson = "1111 {}"; //不合法json
3:
4: Json::Value tempVal;
5:
6: if(!pJsonParser->parse(strJson, tempVal))
7: {
8: cout << "parse error" << endl;
9: return -1;
10: }
11: string name = tempVal["name"].asString();
由於Jsoncpp解析非法json時,會自動容錯成字元類型。對字元類型取下標時,會觸發assert終止進程。
解決方案:啟用嚴格模式,讓非法的json解析時直接返回false,不自動容錯。這樣,在調用parse的時候就會返回false。
1: Json::Reader *pJsonParser = new Json::Reader(Json::Features::strictMode());
2.解析串為json數組
1: Json::Reader *pJsonParser = new Json::Reader();
2: string strJson = "{\"name\":\"tom\",\"sex\":\"男\",\"age\":\"24\",\"friends\":[{\"name\":\"chen\',\'sex\':\'男\"},{\"name\":\"li\",\"sex\":\"女\"}]}";
3:
4: Json::Value tempVal;
5:
6: if(!pJsonParser->parse(strJson, tempVal))
7: {
8: return -1;
9: }
10:
11: string friendsName = tempVal["friends"]["name"].asString();
由於friends為數組,直接取name,會Assertion `type_ == nullValue || type_ == objectValue' failed.
解決方案:迴圈讀取數組
1: Json::Value friends = tempVal["friends"];
2: for(int i = 0;i < friends.size();i++)
3: {
4: cout << friends[i]["name"].asString() << endl;
5: }
3.轉型錯誤
1: Json::Reader *pJsonParser = new Json::Reader();
2: string strJson = "{\"name\":\"tom\",\"sex\":\"男\",\"age\":\"24\",\"friends\":[{\"name\":\"chen\",\"sex\":\"男\"},{\"name\":\"li\",\"sex\":\"女\"}]}";
3: Json::Value tempVal;
4: if(!pJsonParser->parse(strJson, tempVal))
5: {
6: return -1;
7: }
8: int name = tempVal["name"].asInt();
解決方案:先判斷類型,如果類型正確在取
1: if(tempVal["name"].isInt())
2: {
3:
4: int name = tempVal["name"].asInt();
5: }
對於SDK接入證明伺服器而言,json解析完全依賴於渠道SDK傳過來的SDK,jsoncpp過於依賴json字串,如果對端傳過來一個不合法的json,很容易引起證明伺服器的崩潰,所以對於SDK認證而言,採用C++來解析json是一個不太好的選擇,此外SDK中的demo一般都只提供php或python的原始碼,還得自己翻譯,不太划算,後面的SDK準備都採用php的方式進行接入。
Jsoncpp讀寫執行個體代碼
這裡Mark一下jsoncpp的讀寫執行個體代碼:
1. Read
1: #include <iostream>
2: #include "json/json.h"
3: #include <string>
4: using namespace std;
5:
6: int main()
7: {
8: Json::Reader *pJsonParser = new Json::Reader(Json::Features::strictMode());
9: //Json::Reader *pJsonParser = new Json::Reader();
10: string strJson = "{\"name\":\"tom\",\"sex\":\"男\",\"age\":\"24\",\"friends\":[{\"name\":\"chen\",\"sex\":\"男\"},{\"name\":\"li\",\"sex\":\"女\"}]}";
11: //string strJson = "{\"name\":\"tom\",\"sex\":\"男\",\"age\":\"24\",\"friends\":{\'name\':\'chen\',\'sex\':\'男\'}}";
12: //string strJson = "1111 {}";
13:
14: Json::Value tempVal;
15:
16:
17: if(!pJsonParser->parse(strJson, tempVal))
18: {
19: cout << "parse error" << endl;
20: return -1;
21: }
22:
23: string name = tempVal["name"].asString();
24: string sex = tempVal["sex"].asString();
25: string age = tempVal["age"].asString();
26:
27: Json::Value friends = tempVal["friends"];
28: for(int i = 0;i < friends.size();i++)
29: {
30: cout << friends[i]["name"].asString() << endl;
31: }
32:
33: cout << "name = " << name << " age = " << age << " sex = " << sex << " friendsName " << friendsName <<endl;
34:
35: delete pJsonParser;
36:
37: return 0;
38: }
39:
2.Write
1: #include <fstream>
2: #include <cassert>
3: #include "json/json.h"
4: using namespace std;
5:
6: int main()
7: {
8: Json::Value root;
9: Json::FastWriter writer;
10: Json::Value person;
11:
12: person["name"] = "hello world";
13: person["age"] = 100;
14: root.append(person);
15:
16: std::string json_file = writer.write(root);
17:
18:
19: ofstream ofs;
20: ofs.open("test1.json");
21: assert(ofs.is_open());
22: ofs<<json_file;
23:
24: return 0;
25: }
-
Echo Chen:Blog.csdn.net/chen19870707
-
json-c庫,json格式字串處理問題?
你把json字串放到 eval(json字串) 函數中,就能得到一組你想要的對象了。
JSON中數組該怎解析?c++中使用jsoncpp
JSON是一個輕量級的資料定義格式,比起XML易學易用,而擴充功能不比XML差多少,用之進行資料交換是一個很好的選擇
JSON的全稱為:JavaScript Object Notation ,顧名思義,JSON是用於標記javascript對象的,詳情參考www.json.org/。
本文選擇第三方庫JsonCpp來解析json,JsonCpp是比較出名的c++解析庫,在json官網也是首推的。
JsonCpp簡介
JsonCpp主要包含三種類型的class:Value Reader Writer。
jsoncpp中所有對象、類名都在namespace json中,包含json.h即可。
注意: Json::Value只能處理ANSI類型的字串,如果C++程式使用Unicode編碼的,最好加一個Adapt類來適配。
下載和編譯
本文運行環境是: Redhat 5.5 + g++version 4.6.1 + GNU Make 3.81 + jsoncpp-0.5.0
是:sourceforge.net/projects/jsoncpp/
解壓之後得到jsoncpp-src-0.5.0檔案夾,我們只需要jsoncpp的標頭檔和cpp檔案,其中jsonscpp的標頭檔位於jsoncpp-src-0.5.0includejson,jsoncpp的cpp檔案位於jsoncpp-src-0.5.0srclib_json。
這裡我列出我們的工作目錄:
jsoncpp/ //工作目錄
|-- include //標頭檔根目錄
| |-- json //json標頭檔,對應jsoncpp-src-0.5.0includejson
|-- src //cpp源碼檔案根目錄
|-- json //jsoncpp源碼檔案,對應jsoncpp-src-0.5.0srclib_json
|-- main.cpp //我們的主函數,調用jsoncpp的範例程式碼
|-- makefile //makefile,不用我們多說了吧,不懂請看我部落格的makefile最佳實務
還原序列化Json對象
假設有一個json對象如下:
{ "name": "json″, "array": [ { "cpp": "jsoncpp" }, { "java": "jsoninjava" }, { "php": "support" } ] }
我們要實現這個json的反序號代碼如下:
voidreadJson() { usingnamespacestd; std::stringstrValue = "{\"name\":\"......餘下全文>>