json::rapidjson工具
源码地址: https://github.com/Tencent/rapidjson
可跨平台使用。
将 rapidjson-master\include\rapidjson 中的 rapidjson 文件夹添加到 项目中 即可。
#pragma once
#include <type_traits>
#include <rapidjson/error/en.h>
#include <rapidjson/document.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/prettywriter.h>
#include <rapidjson/reader.h>
#include <rapidjson/error/en.h> #ifndef _WIN32
template<bool _Test,
class _Ty = void>
using enable_if_t = typename std::enable_if<_Test, _Ty>::type;
#else
using std::enable_if_t;
#endif template<typename T>
enable_if_t<(std::is_same<std::string, T>::value || std::is_same<const char*, T>::value), bool>
safe_get_json_val(const rapidjson::Value& v, T& r)
{
if (!v.IsString())
return false; r = v.Get<T>();
return true;
} template<typename T>
enable_if_t<std::is_same<int, T>::value, bool>
safe_get_json_val(const rapidjson::Value& v, T& r)
{
if (!v.IsInt())
return false; r = v.Get<T>();
return true;
} template<typename T>
enable_if_t<std::is_same<int64_t, T>::value, bool>
safe_get_json_val(const rapidjson::Value& v, T& r)
{
if (!v.IsInt64())
return false; r = v.Get<T>();
return true;
} template<typename T>
enable_if_t<std::is_same<uint32_t, T>::value, bool>
safe_get_json_val(const rapidjson::Value& v, T& r)
{
if (!v.IsUint())
return false; r = v.Get<T>();
return true;
} template<typename T>
enable_if_t<std::is_same<uint16_t, T>::value, bool>
safe_get_json_val(const rapidjson::Value& v, T& r)
{
if (!v.IsUint())
return false; r = v.Get<uint32_t>();
return true;
} template<typename T>
enable_if_t<std::is_same<int16_t, T>::value, bool>
safe_get_json_val(const rapidjson::Value& v, T& r)
{
if (!v.IsInt())
return false; r = v.Get<int>();
return true;
} template<typename T>
enable_if_t<std::is_same<uint64_t, T>::value, bool>
safe_get_json_val(const rapidjson::Value& v, T& r)
{
if (!v.IsUint64())
return false; r = v.Get<T>();
return true;
} template<typename T>
enable_if_t<std::is_same<double, T>::value, bool>
safe_get_json_val(const rapidjson::Value& v, T& r)
{
if (!v.IsDouble())
return false; r = v.Get<T>();
return true;
} template<typename T>
enable_if_t<std::is_same<float, T>::value, bool>
safe_get_json_val(const rapidjson::Value& v, T& r)
{
if (!v.IsFloat())
return false; r = v.Get<T>();
return true;
} template<typename T>
enable_if_t<std::is_same<rapidjson::Value::Object, T>::value, bool>
safe_get_json_val(const rapidjson::Value& v, T& r)
{
if (!v.IsObject())
return false; r = v.Get<T>();
return true;
} template<typename T>
enable_if_t<std::is_same<rapidjson::Value*, T>::value, bool>
safe_get_json_val(rapidjson::Value& v, T& r)
{
r = &v;
return true;
} template<typename T>
enable_if_t<std::is_same<rapidjson::Value::Array, T>::value, bool>
safe_get_json_val(const rapidjson::Value& v, T& r)
{
if (!v.IsArray())
return false; r = v.Get<T>();
return true;
} template<typename T>
bool safe_get_json_member(rapidjson::Value& v, const char* field, T& r)
{
if (!v.HasMember(field))
return false; return safe_get_json_val(v[field], r);
}
std::string Json2str()
{
std::lock_guard<mutex> lck(m_mx);
Document doc;
doc.SetObject(); Document::AllocatorType& allocator = doc.GetAllocator(); Value base(rapidjson::kObjectType); base.AddMember("SnapPicturePath", StringRef(m_bc.SnapPicturePath.c_str()), allocator);
base.AddMember("vehThreadNum", StringRef(m_bc.vehThreadNum.c_str()), allocator);
base.AddMember("vehUrl", StringRef(m_bc.vehUrl.c_str()), allocator);
base.AddMember("cmsUrl", StringRef(m_bc.cmsUrl.c_str()), allocator); doc.AddMember("base", base, allocator);
rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer); return buffer.GetString();
}
bool str2json(const string str, string& outHexImage, string& confidence)
{
Document doc;
doc.Parse<>(json.c_str());
if (doc.HasParseError()) {
return false;
} Value* pValue = nullptr;
Value* Value = nullptr;
if (safe_get_json_member(doc, "result", pValue))
{
if(safe_get_json_member(*pValue, "image", Value))
outHexImage = Value->GetString();
if(safe_get_json_member(*pValue, "confidence", Value))
confidence = Value->GetString();
}
return true;
}
json::rapidjson工具的更多相关文章
- Json解析工具Jackson(使用注解)
原文http://blog.csdn.net/nomousewch/article/details/8955796 接上一篇文章Json解析工具Jackson(简单应用),jackson在实际应用中给 ...
- Android进阶笔记17:3种JSON解析工具(org.json、fastjson、gson)
一. 目前解析json有三种工具:org.json(Java常用的解析),fastjson(阿里巴巴工程师开发的),Gson(Google官网出的),其中解析速度最快的是Gson. 3种json工具下 ...
- 在线的JSON formate工具
一个非常好的json formate工具 可以很容易发现json的错误,以及对json进行格式化 https://jsonformatter.curiousconcept.com/
- json 帮助工具
import java.lang.reflect.Type; import com.google.gson.Gson; /** * json 帮助工具 */public final class Gso ...
- JSON 解析工具的封装(Java)
JSON 解析工具的封装(Java) 一直想有一个属于自己的JSON工具,今天终于弄好了..... 1.添加pom依赖包 <!--json解析--> <dependency> ...
- 自定义Json解析工具
此博客为博主原创文章,转载请标明出处,维权必究:https://www.cnblogs.com/tangZH/p/10689536.html fastjson是很好用的json解析工具,只可惜项目中要 ...
- java后台常用json解析工具问题小结
若排版紊乱可查看我的个人博客原文地址 java后台常用json解析工具问题小结 这里不细究造成这些问题的底层原因,只是单纯的描述我碰到的问题及对应的解决方法 jackson将java对象转json字符 ...
- Json转换工具
import java.util.List; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterx ...
- Json转换工具类(基于google的Gson和阿里的fastjson)
在项目之中我们经常会涉及到字符串和各种对象的转换,为此特地整理了一下常用的转换方法 一.基于com.google.code.gson封装的json转换工具类 1. 在pom.xml文件里面引入gson ...
随机推荐
- .NET Conf 2019 大会上发布.NET Core 3.0
北京时间今天凌晨如期在.NET Conf 上发布.NET Core 3.0,Keynotes 由Scott Hunter 主演,主要围绕.NET Core 3.0的新特性和社区展开. 多功能性是.Ne ...
- DataGuard开启failover
1.修改保护模式 DGMGRL> edit configuration set protection mode as maxAvailability; Succeeded. 2.修改日志同步方式 ...
- Cocos Creator实现左右跳游戏
1. 玩法说明 游戏开始后,点击屏幕左右两侧,机器人朝左上方或右上方跳一步,如果下一步有石块,成功得1分,否则游戏结束. 2. 模块介绍 游戏场景分为2个:主页场景(home).游戏场景(game) ...
- 利用Python进行数据分析:【Matplotlib】
一.简单介绍Matplotlib 1.Matplotlib是一个强大的Python绘图和数据可视化的工具包2.安装方法:pip install matplotlib 3.引用方法:import mat ...
- Ubuntu18.04安装PHP7.3
因为近期需要做毕业设计,需要用到Linux系统,在此分享一下在Linux-Ubuntu系统下安装PHP环境的一小点知识,如有偏差错误的,请各位学友多多指教哈! sudo apt-get install ...
- Spring 梳理-MVC-配置DispatcherServet和ContextLoaderListener
在使用JavaConfig时,AbstractAnnotationConfigDispatcherServletInitializer会自动注册 DispatcherServlet 和 Context ...
- js三级联动效果city-picker
链接:https://pan.baidu.com/s/1NE_EO5_xGvR-y-lboYap7g 提取码:h00e 效果展示: 解决: 动态赋值: 注意:在执行赋值之前,必须执行reset和des ...
- java架构之路-(SpringMVC篇)SpringMVC主要流程源码解析(下)注解配置,统一错误处理和拦截器
我们上次大致说完了执行流程,也只是说了大致的过程,还有中间会出错的情况我们来处理一下. 统一异常处理 比如我们的运行时异常的500错误.我们来自定义一个类 package com.springmvcb ...
- [Scrapy] Some things about Scrapy
1. Pause and resume a crawl Scrapy supports this functionality out of the box by providing > the ...
- ELK 学习笔记之 Logstash基本语法
Logstash基本语法: 处理输入的input 处理过滤的filter 处理输出的output 区域 数据类型 条件判断 字段引用 区域: Logstash中,是用{}来定义区域 区域内,可以定义插 ...