json::rapidjson工具
阅读原文时间:2023年07月11日阅读:1

源码地址: https://github.com/Tencent/rapidjson
可跨平台使用。
将 rapidjson-master\include\rapidjson 中的 rapidjson 文件夹添加到 项目中 即可。

#pragma once
#include
#include
#include
#include
#include
#include
#include

#ifndef _WIN32
template
using enable_if_t = typename std::enable_if<_Test, _Ty>::type;
#else
using std::enable_if_t;
#endif

template
enable_if_t<(std::is_same::value || std::is_same::value), bool>
safe_get_json_val(const rapidjson::Value& v, T& r)
{
if (!v.IsString())
return false;

r = v.Get<T>();  
return true;  

}

template
enable_if_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
enable_if_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
enable_if_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
enable_if_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
enable_if_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
enable_if_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
enable_if_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
enable_if_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
enable_if_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
enable_if_t::value, bool>
safe_get_json_val(rapidjson::Value& v, T& r)
{
r = &v;
return true;
}

template
enable_if_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
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 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;  

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章