首页 > 编程语言 >C++ json库jsoncpp 吐槽

C++ json库jsoncpp 吐槽

时间:2023-02-21 09:56:18浏览次数:57  
标签:name Json C++ sex json jsoncpp tempVal string

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: }

标签:name,Json,C++,sex,json,jsoncpp,tempVal,string
From: https://www.cnblogs.com/kn-zheng/p/17139896.html

相关文章

  • Jni中C++和Java的参数传递
    如何使用JNI的一些基本方法和过程在网上多如牛毛,如果你对Jni不甚了解,不知道Jni是做什么的,如何建立一个基本的jni程序,或许可以参考下面下面这些文章:利用VC++6.0实现JNI的最......
  • C++输出文件名、函数名、行号
    11std::cout<<"filepath=%s"<<__FILE__;//源文件名22std::cout<<"functionname=%s"<<__FUNCTION__;//函数名称33std::c......
  • C++数组
    C++一维数组C++数组的定义方式数据类型数组名[数组长度];例子:intarr[3];arr[0]=1;arr[1]=2;arr[2]=3;数据类型数组名[数组长度]=intarr[3]=......
  • spring中处理json
    1.使用fastJson2.controller层@PostMapping("")@ResponseBodypublicBaseResponsejsonFunction(@RequestBodyStringjsonData){BaseResponsebaseResponse=ne......
  • c++学习
      c++字符串转化为整数浮点数。   string和char直接转换============31m代表字体为红色,0m代表关闭所有属性。常用的ANSI控制码如下(有些不支持):\033[0m关闭所......
  • 树状数组板子C++
    1intn;2inta[1005],c[1005];//对应原数组和树状数组34intlowbit(intx){5returnx&(-x);6}78voidupdata(inti,intk){//在i位置加......
  • 【数组与链表算法】矩阵算法在程序中常见的简单应用 | C++
    第二十三章矩阵算法:::hljs-center目录第二十三章矩阵算法●前言●矩阵算法与深度学习●一、矩阵相加●二、矩阵相乘●三、矩阵转置●四、稀疏矩阵●......
  • C++学习-const
    1,定义常量​ A,const与#define的区别:​ a,const常量具有类型,编译器可以进行安全检查,#define没有类型,只是简单替换字符串​ b,const只能定义整数或枚举的常量2,const......
  • 前端Blob数据流转JSON格式
    blobToJson=(blobData)=>{returnnewPromise((resolve,reject)=>{constreader:any=newFileReader()letjsonData:anyreader.readAsText(b......
  • 项目中TS的配置文件tsconfig.json
    1.快速搭建一个浏览器开发环境建立好文件夹后,打开VSCode,把文件夹拉到编辑器当中,然后打开终端,运行npminit-y,创建package.json文件。生成文件后,我们接着在终端中运行t......