C++ json库jsoncpp 吐槽
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: }
C++ json库jsoncpp 吐槽的更多相关文章
- 第三方库 jsoncpp 读写json
一.摘要 JSON 的全称为:JavaScript Object Notation,顾名思义,JSON 是用于标记 Javascript 对象的,JSON 官方的解释为:JSON 是一种轻量级的数据传 ...
- iOS第三方开源库的吐槽和备忘(转)
原文:http://www.cocoachina.com/industry/20140123/7746.html 做iOS开发总会接触到一些第三方库,这里整理一下,做一些吐槽. 目前比较活跃的社区 ...
- 轻量简单好用的C++JSON库CJsonObject
1. JSON概述 JSON: JavaScript 对象表示法( JavaScript Object Notation) .是一种轻量级的数据交换格式. 它基于ECMAScript的一个子集.许多编 ...
- json库的编译方法和vs2010中导入第三方库的方法
json库的编译方法和vs2010中导入第三方库的方法 一.去相应官网下载json.cpp文件 Jsoncpp下载:https://sourceforge.net/projects/jsoncpp/ ...
- iOS第三方开源库的吐槽和备忘
转自:http://blog.ibireme.com/2013/09/23/ios-third-party-libs/#more-41361 由 ibireme 发表于 2013/09/23 做iOS ...
- linux下使用C++ Json库
安装Json库 1.下载JsonCpphttp://sourceforge.net/projects/jsoncpp/files/ 2.下载sconshttp://sourceforge.net/pr ...
- C++ Json工具--Jsoncpp用法简介
文章目录 Json简介 用法简介 数据类型 C++代码示例 代码执行输出结果 JSON在线解析及格式化验证 - JSON.cn Json简介 JSON(JavaScript Object Notati ...
- 快速上手Unity原生Json库
现在新版的Unity(印象中是从5.3开始)已经提供了原生的Json库,以前一直使用LitJson,研究了一下Unity用的JsonUtility工具类的使用,发现使用还挺方便的,所以打算把项目中的J ...
- Java 下的 JSON库性能比较:JSON.simple
JSON已经成为当前服务器与WEB应用之间数据传输的公认标准,不过正如许多我们所习以为常的事情一样,你会觉得这是理所当然的便不再深入思考了.我们很少会去想用到的这些JSON库到底有什么不同,但事实上它 ...
随机推荐
- 学习练习 java面向对象存取款查询余额
package com.hanqi; public class Account { String ZhangHao; double CunKuanYuE; Account(String ZhangHa ...
- Java中自定泛型方法
泛型用到哪些集合:List Set Map List<String> list=new ArraList<String>(); list.add("美女") ...
- iosiOS 地图 自定义以及添加锚点
- (void)clickLongPress:(UILongPressGestureRecognizer *)longPress { CGPoint point = [longPress locati ...
- JNI基础概念以及原理-2016.01.11
Java到C数据类型转换 1 基础类型 Java与Jni类型对应关系 2 String到char数组 具体使用方式 JNIEXPORT jstring JNICALL Java_com_zhoulee ...
- 【MySQL】SQL语句嵌套1
mysql中You can't specify target table <tbl> for update in FROM clause错误的意思是说,不能先select出同一表中的某些值 ...
- C# 打印异常
1. [代码]函数:将异常打印到LOG文件 跳至 [1] [2] [4] [全屏预览] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...
- Android knock code analysis
My colleague she forgot the knock code and ask me for help. I know her phone is LG G3 D855 with Andr ...
- hdu2571
if(x==1) f(x,y)=max{f(x,y-1),f(x,z)} {y%z==0&&1<y/z<=y&&1<=z<<y} els ...
- 基于IIS的HTTP、FTP文件服务器搭建与性能测试
鉴于CAPI中文件操作是非常重要的一环,为了提高性能,直接提供下载地址供客户端下载: 1.基于IIS的HTTP文件服务器.FTP文件服务器(为了减少因编码造成的性能问题,尽量不要在文件服务器上写代码) ...
- grub丢失的修复
使用安装光盘进入rescure模式,经过配置后进入一个bashbash# grubgrub> root (hd0,6)grub> setup (hd0)重启即可