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 ...
随机推荐
- Django中自定义模型管理器(Manager)及方法
1.自定义管理器(Manager) 在语句Book.objects.all()中,objects是一个特殊的属性,通过它来查询数据库,它就是模型的一个Manager.每个Django模型至少有一个ma ...
- abp(net core)+easyui+efcore实现仓储管理系统——EasyUI之货物管理一 (十九)
abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...
- php接受的post数据类型
通常情况下用户使用浏览器网页表单向服务器post提交数据,我们使用PHP的$_POST接收用户POST到服务器的数据,并进行适当的处理.但有些情况下,如用户使用客户端软件向服务端php程序发送post ...
- webstorm中关闭烦人Eslint语法检查
打开许久没打开的webstrom,以前关闭的配置不知道怎么又乱了,react项目到处报错,真是没法忍. 关闭eslint位置:File-->Setting-->Languages& ...
- 从零开始使用 Webpack 搭建 Vue 开发环境
创建项目 先创建一个空目录,在该目录打开命令行,执行 npm init 命令创建一个项目(无法执行 npm 命令?需要先安装 Node),这个过程会提示输入一些内容,随意输入就行,完成后会自动生成一个 ...
- Python学习笔记整理总结【Django】【MVC/MTV/路由分配系统(URL)/视图函数 (views)/表单交互】
一.Web框架概述 Web框架本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. #!/usr/bin/env python # -*- coding:utf-8 ...
- Entity Framework Core生成的存储过程在MySQL中需要进行处理及PMC中的常用命令
在使用Entity Framework Core生成MySQL数据库脚本,对于生成的存储过程,在执行的过程中出现错误,需要在存储过程前面添加 delimiter // 附:可以使用Visual Stu ...
- Spring boot 官网学习笔记 - Spring Boot CLI 入门案例
安装CLI https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.1.1.RELEASE/spring-b ...
- springboot系列之01-产生的背景及其优势
未经允许,不得转载 原作者:字母哥博客 本文完整系列出自:springboot深入浅出系列 一.前置说明 本节大纲 spring boot 诞生的背景 Spring boot 改变了什么 Spring ...
- 织梦cms列表页获取标签
<!-- 标签 --> [field:id runphp='yes'] global $cfg_cmspath; $tags = GetTags(@me); $revalue = ''; ...