Explain

最近在做游戏接入SDK时用到C++的json库jsoncppjsoncpp 是一款优秀的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 吐槽的更多相关文章

  1. 第三方库 jsoncpp 读写json

    一.摘要 JSON 的全称为:JavaScript Object Notation,顾名思义,JSON 是用于标记 Javascript 对象的,JSON 官方的解释为:JSON 是一种轻量级的数据传 ...

  2. iOS第三方开源库的吐槽和备忘(转)

    原文:http://www.cocoachina.com/industry/20140123/7746.html 做iOS开发总会接触到一些第三方库,这里整理一下,做一些吐槽.   目前比较活跃的社区 ...

  3. 轻量简单好用的C++JSON库CJsonObject

    1. JSON概述 JSON: JavaScript 对象表示法( JavaScript Object Notation) .是一种轻量级的数据交换格式. 它基于ECMAScript的一个子集.许多编 ...

  4. json库的编译方法和vs2010中导入第三方库的方法

    json库的编译方法和vs2010中导入第三方库的方法 一.去相应官网下载json.cpp文件 Jsoncpp下载:https://sourceforge.net/projects/jsoncpp/  ...

  5. iOS第三方开源库的吐槽和备忘

    转自:http://blog.ibireme.com/2013/09/23/ios-third-party-libs/#more-41361 由 ibireme 发表于 2013/09/23 做iOS ...

  6. linux下使用C++ Json库

    安装Json库 1.下载JsonCpphttp://sourceforge.net/projects/jsoncpp/files/ 2.下载sconshttp://sourceforge.net/pr ...

  7. C++ Json工具--Jsoncpp用法简介

    文章目录 Json简介 用法简介 数据类型 C++代码示例 代码执行输出结果 JSON在线解析及格式化验证 - JSON.cn Json简介 JSON(JavaScript Object Notati ...

  8. 快速上手Unity原生Json库

    现在新版的Unity(印象中是从5.3开始)已经提供了原生的Json库,以前一直使用LitJson,研究了一下Unity用的JsonUtility工具类的使用,发现使用还挺方便的,所以打算把项目中的J ...

  9. Java 下的 JSON库性能比较:JSON.simple

    JSON已经成为当前服务器与WEB应用之间数据传输的公认标准,不过正如许多我们所习以为常的事情一样,你会觉得这是理所当然的便不再深入思考了.我们很少会去想用到的这些JSON库到底有什么不同,但事实上它 ...

随机推荐

  1. linux配置tns

    .三个配置文件都是放在$ORACLE_HOME\network\admin目录下. .sqlnet.ora确定解析方式 .listener.ora上设SID_NAME,通常用于JDBC访问,对应的错误 ...

  2. Java基础学习(学习IT企业必读的324个JAVA面试题.pdf 整理)

    一.Java程序基础 javac 文件名.java    编译程序 java 类名               运行java程序 代码规范中,一下几点要注意: 包名:包名是全小写的名词,中间可以由点分 ...

  3. TCP/IP详解学习笔记(4)-- ARP 和 RARP

    1.ARP      地址解析协议(Address Resolution Protocol,ARP)是在仅知道主机的IP地址时确地址解析协议定其物理地址的一种协议.      在TCP/IP协议中,A ...

  4. 【EF 4】ORM框架及其流行产品之一EF介绍

    导读:跳进了多租户切换数据库的坑,那么就继续走下去吧.在我们的项目中,是运用EF实现对数据库的操作,那么EF其实是.NET系统中,基于ORM框架的一个产品实现.在java那边,则有Hibernate和 ...

  5. Duilib学习笔记《05》— 消息响应处理

    在Duilib学习笔记<04>中已经知道了如何将窗体显示出来,而如何处理窗体上的事件.消息呢? 一. 系统消息 窗体显示的时候我们就已经说了,窗体是继承CWindowWnd类的,对于窗体的 ...

  6. firefox chrome强制指定网址使用https

    chrome强制网站使用httpschrome://net-internals/firefox强制指定网站使用https使用noScript插件

  7. [zt]Which are the 10 algorithms every computer science student must implement at least once in life?

    More important than algorithms(just problems #$!%), the techniques/concepts residing at the base of ...

  8. SQL Server 数据库基础编程

    Ø Go批处理语句 用于同时执行多个语句 Ø 使用.切换数据库 use master go     Ø 创建.删除数据库   方法1.   --判断是否存在该数据库,存在就删除 if (exists ...

  9. SpringData JPA 排除 扫描 exclude-filter 不能使用解决

    在加上JPA的配置外,还需要再加上 : xmlns:repository="http://www.springframework.org/schema/data/repository&quo ...

  10. Cent OS 6 主机名设置

    1:查看主机名: #hostname 2:修改主机名: #vi /etc/sysconfig/network ETWORKING=yes HOSTNAME=superboy.com 3:重启生效: # ...