#ifndef __TZ_JSON_H #define __TZ_JSON_H // rapidjson #include "rapidjson/document.h" #include "rapidjson/prettywriter.h" #include "rapidjson/stringbuffer.h" // std #include #include #include namespace tzc { namespace json { typedef rapidjson::Document JsonDoc; typedef rapidjson::Value JsonValue; template std::string ValueToJson(T & doc) { rapidjson::StringBuffer sb; rapidjson::PrettyWriter writer( sb); doc.Accept(writer); return sb.GetString(); } /** * @brief base type to json */ template inline bool setFiled(JsonDoc & doc, const std::string & fildName, const T & value) { auto & allocator = doc.GetAllocator(); doc.AddMember(JsonValue(fildName.c_str(), allocator), JsonValue(value), allocator); return true; } inline bool setFiled(JsonDoc & doc, const std::string & fildName, const std::string & value) { auto & allocator = doc.GetAllocator(); doc.AddMember(JsonValue(fildName.c_str(), allocator), JsonValue(value.c_str(), allocator), allocator); return true; } /** * @brief base type from json */ template inline T getFiled(const JsonDoc & doc, const std::string & fildName, const T & defaultValue) { const char * pfildName = fildName.c_str(); JsonValue::ConstMemberIterator itr = doc.FindMember(pfildName); if (itr != doc.MemberEnd()) { if (itr->value.Is()) { return itr->value.Get(); } } return defaultValue; } inline float getFiled(const JsonDoc & doc, const std::string & fildName, const float & defaultValue) { const char * pfildName = fildName.c_str(); JsonValue::ConstMemberIterator itr = doc.FindMember(pfildName); if (itr != doc.MemberEnd()) { if (itr->value.IsFloat()) { return itr->value.Get(); } else if (itr->value.IsInt()) { auto iValue = itr->value.Get(); return (float)iValue; } } return defaultValue; } inline double getFiled(const JsonDoc & doc, const std::string & fildName, const double & defaultValue) { const char * pfildName = fildName.c_str(); JsonValue::ConstMemberIterator itr = doc.FindMember(pfildName); if (itr != doc.MemberEnd()) { if (itr->value.IsDouble()) { return itr->value.Get(); } else if (itr->value.IsInt()) { auto iValue = itr->value.Get(); return (double)iValue; } } return defaultValue; } inline std::string getFiled(const JsonDoc & doc, const std::string & fildName, const std::string & defaultValue) { const char * pfildName = fildName.c_str(); JsonValue::ConstMemberIterator itr = doc.FindMember(pfildName); if (itr != doc.MemberEnd() && itr->value.IsString()) { return itr->value.GetString(); } else { return defaultValue; } } inline std::string getFiled(const JsonDoc & doc, const std::string & fildName, const char * defaultValue) { const char * pfildName = fildName.c_str(); JsonValue::ConstMemberIterator itr = doc.FindMember(pfildName); if (itr != doc.MemberEnd() && itr->value.IsString()) { return itr->value.GetString(); } else { return defaultValue; } } /** * @brief object type to json */ template inline bool setFiledObject(JsonDoc & doc, const std::string & fildName, const T & value) { auto & allocator = doc.GetAllocator(); JsonDoc inner(&allocator); inner.Parse(value.toJson().c_str()); if (inner.HasParseError()) { return false; } doc.AddMember( JsonValue(fildName.c_str(), allocator), inner, allocator); return true; } /** * @brief object type from json */ template inline bool ConverObject(JsonDoc & document, const char * name, T & value) { JsonValue::ConstMemberIterator itr = document.FindMember(name); if (itr != document.MemberEnd()) { if (!itr->value.IsObject()) { return false; } return T::fromJson(ValueToJson(itr->value), value); } return false; } /** * @brief base type array to json */ template inline bool setFiledArray(JsonDoc & doc, const std::string & fildName, const T & collection) { auto & allocator = doc.GetAllocator(); JsonDoc inner(&allocator); inner.SetArray(); for (typename T::const_iterator it = collection.begin(); it != collection.end(); ++it) { JsonValue ele(*it); inner.PushBack(ele, allocator); } doc.AddMember(JsonValue(fildName.c_str(), allocator), inner, allocator); return true; } inline bool setFiledArray(JsonDoc & doc, const std::string & fildName, const std::vector & collection) { auto & allocator = doc.GetAllocator(); JsonDoc inner(&allocator); inner.SetArray(); for (std::vector::const_iterator it = collection.begin(); it != collection.end(); ++it) { JsonValue ele; ele.SetString(it->c_str(), (rapidjson::SizeType)it->size(), allocator); inner.PushBack(ele, allocator); } doc.AddMember(JsonValue(fildName.c_str(), allocator), inner, allocator); return true; } inline bool setFiledArray(JsonDoc & doc, const std::string & fildName, const std::list & collection) { std::vector tmpcollection { collection.begin(), collection.end() }; return setFiledArray(doc, fildName, tmpcollection); } /** * @brief base type array from json */ template inline bool ConverArray(JsonDoc & document, const char * name, T & value) { JsonValue::ConstMemberIterator itr = document.FindMember(name); if (itr != document.MemberEnd()) { if (!itr->value.IsArray()) { return false; } for (auto & v : itr->value.GetArray()) { value.push_back(v.Get()); } return true; } return false; } inline bool ConverArray(JsonDoc & document, const char * name, std::vector & value) { JsonValue::ConstMemberIterator itr = document.FindMember(name); if (itr != document.MemberEnd()) { if (!itr->value.IsArray()) { return false; } for (auto & v : itr->value.GetArray()) { if (v.IsString()) { value.push_back(v.GetString()); } } return true; } return false; } inline bool ConverArray(JsonDoc & document, const char * name, std::list & value) { JsonValue::ConstMemberIterator itr = document.FindMember(name); if (itr != document.MemberEnd()) { if (!itr->value.IsArray()) { return false; } for (auto & v : itr->value.GetArray()) { if (v.IsString()) { value.push_back(v.GetString()); } } return true; } return false; } /** * @brief object type array to json */ template inline bool setFiledArrayObject(JsonDoc & doc, const std::string & fildName, const T & collection) { auto & allocator = doc.GetAllocator(); JsonDoc inner(&allocator); inner.SetArray(); for (typename T::const_iterator it = collection.begin(); it != collection.end(); ++it) { std::string json = it->toJson(); JsonDoc ele(&allocator); ele.Parse(json.c_str()); if (ele.HasParseError()) { continue; } inner.PushBack(ele, allocator); } doc.AddMember(JsonValue(fildName.c_str(), allocator), inner, allocator); return true; } /** * @brief object type array from json */ template inline bool ConverArrayObject( JsonDoc & document, const char * name, T & value) { JsonValue::ConstMemberIterator itr = document.FindMember(name); if (itr != document.MemberEnd()) { if (!itr->value.IsArray()) { return false; } for (auto & v : itr->value.GetArray()) { typename T::value_type ele; T::value_type::fromJson(ValueToJson(v), ele); value.push_back(ele); } return true; } return false; } /** * @brief base type bare array to json */ template inline std::string BaseArrayToJson(const T & collection) { JsonDoc doc; auto & allocator = doc.GetAllocator(); doc.SetArray(); for (typename T::const_iterator it = collection.begin(); it != collection.end(); ++it) { JsonValue value(*it); doc.PushBack(value, allocator); } return ValueToJson(doc); } inline std::string BaseArrayToJson( const std::vector & collection) { JsonDoc doc; auto & allocator = doc.GetAllocator(); doc.SetArray(); for (auto it = collection.begin(); it != collection.end(); ++it) { JsonValue ele; ele.SetString(it->c_str(), (rapidjson::SizeType)it->size(), allocator); doc.PushBack(ele, allocator); } return ValueToJson(doc); } inline std::string BaseArrayToJson( const std::list & collection) { std::vector tmpcollection { collection.begin(), collection.end() }; return BaseArrayToJson(tmpcollection); } /** * @brief base type bare array from json */ template inline bool JsonToBaseArray( JsonDoc & document, std::vector & collections) { if (!document.IsArray()) { return false; } for (auto & val : document.GetArray()) { if (!val.Is()) { continue; } collections.push_back(val.Get()); } return true; } template inline bool JsonToBaseArray(JsonDoc & document, std::list & collections) { if (!document.IsArray()) { return false; } for (auto & val : document.GetArray()) { if (!val.Is()) { continue; } collections.emplace_back(val.Get()); } return true; } inline bool JsonToBaseArray( JsonDoc & document, std::vector & collections) { if (!document.IsArray()) { return false; } for (auto & val : document.GetArray()) { if (!val.IsString()) { continue; } collections.emplace_back(val.GetString()); } return true; } inline bool JsonToBaseArray( JsonDoc & document, std::list & collections) { if (!document.IsArray()) { return false; } for (auto & val : document.GetArray()) { if (!val.IsString()) { continue; } collections.emplace_back(val.GetString()); } return true; } /** * @brief object type bare array to json */ template inline std::string ArrayToJson(const T & collection) { JsonDoc doc; auto & allocator = doc.GetAllocator(); doc.SetArray(); for (typename T::const_iterator it = collection.begin(); it != collection.end(); ++it) { std::string json = it->toJson(); JsonDoc ele(&allocator); ele.Parse(json.c_str()); if (ele.HasParseError()) { continue; } doc.PushBack(ele, allocator); } return ValueToJson(doc); } /** * @brief object type bare array to json */ template inline bool JsonToArray(JsonDoc & document, std::vector & collections) { if (!document.IsArray()) { return false; } for (auto & val : document.GetArray()) { if (!val.IsObject()) { continue; } T value; if (!T::fromJson(ValueToJson(val), value)) { continue; } collections.push_back(value); } return true; } template inline bool JsonToArray(JsonDoc & document, std::list & collections) { if (!document.IsArray()) { return false; } for (auto & val : document.GetArray()) { if (!val.IsObject()) { continue; } T value; if (!T::fromJson(ValueToJson(val), value)) { continue; } collections.push_back(value); } return true; } /** * 以下函数为TzJson新增函数 用于处理不定类型的成员json转换 * 并且重载了vector和list的容器的两个版本 */ // integer toJson and fromJson inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const int & value) { return tzc::json::setFiled(doc, fildName, value); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const unsigned int & value) { return tzc::json::setFiled(doc, fildName, value); } /** * 针对长整型,强烈建议使用标准库定义的平台通用类型 int64_t 和 uint64_t * * !! 注意: * 类型int64_t和类型uint64_t 在Linux系统和Windows系统中定义不同 * Linux Windows * int64_t long long long * uint64_t unsigned long unsigned long long * * rapidjson中采用了类型int64_t和类型uint64_t * * common库中 实质为 long的类型有 TZ_ULONG TZ_LONG TZ_SLONG, * 这些类型的变量 在Windows系统中 无法json转换 * * common库中 实质为 long long的类型有 * TZ_ULONGLONG TZ_LONGLONG TZ_SLONGLONG TZ_Uint64 TZ_Int64, * 这些类型的变量 在Linux系统中 无法json转换 * * 在 TZJson 中, 完全兼容 longlong 类型。 * * long类型在Linux和Windows中大小不一致,不考虑使用 */ inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const int64_t & value) { return tzc::json::setFiled(doc, fildName, value); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const uint64_t & value) { return tzc::json::setFiled(doc, fildName, value); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const float & value) { return tzc::json::setFiled(doc, fildName, value); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const double & value) { return tzc::json::setFiled(doc, fildName, value); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const bool & value) { return tzc::json::setFiled(doc, fildName, value); } // string toJson inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::string & value) { return tzc::json::setFiled(doc, fildName, value); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const char * value) { return tzc::json::setFiled(doc, fildName, std::string(value)); } // class / struct toJson template inline bool setFiledParams(JsonDoc & doc, const std::string & fildName, const T & value) { return tzc::json::setFiledObject(doc, fildName, value); } // container number (arrar number) toJson inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::vector & value) { return tzc::json::setFiledArray(doc, fildName, value); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::list & value) { std::vector vecValue(value.begin(), value.end()); return tzc::json::setFiledArray(doc, fildName, vecValue); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::vector & value) { return tzc::json::setFiledArray(doc, fildName, value); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::list & value) { std::vector vecValue(value.begin(), value.end()); return tzc::json::setFiledArray(doc, fildName, vecValue); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::vector & value) { return tzc::json::setFiledArray(doc, fildName, value); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::list & value) { std::vector vecValue(value.begin(), value.end()); return tzc::json::setFiledArray(doc, fildName, vecValue); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::vector & value) { return tzc::json::setFiledArray(doc, fildName, value); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::list & value) { std::vector vecValue(value.begin(), value.end()); return tzc::json::setFiledArray(doc, fildName, vecValue); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::vector & value) { return tzc::json::setFiledArray(doc, fildName, value); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::list & value) { std::vector vecValue(value.begin(), value.end()); return tzc::json::setFiledArray(doc, fildName, vecValue); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::vector & value) { return tzc::json::setFiledArray(doc, fildName, value); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::list & value) { std::vector vecValue(value.begin(), value.end()); return tzc::json::setFiledArray(doc, fildName, vecValue); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::vector & value) { return tzc::json::setFiledArray(doc, fildName, value); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::list & value) { std::vector vecValue(value.begin(), value.end()); return tzc::json::setFiledArray(doc, fildName, vecValue); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::vector & value) { return tzc::json::setFiledArray(doc, fildName, value); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::list & value) { std::vector vecValue(value.begin(), value.end()); return tzc::json::setFiledArray(doc, fildName, vecValue); } // class / struct array toJson template inline bool setFiledParams(JsonDoc & doc, const std::string & fildName, const std::list & value) { return tzc::json::setFiledArrayObject(doc, fildName, value); } template inline bool setFiledParams(JsonDoc & doc, const std::string & fildName, const std::vector & value) { return tzc::json::setFiledArrayObject(doc, fildName, value); } // integer toJson and fromJson inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, int & value) { value = tzc::json::getFiled(doc, fildName, (int)(0)); return true; } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, unsigned int & value) { value = tzc::json::getFiled(doc, fildName, (unsigned int)(0)); return true; } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, int64_t & value) { value = tzc::json::getFiled(doc, fildName, (int64_t)(0)); return true; } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, uint64_t & value) { value = tzc::json::getFiled(doc, fildName, (uint64_t)(0)); return true; } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, float & value) { value = tzc::json::getFiled(doc, fildName, (float)(0)); return true; } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, double & value) { value = tzc::json::getFiled(doc, fildName, (double)(0)); return true; } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, bool & value) { value = tzc::json::getFiled(doc, fildName, false); return true; } // string fromJson inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::string & value) { value = tzc::json::getFiled(doc, fildName, ""); return true; } // class / struct fromJson template inline bool getFiledParams(JsonDoc & doc, const std::string & fildName, T & value) { return tzc::json::ConverObject(doc, fildName.c_str(), value); } // number array fromJson inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::list & value) { return tzc::json::ConverArray(doc, fildName.c_str(), value); } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::vector & value) { return tzc::json::ConverArray(doc, fildName.c_str(), value); } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::list & value) { return tzc::json::ConverArray(doc, fildName.c_str(), value); } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::vector & value) { return tzc::json::ConverArray(doc, fildName.c_str(), value); } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::list & value) { return tzc::json::ConverArray(doc, fildName.c_str(), value); } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::vector & value) { return tzc::json::ConverArray(doc, fildName.c_str(), value); } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::list & value) { return tzc::json::ConverArray(doc, fildName.c_str(), value); } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::vector & value) { return tzc::json::ConverArray(doc, fildName.c_str(), value); } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::list & value) { return tzc::json::ConverArray(doc, fildName.c_str(), value); } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::vector & value) { return tzc::json::ConverArray(doc, fildName.c_str(), value); } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::list & value) { return tzc::json::ConverArray(doc, fildName.c_str(), value); } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::vector & value) { return tzc::json::ConverArray(doc, fildName.c_str(), value); } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::list & value) { return tzc::json::ConverArray(doc, fildName.c_str(), value); } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::vector & value) { return tzc::json::ConverArray(doc, fildName.c_str(), value); } // string array fromJson inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::list & value) { std::vector vecValue; if (tzc::json::ConverArray(doc, fildName.c_str(), vecValue)) { value.assign(vecValue.begin(), vecValue.end()); return true; } return false; } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::vector & value) { return tzc::json::ConverArray(doc, fildName.c_str(), value); } // class / struct array fromJson template inline bool getFiledParams(JsonDoc & doc, const std::string & fildName, std::list & value) { return tzc::json::ConverArrayObject(doc, fildName.c_str(), value); } template inline bool getFiledParams(JsonDoc & doc, const std::string & fildName, std::vector & value) { return tzc::json::ConverArrayObject(doc, fildName.c_str(), value); } /** * @brief Encapsulation functions for bare array json convert */ template inline std::string toJson(const std::vector & list) { return tzc::json::ArrayToJson(list); } template inline std::string toJson(const std::list & list) { return tzc::json::ArrayToJson(list); } template inline bool fromJson( const std::string & json, std::vector & values) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToArray(document, values); } template inline bool fromJson( const std::string & json, std::list & values) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToArray(document, values); } inline std::string toJson(const std::vector & list) { return tzc::json::BaseArrayToJson(list); } inline std::string toJson(const std::list & list) { return tzc::json::BaseArrayToJson(list); } inline bool fromJson( const std::string & json, std::vector & list) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToBaseArray(document, list); } inline bool fromJson( const std::string & json, std::list & list) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToBaseArray(document, list); } inline std::string toJson( const std::vector & list) { return tzc::json::BaseArrayToJson(list); } inline std::string toJson( const std::list & list) { return tzc::json::BaseArrayToJson(list); } inline bool fromJson( const std::string & json, std::vector & list) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToBaseArray(document, list); } inline bool fromJson( const std::string & json, std::list & list) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToBaseArray(document, list); } inline std::string toJson(const std::vector & list) { return tzc::json::BaseArrayToJson(list); } inline std::string toJson(const std::list & list) { return tzc::json::BaseArrayToJson(list); } inline bool fromJson( const std::string & json, std::vector & list) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToBaseArray(document, list); } inline bool fromJson( const std::string & json, std::list & list) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToBaseArray(document, list); } inline std::string toJson(const std::vector & list) { return tzc::json::BaseArrayToJson(list); } inline std::string toJson(const std::list & list) { return tzc::json::BaseArrayToJson(list); } inline bool fromJson( const std::string & json, std::vector & list) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToBaseArray(document, list); } inline bool fromJson( const std::string & json, std::list & list) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToBaseArray(document, list); } inline std::string toJson(const std::vector & list) { return tzc::json::BaseArrayToJson(list); } inline std::string toJson(const std::list & list) { return tzc::json::BaseArrayToJson(list); } inline bool fromJson(const std::string & json, std::vector & list) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToBaseArray(document, list); } inline bool fromJson( const std::string & json, std::list & list) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToBaseArray(document, list); } inline std::string toJson(const std::vector & list) { return tzc::json::BaseArrayToJson(list); } inline std::string toJson(const std::list & list) { return tzc::json::BaseArrayToJson(list); } inline bool fromJson( const std::string & json, std::vector & list) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToBaseArray(document, list); } inline bool fromJson(const std::string & json, std::list & list) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToBaseArray(document, list); } inline std::string toJson(const std::vector & list) { return tzc::json::BaseArrayToJson(list); } inline std::string toJson(const std::list & list) { return tzc::json::BaseArrayToJson(list); } inline bool fromJson(const std::string & json, std::vector & list) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToBaseArray(document, list); } inline bool fromJson( const std::string & json, std::list & list) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToBaseArray(document, list); } inline std::string toJson(const std::vector & list) { return tzc::json::BaseArrayToJson(list); } inline std::string toJson(const std::list & list) { return tzc::json::BaseArrayToJson(list); } inline bool fromJson( const std::string & json, std::vector & list) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToBaseArray(document, list); } inline bool fromJson( const std::string & json, std::list & list) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToBaseArray(document, list); } /** * @brief Encapsulation a struct for bare array */ template struct BareArray { std::vector Numbers; std::string toJson() const { return tzc::json::toJson(Numbers); } static bool fromJson( const std::string & json, BareArray & value) { return tzc::json::fromJson(json, value.Numbers); } }; template<> struct BareArray { std::vector Numbers; std::string toJson() const { return tzc::json::toJson(Numbers); } static bool fromJson( const std::string & json, BareArray & value) { return tzc::json::fromJson(json, value.Numbers); } }; template<> struct BareArray { std::vector Numbers; std::string toJson() const { return tzc::json::toJson(Numbers); } static bool fromJson( const std::string & json, BareArray & value) { return tzc::json::fromJson(json, value.Numbers); } }; template<> struct BareArray { std::vector Numbers; std::string toJson() const { return tzc::json::toJson(Numbers); } static bool fromJson( const std::string & json, BareArray & value) { return tzc::json::fromJson(json, value.Numbers); } }; template<> struct BareArray { std::vector Numbers; std::string toJson() const { return tzc::json::toJson(Numbers); } static bool fromJson( const std::string & json, BareArray & value) { return tzc::json::fromJson(json, value.Numbers); } }; template<> struct BareArray { std::vector Numbers; std::string toJson() const { return tzc::json::toJson(Numbers); } static bool fromJson( const std::string & json, BareArray & value) { return tzc::json::fromJson(json, value.Numbers); } }; template<> struct BareArray { std::vector Numbers; std::string toJson() const { return tzc::json::toJson(Numbers); } static bool fromJson( const std::string & json, BareArray & value) { return tzc::json::fromJson(json, value.Numbers); } }; template<> struct BareArray { std::vector Numbers; std::string toJson() const { return tzc::json::toJson(Numbers); } static bool fromJson( const std::string & json, BareArray & value) { return tzc::json::fromJson(json, value.Numbers); } }; template<> struct BareArray { std::vector Numbers; std::string toJson() const { return tzc::json::toJson(Numbers); } static bool fromJson(const std::string & json, BareArray & value) { return tzc::json::fromJson(json, value.Numbers); } }; #if defined(LINUX) && (__WORDSIZE == 64) inline bool setFiled(JsonDoc & doc, const std::string & fildName, const long long & value) { auto & allocator = doc.GetAllocator(); doc.AddMember(JsonValue(fildName.c_str(), allocator), JsonValue((int64_t)value), allocator); return true; } inline bool setFiled(JsonDoc & doc, const std::string & fildName, const unsigned long long & value) { auto & allocator = doc.GetAllocator(); doc.AddMember(JsonValue(fildName.c_str(), allocator), JsonValue((uint64_t)value), allocator); return true; } inline long long getFiled(const JsonDoc & doc, const std::string & fildName, const long long & defaultValue) { const char * pfildName = fildName.c_str(); JsonValue::ConstMemberIterator itr = doc.FindMember(pfildName); if (itr != doc.MemberEnd()) { if (itr->value.Is()) { return (long long)itr->value.Get(); } } return defaultValue; } inline unsigned long long getFiled(const JsonDoc & doc, const std::string & fildName, const unsigned long long & defaultValue) { const char * pfildName = fildName.c_str(); JsonValue::ConstMemberIterator itr = doc.FindMember(pfildName); if (itr != doc.MemberEnd()) { if (itr->value.Is()) { return (unsigned long long)itr->value.Get(); } } return defaultValue; } inline bool setFiledArray(JsonDoc & doc, const std::string & fildName, const std::vector & collection) { auto & allocator = doc.GetAllocator(); JsonDoc inner(&allocator); inner.SetArray(); for (auto it = collection.begin(); it != collection.end(); ++it) { JsonValue ele((int64_t)(*it)); inner.PushBack(ele, allocator); } doc.AddMember(JsonValue(fildName.c_str(), allocator), inner, allocator); return true; } inline bool setFiledArray(JsonDoc & doc, const std::string & fildName, const std::list & collection) { auto & allocator = doc.GetAllocator(); JsonDoc inner(&allocator); inner.SetArray(); for (auto it = collection.begin(); it != collection.end(); ++it) { JsonValue ele((int64_t)(*it)); inner.PushBack(ele, allocator); } doc.AddMember(JsonValue(fildName.c_str(), allocator), inner, allocator); return true; } inline bool setFiledArray(JsonDoc & doc, const std::string & fildName, const std::vector & collection) { auto & allocator = doc.GetAllocator(); JsonDoc inner(&allocator); inner.SetArray(); for (auto it = collection.begin(); it != collection.end(); ++it) { JsonValue ele((uint64_t)(*it)); inner.PushBack(ele, allocator); } doc.AddMember(JsonValue(fildName.c_str(), allocator), inner, allocator); return true; } inline bool setFiledArray(JsonDoc & doc, const std::string & fildName, const std::list & collection) { auto & allocator = doc.GetAllocator(); JsonDoc inner(&allocator); inner.SetArray(); for (auto it = collection.begin(); it != collection.end(); ++it) { JsonValue ele((uint64_t)(*it)); inner.PushBack(ele, allocator); } doc.AddMember(JsonValue(fildName.c_str(), allocator), inner, allocator); return true; } inline bool ConverArray(JsonDoc & document, const char * name, std::vector & value) { JsonValue::ConstMemberIterator itr = document.FindMember(name); if (itr != document.MemberEnd()) { if (!itr->value.IsArray()) { return false; } for (auto & v : itr->value.GetArray()) { value.push_back(v.Get()); } return true; } return false; } inline bool ConverArray(JsonDoc & document, const char * name, std::list & value) { JsonValue::ConstMemberIterator itr = document.FindMember(name); if (itr != document.MemberEnd()) { if (!itr->value.IsArray()) { return false; } for (auto & v : itr->value.GetArray()) { value.push_back(v.Get()); } return true; } return false; } inline bool ConverArray(JsonDoc & document, const char * name, std::vector & value) { JsonValue::ConstMemberIterator itr = document.FindMember(name); if (itr != document.MemberEnd()) { if (!itr->value.IsArray()) { return false; } for (auto & v : itr->value.GetArray()) { value.push_back(v.Get()); } return true; } return false; } inline bool ConverArray(JsonDoc & document, const char * name, std::list & value) { JsonValue::ConstMemberIterator itr = document.FindMember(name); if (itr != document.MemberEnd()) { if (!itr->value.IsArray()) { return false; } for (auto & v : itr->value.GetArray()) { value.push_back(v.Get()); } return true; } return false; } inline std::string BaseArrayToJson( const std::vector & collection) { JsonDoc doc; auto & allocator = doc.GetAllocator(); doc.SetArray(); for (auto it = collection.begin(); it != collection.end(); ++it) { JsonValue value((int64_t)(*it)); doc.PushBack(value, allocator); } return ValueToJson(doc); } inline std::string BaseArrayToJson(const std::list & collection) { JsonDoc doc; auto & allocator = doc.GetAllocator(); doc.SetArray(); for (auto it = collection.begin(); it != collection.end(); ++it) { JsonValue value((int64_t)(*it)); doc.PushBack(value, allocator); } return ValueToJson(doc); } inline std::string BaseArrayToJson(const std::vector & collection) { JsonDoc doc; auto & allocator = doc.GetAllocator(); doc.SetArray(); for (auto it = collection.begin(); it != collection.end(); ++it) { JsonValue value((uint64_t)(*it)); doc.PushBack(value, allocator); } return ValueToJson(doc); } inline std::string BaseArrayToJson(const std::list & collection) { JsonDoc doc; auto & allocator = doc.GetAllocator(); doc.SetArray(); for (auto it = collection.begin(); it != collection.end(); ++it) { JsonValue value((uint64_t)(*it)); doc.PushBack(value, allocator); } return ValueToJson(doc); } inline bool JsonToBaseArray(JsonDoc & document, std::vector & collections) { if (!document.IsArray()) { return false; } for (auto & val : document.GetArray()) { if (!val.Is()) { continue; } collections.emplace_back(val.Get()); } return true; } inline bool JsonToBaseArray(JsonDoc & document, std::list & collections) { if (!document.IsArray()) { return false; } for (auto & val : document.GetArray()) { if (!val.Is()) { continue; } collections.emplace_back(val.Get()); } return true; } inline bool JsonToBaseArray(JsonDoc & document, std::vector & collections) { if (!document.IsArray()) { return false; } for (auto & val : document.GetArray()) { if (!val.Is()) { continue; } collections.emplace_back(val.Get()); } return true; } inline bool JsonToBaseArray(JsonDoc & document, std::list & collections) { if (!document.IsArray()) { return false; } for (auto & val : document.GetArray()) { if (!val.Is()) { continue; } collections.emplace_back(val.Get()); } return true; } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const long long & value) { return tzc::json::setFiled(doc, fildName, value); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const unsigned long long & value) { return tzc::json::setFiled(doc, fildName, value); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::vector & value) { return tzc::json::setFiledArray(doc, fildName, value); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::list & value) { std::vector vecValue(value.begin(), value.end()); return tzc::json::setFiledArray(doc, fildName, vecValue); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::vector & value) { return tzc::json::setFiledArray(doc, fildName, value); } inline bool setFiledParams( JsonDoc & doc, const std::string & fildName, const std::list & value) { std::vector vecValue(value.begin(), value.end()); return tzc::json::setFiledArray(doc, fildName, vecValue); } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, long long & value) { value = tzc::json::getFiled(doc, fildName, (int64_t)(0)); return true; } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, unsigned long long & value) { value = tzc::json::getFiled(doc, fildName, (uint64_t)(0)); return true; } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::list & value) { return tzc::json::ConverArray(doc, fildName.c_str(), value); } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::vector & value) { return tzc::json::ConverArray(doc, fildName.c_str(), value); } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::list & value) { return tzc::json::ConverArray(doc, fildName.c_str(), value); } inline bool getFiledParams( JsonDoc & doc, const std::string & fildName, std::vector & value) { return tzc::json::ConverArray(doc, fildName.c_str(), value); } inline std::string toJson(const std::vector & list) { return tzc::json::BaseArrayToJson(list); } inline std::string toJson(const std::list & list) { return tzc::json::BaseArrayToJson(list); } inline bool fromJson(const std::string & json, std::vector & list) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToBaseArray(document, list); } inline bool fromJson(const std::string & json, std::list & list) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToBaseArray(document, list); } inline std::string toJson( const std::vector & list) { return tzc::json::BaseArrayToJson(list); } inline std::string toJson( const std::list & list) { return tzc::json::BaseArrayToJson(list); } inline bool fromJson(const std::string & json, std::vector & list) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToBaseArray(document, list); } inline bool fromJson(const std::string & json, std::list & list) { tzc::json::JsonDoc document; document.Parse(json.c_str()); if (document.HasParseError()) { return false; } return tzc::json::JsonToBaseArray(document, list); } template<> struct BareArray { std::vector Numbers; std::string toJson() const { return tzc::json::toJson(Numbers); } static bool fromJson(const std::string & json, BareArray & value) { return tzc::json::fromJson(json, value.Numbers); } }; template<> struct BareArray { std::vector Numbers; std::string toJson() const { return tzc::json::toJson(Numbers); } static bool fromJson(const std::string & json, BareArray & value) { return tzc::json::fromJson(json, value.Numbers); } }; #endif } // namespace json } // namespace tzc #endif