RapidJSON的一些方便操作的宏定义

以前是一直使用CCJsonConventer去把JSON字符串转化为CCDictionary对象的,现在使用cocos2d-x 3.x后,不推荐使用CCDictionary了,而且,JSON库也换成了rapidjson,不过我暂时没找到好的封装,如果仅仅是用作数据提取的话,做几个宏定义就可以达到目的了。

/*
 * =====================================================================================
 *
 *       Filename:  RapidJsonMacro.h
 *
 *    Description:  Easy macro to operate RapidJSON
 *
 *        Version:  1.0
 *        Created:  12/26/2014 11:38:23
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Jason Tou (), sosoayaen@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */

#pragma once

#include "json/rapidjson.h"
#include "json/document.h"

// 基础变量的校验
#define json_check_is_bool(value, strKey) (value.HasMember(strKey) && value[strKey].IsBool())
#define json_check_is_string(value, strKey) (value.HasMember(strKey) && value[strKey].IsString())
#define json_check_is_int32(value, strKey) (value.HasMember(strKey) && value[strKey].IsInt())
#define json_check_is_uint32(value, strKey) (value.HasMember(strKey) && value[strKey].IsUint())
#define json_check_is_int64(value, strKey) (value.HasMember(strKey) && value[strKey].IsInt64())
#define json_check_is_uint64(value, strKey) (value.HasMember(strKey) && value[strKey].IsUint64())
#define json_check_is_float(value, strKey) (value.HasMember(strKey) && value[strKey].IsFloat())
#define json_check_is_double(value, strKey) (value.HasMember(strKey) && value[strKey].IsDouble())

#define json_check_is_number(value, strKey) (value.HasMember(strKey) && value[strKey].IsNumber())
#define json_check_is_array(value, strKey) (value.HasMember(strKey) && value[strKey].IsArray())

// 得到对应类型的数据,如果数据不存在则得到一个默认值
#define json_check_bool(value, strKey) (json_check_is_bool(value, strKey) && value[strKey].GetBool())
#define json_check_string(value, strKey) (json_check_is_string(value, strKey) ? value[strKey].GetString() : "")
#define json_check_int32(value, strKey) (json_check_is_int32(value, strKey) ? value[strKey].GetInt() : 0)
#define json_check_uint32(value, strKey) (json_check_is_uint32(value, strKey) ? value[strKey].GetUint() : 0)
#define json_check_int64(value, strKey) (json_check_is_int64(value, strKey) ? ((value)[strKey]).GetInt64() : 0)
#define json_check_uint64(value, strKey) (json_check_is_uint64(value, strKey) ? ((value)[strKey]).GetUint64() : 0)
#define json_check_float(value, strKey) (json_check_is_float(value, strKey) ? ((value)[strKey]).GetFloat() : 0)
#define json_check_double(value, strKey) (json_check_is_double(value, strKey) ? ((value)[strKey]).GetDouble() : 0)

// 得到Value指针
#define json_check_value_ptr(value, strKey) (((value).HasMember(strKey)) ? &((value)[strKey]) : nullptr)

使用上也比较简单,比如获取一个字符串:

void getName(const rapidjson::Value& data)
{
    std::string strName = json_check_string(data, "name");
}
不过这里仅仅是罗列了只读数据的操作,写操作还没搞,貌似json数据在客户端还是解析的操作比较多,以后用到再总结吧

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用说明: 1.易语言模块和使用例子在Release目录下 2.为了节省打包大小,删除了Visual Studio的配置,重新编译的话 选择Release x86即可 3.官方库若有更新可直接替换include文件夹,重新编译即可(官方git: https://github.com/Tencent/rapidjson/)(官方文档: http://rapidjson.org/zh-cn/) 封装日志: 1.0.0.9版-2019.5.6 1) 升级 rapidjson库到官方最新版本(2019.4.15) 1.0.0.8版-2018.11.22 1)  修复 gstrlen函数 pop顺序错误问题. 2)  修复 win10环境下【SAX解析】路径深度到达3时,路径未以0结尾问题. 1.0.0.7版-2018.11.17 1)  修复 NumConversion.h中 StrToInt64函数 转换异常问题。(所有取长整数值,若类型是文本型,自动转换时会调用该函数) 2)  升级 rapidjson库到官方最新版本(2018.10.8) 1.0.0.6版-2018.10.8 1)  修复 rapidjson_dll_ec.e RJ生成W.创建对象和RJ生成W.创建数组 键名为空时,生成异常问题 2)  优化 取数值时,若为文本型,则强转为对应数值返回. 3)  添加 通配_取xx值配置 系列 (作用:取值,需要提供一个默认值,若节点存在则返回节点值,不存在则添加默认值) 4)  添加 通配_置xx值 系列 (作用:可多路径生成json) 5)  添加 pointer_erase_path 函数 (作用:删除某个节点) 6)  添加 pointer_is_exist 函数 (作用: 查询节点是否存在) 7)  添加 几个性能优化过的辅助函数,实现在rapidjson_dll_ec.e(辅助功能) 8)  封装 zlib部分解压缩功能,实现在auxiliary.cpp 9)  更新 易语言模块和使用例子 1.0.0.5版-2018.9.26 1)  添加SAX解析方式,实现在sax.cpp 2)  同步更新使用例子(rapidjson.e) 1.0.0.4版-2018.9.9 1)  修复解析时传入空指针导致奔溃问题 2)  修复一些隐患 3)  增加object_get_key函数(取对象成员键名) 4)  增加double_to_string函数(双精度到文本 Grisu2算法),实现在auxiliary.cpp 5)  同步更新易语言模块和使用例子 1.0.0.3版-2018.8.30 1)  修复object_get_int和get_path_type返回错误问题(测试的时候加了个取字符串长度的代码,忘记删掉了- -)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值