acl/lib_acl_cpp/include/acl_cpp/serialize/gson_helper.ipp

846 lines
18 KiB
Plaintext
Raw Normal View History

2016-10-09 22:05:41 +08:00
/**
* Copyright (C) 2015-2018
* All rights reserved.
*
* AUTHOR(S)
* E-mail: niukey@qq.com
*
* VERSION
* Sat 08 Oct 2016 09:07:14 PM CST
*/
2016-10-08 22:11:25 +08:00
#pragma once
#include "acl_cpp/acl_cpp_define.hpp"
#include "acl_cpp/stdlib/string.hpp"
#include "acl_cpp/stdlib/json.hpp"
#include "acl_cpp/stdlib/string.hpp"
namespace acl
{
///////////////////////////////////type_traits//////////////////////////
// TEMPLATE CLASS _Is_number
template<class T>
2016-10-11 22:04:45 +08:00
struct _Is_number : std::false_type
2016-10-08 22:11:25 +08:00
{
};
template<>
2016-10-11 22:04:45 +08:00
struct _Is_number<unsigned short> : std::true_type
2016-10-08 22:11:25 +08:00
{
};
template<>
2016-10-11 22:04:45 +08:00
struct _Is_number<signed short> : std::true_type
2016-10-08 22:11:25 +08:00
{
};
template<>
2016-10-11 22:04:45 +08:00
struct _Is_number<unsigned int> : std::true_type
2016-10-08 22:11:25 +08:00
{
};
template<>
2016-10-11 22:04:45 +08:00
struct _Is_number<signed int> : std::true_type
2016-10-08 22:11:25 +08:00
{
};
template<>
2016-10-11 22:04:45 +08:00
struct _Is_number<unsigned long> : std::true_type
2016-10-08 22:11:25 +08:00
{
};
template<>
2016-10-11 22:04:45 +08:00
struct _Is_number<signed long> : std::true_type
2016-10-08 22:11:25 +08:00
{
};
template<>
2016-10-11 22:04:45 +08:00
struct _Is_number<long long> : std::true_type
2016-10-08 22:11:25 +08:00
{
};
template<>
2016-10-11 22:04:45 +08:00
struct _Is_number<unsigned long long> : std::true_type
2016-10-08 22:11:25 +08:00
{
};
template<class T>
2016-10-11 22:04:45 +08:00
struct is_number : _Is_number<typename std::remove_cv
<typename std::remove_pointer<T>::type>::type>
2016-10-08 22:11:25 +08:00
{
};
//string
template<class T>
2016-10-11 22:04:45 +08:00
struct _Is_string : std::false_type
2016-10-08 22:11:25 +08:00
{
};
template<>
2016-10-11 22:04:45 +08:00
struct _Is_string<std::string> : std::true_type
2016-10-08 22:11:25 +08:00
{
};
template<>
2016-10-11 22:04:45 +08:00
struct _Is_string<acl::string> : std::true_type
2016-10-08 22:11:25 +08:00
{
};
template<class T>
2016-10-11 22:04:45 +08:00
struct is_string : _Is_string<typename std::remove_cv
<typename std::remove_pointer<T>::type>::type>
2016-10-08 22:11:25 +08:00
{
};
//double
template<class T>
2016-10-11 22:04:45 +08:00
struct _Is_double : std::false_type
2016-10-08 22:11:25 +08:00
{
};
template<>
2016-10-11 22:04:45 +08:00
struct _Is_double<float> : std::true_type
2016-10-08 22:11:25 +08:00
{
};
template<>
2016-10-11 22:04:45 +08:00
struct _Is_double<double> : std::true_type
2016-10-08 22:11:25 +08:00
{
};
template<>
2016-10-11 22:04:45 +08:00
struct _Is_double<long double> : std::true_type
2016-10-08 22:11:25 +08:00
{
};
template <class T>
2016-10-11 22:04:45 +08:00
struct is_double : _Is_double<typename std::remove_pointer
<typename std::remove_pointer<T>::type>::type>
2016-10-08 22:11:25 +08:00
{
};
//char ptr. c string in cpp
template<class T>
2016-10-11 22:04:45 +08:00
struct _Is_char_ptr : std::false_type
2016-10-08 22:11:25 +08:00
{
};
template<>
2016-10-11 22:04:45 +08:00
struct _Is_char_ptr<char*> : std::true_type
2016-10-08 22:11:25 +08:00
{
};
template<class T>
struct is_char_ptr :_Is_char_ptr<typename std::remove_cv<T>::type>
{
};
template<class T>
2016-10-11 22:04:45 +08:00
struct _Is_bool : std::false_type
2016-10-08 22:11:25 +08:00
{
};
template<>
2016-10-11 22:04:45 +08:00
struct _Is_bool<bool> : std::true_type
2016-10-08 22:11:25 +08:00
{
};
template<class T>
2016-10-11 22:04:45 +08:00
struct is_bool : _Is_bool<typename std::remove_cv
<typename std::remove_pointer<T>::type>::type>
2016-10-08 22:11:25 +08:00
{
};
template<bool T>
struct _Is_object
:std::false_type
{
};
template<>
2016-10-11 22:04:45 +08:00
struct _Is_object<true> : std::true_type
2016-10-08 22:11:25 +08:00
{
};
template<class T>
2016-10-11 22:04:45 +08:00
struct is_object : _Is_object
<!is_string<T>::value
&& !std::is_floating_point<typename std::remove_pointer
<typename std::remove_pointer<T>::type>::type>::value
&& !is_number<T>::value
&& !is_bool<T>::value
&& !is_char_ptr<T>::value>
2016-10-08 22:11:25 +08:00
{
};
2016-10-11 18:03:26 +08:00
//
template <class T>
2016-10-11 22:04:45 +08:00
typename std::enable_if<!std::is_pointer<T>::value, bool>::type
2016-10-11 18:03:26 +08:00
static inline check_nullptr(T&)
{
return false;
}
template<class T>
typename std::enable_if<!std::is_pointer<T>::value, bool>::type
static inline check_nullptr(T *t)
{
if (t == nullptr)
return true;
return false;
}
2016-10-08 22:11:25 +08:00
//acl::string ,std::string
template<class T>
2016-10-11 22:04:45 +08:00
typename std::enable_if<is_string<T>::value
&& !std::is_pointer<T>::value, const char *>::type
2016-10-08 22:11:25 +08:00
static inline get_value(const T &value)
{
return value.c_str();
}
template<class T>
2016-10-11 22:04:45 +08:00
typename std::enable_if<is_string<T>::value
&& std::is_pointer<T>::value, const char *>::type
2016-10-08 22:11:25 +08:00
static inline get_value(const T &value)
{
return value->c_str();
}
//char *,const char *
static inline const char *get_value(const char *value)
{
return value;
}
//bool
static inline bool get_value(const bool value)
{
return value;
}
static inline bool get_value(const bool* value)
{
return *value;
}
//number
template <class T>
2016-10-11 22:04:45 +08:00
typename std::enable_if<is_number<T>::value
&& !std::is_pointer<T>::value, T>::type
static inline get_value(const T t)
2016-10-08 22:11:25 +08:00
{
return t;
}
// number pointor -> number
// eg: int * -> int .
template <class T>
2016-10-11 22:04:45 +08:00
typename std::enable_if<is_number<T>::value && std::is_pointer<T>::value,
typename std::remove_pointer<T>::type>::type
static inline get_value(const T &t)
2016-10-08 22:11:25 +08:00
{
return *t;
}
template <class T>
typename std::enable_if<std::is_floating_point<T>::value, T>::type
2016-10-11 22:04:45 +08:00
static inline get_value(const T &t)
2016-10-08 22:11:25 +08:00
{
return t;
}
template <class T>
typename std::enable_if<std::is_floating_point<T>::value , T>::type
2016-10-11 22:04:45 +08:00
static inline get_value(const T *t)
2016-10-08 22:11:25 +08:00
{
return *t;
}
// obj
template<class T>
typename std::enable_if<is_object<T>::value , void>::type
static inline add_item(acl::json &json, acl::json_node &node, const T &obj)
{
2016-10-11 18:03:26 +08:00
if (check_nullptr(obj))
node.add_array_null();
else
node.add_child(gson(json, obj));
2016-10-08 22:11:25 +08:00
}
template<class T>
typename std::enable_if<is_object<T>::value , void>::type
2016-10-11 22:04:45 +08:00
static inline add_item(acl::json &json, acl::json_node &node, const T *obj)
2016-10-08 22:11:25 +08:00
{
2016-10-11 18:03:26 +08:00
if (check_nullptr(obj))
node.add_array_null();
else
add_item(json, node, *obj);
2016-10-08 22:11:25 +08:00
}
// number
template<class T>
typename std::enable_if<is_number<T>::value, void>::type
static inline add_item(acl::json &, acl::json_node &node, T value)
{
2016-10-11 18:03:26 +08:00
if (check_nullptr(value))
node.add_array_null();
else
node.add_array_number(get_value(value));
2016-10-08 22:11:25 +08:00
}
template<class T>
typename std::enable_if<is_number<T>::value, void>::type
static inline add_item(acl::json &, acl::json_node &node, T *value)
{
2016-10-11 18:03:26 +08:00
if (check_nullptr(value))
node.add_array_null();
else
node.add_array_number(get_value(value));
2016-10-08 22:11:25 +08:00
}
template<class T>
typename std::enable_if<std::is_floating_point<
typename std::remove_pointer<T>::type>::value, void>::type
static inline add_item(acl::json &, acl::json_node &node, T value)
{
2016-10-11 18:03:26 +08:00
if (check_nullptr(value))
node.add_array_null();
else
node.add_array_double(get_value(value));
2016-10-08 22:11:25 +08:00
}
//bool
template<class T>
typename std::enable_if<is_bool<T>::value, void>::type
static inline add_item(acl::json &, acl::json_node &node, T value)
{
2016-10-11 18:03:26 +08:00
if (check_nullptr(value))
node.add_array_null();
else
node.add_array_bool(get_value(value));
2016-10-08 22:11:25 +08:00
}
template<class V>
static inline
acl::json_node &gson(acl::json &json, const std::list<V> &objects)
{
acl::json_node &node = json.create_array();
2016-10-11 22:04:45 +08:00
for (typename std::list<V>::const_iterator
itr = objects.begin(); itr != objects.end(); ++itr)
2016-10-08 22:11:25 +08:00
{
add_item(json, node, *itr);
}
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return node;
}
template<class V>
static inline
acl::json_node &gson(acl::json &json, const std::list<V> *objects)
{
acl::json_node &node = json.create_array();
2016-10-11 22:04:45 +08:00
for (typename std::list<V>::const_iterator
itr = objects->begin(); itr != objects->end(); ++itr)
2016-10-08 22:11:25 +08:00
{
add_item(json, node, *itr);
}
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return node;
}
template<class V>
2016-10-11 22:04:45 +08:00
static inline
acl::json_node &gson(acl::json &json, const std::vector<V> &objects)
2016-10-08 22:11:25 +08:00
{
acl::json_node &node = json.create_array();
2016-10-11 22:04:45 +08:00
for (typename std::vector<V>::const_iterator
itr = objects.begin(); itr != objects.end(); ++itr)
2016-10-08 22:11:25 +08:00
{
add_item(json, node, *itr);
}
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return node;
}
template<class V>
2016-10-11 22:04:45 +08:00
static inline
acl::json_node &gson(acl::json &json, const std::vector<V> *objects)
2016-10-08 22:11:25 +08:00
{
return gson(json, *objects);
}
//define number map
template<class K, class V>
2016-10-11 22:04:45 +08:00
typename std::enable_if<is_number<V>::value, acl::json_node &>::type
2016-10-08 22:11:25 +08:00
static inline gson(acl::json &json, const std::map<K, V> &objects)
{
acl::json_node &node = json.create_array();
2016-10-11 22:04:45 +08:00
for (typename std::map<K, V>::const_iterator itr = objects.begin();
2016-10-08 22:11:25 +08:00
itr != objects.end(); ++itr)
{
2016-10-11 18:03:26 +08:00
const char *tag = get_value(itr->first);
if (check_nullptr(itr->second))
{
node.add_child(json.create_node().add_null(tag));
}
else
{
acl::json_node &item = gson(json, itr->second);
2016-10-11 22:04:45 +08:00
node.add_child(json.create_node().add_child(tag, item));
2016-10-11 18:03:26 +08:00
}
2016-10-08 22:11:25 +08:00
}
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return node;
}
//define number map
template<class K, class V>
typename std::enable_if< is_number<V>::value, acl::json_node &>::type
static inline gson(acl::json &json, const std::map<K, V> *objects)
{
acl::json_node &node = json.create_array();
2016-10-11 22:04:45 +08:00
for (typename std::map<K, V>::const_iterator itr = objects->begin();
2016-10-08 22:11:25 +08:00
itr != objects->end(); ++itr)
{
2016-10-11 18:03:26 +08:00
const char *tag = get_value(itr->first);
if (check_nullptr(itr->second))
{
node.add_child(json.create_node().add_null(tag));
}
else
{
acl::json_node &item = gson(json, itr->second);
2016-10-11 22:04:45 +08:00
node.add_child(json.create_node().add_child(tag, item));
2016-10-11 18:03:26 +08:00
}
2016-10-08 22:11:25 +08:00
}
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return node;
}
//define floating map
template<class K, class V>
typename std::enable_if<is_double<V>::value, acl::json_node &>::type
static inline gson(acl::json &json, const std::map<K, V> &objects)
{
acl::json_node &node = json.create_array();
2016-10-11 22:04:45 +08:00
for (typename std::map<K, V>::const_iterator itr = objects.begin();
2016-10-08 22:11:25 +08:00
itr != objects.end(); ++itr)
{
2016-10-11 18:03:26 +08:00
const char *tag = get_value(itr->first);
if (check_nullptr(itr->second))
{
node.add_child(json.create_node().add_null(tag));
}
else
{
acl::json_node &item = gson(json, itr->second);
2016-10-11 22:04:45 +08:00
node.add_child(json.create_node().add_child(tag, item));
2016-10-11 18:03:26 +08:00
}
2016-10-08 22:11:25 +08:00
}
return node;
}
template<class K, class V>
typename std::enable_if<is_double<V>::value, acl::json_node &>::type
static inline gson(acl::json &json, const std::map<K, V> *objects)
{
acl::json_node &node = json.create_array();
2016-10-11 22:04:45 +08:00
for (typename std::map<K, V>::const_iterator itr = objects->begin();
2016-10-08 22:11:25 +08:00
itr != objects->end(); ++itr)
{
2016-10-11 18:03:26 +08:00
const char *tag = get_value(itr->first);
if (check_nullptr(itr->second))
{
node.add_child(json.create_node().add_null(tag));
}
else
{
acl::json_node &item = gson(json, itr->second);
2016-10-11 22:04:45 +08:00
node.add_child(json.create_node().add_child(tag, item));
2016-10-11 18:03:26 +08:00
}
2016-10-08 22:11:25 +08:00
}
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return node;
}
//define bool map
template<class K, class V>
typename std::enable_if<is_bool<V>::value, acl::json_node &>::type
static inline gson(acl::json &json, const std::map<K, V> &objects)
{
acl::json_node &node = json.create_array();
2016-10-11 22:04:45 +08:00
for (typename std::map<K, V>::const_iterator itr = objects;
2016-10-08 22:11:25 +08:00
itr != objects.end(); ++itr)
{
2016-10-11 18:03:26 +08:00
const char *tag = get_value(itr->first);
if (check_nullptr(itr->second))
{
node.add_child(json.create_node().add_null(tag));
}
else
{
acl::json_node &item = gson(json, itr->second);
2016-10-11 22:04:45 +08:00
node.add_child(json.create_node().add_child(tag, item));
2016-10-11 18:03:26 +08:00
}
2016-10-08 22:11:25 +08:00
}
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return node;
}
template<class K, class V>
2016-10-11 22:04:45 +08:00
typename std::enable_if<is_string<V>::value
|| is_char_ptr<V>::value, acl::json_node &>::type
2016-10-11 18:03:26 +08:00
static inline gson(acl::json &json, const std::map<K, V> & objects)
2016-10-08 22:11:25 +08:00
{
acl::json_node &node = json.create_array();
2016-10-11 22:04:45 +08:00
for (typename std::map<K, V>::const_iterator itr = objects.begin();
itr != objects.end(); ++itr)
2016-10-08 22:11:25 +08:00
{
2016-10-11 18:03:26 +08:00
const char *tag = get_value(itr->first);
if (check_nullptr(itr->second))
{
node.add_child(json.create_node().add_null(tag));
}
else
{
acl::json_node &item = gson(json, itr->second);
2016-10-11 22:04:45 +08:00
node.add_child(json.create_node().add_child(tag, item));
2016-10-11 18:03:26 +08:00
}
2016-10-08 22:11:25 +08:00
}
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return node;
}
template<class T, class V>
typename std::enable_if<is_object<V>::value, acl::json_node &>::type
static inline gson(acl::json &json,const std::map<T, V> &objects)
{
acl::json_node &node = json.create_array();
2016-10-11 22:04:45 +08:00
for (typename std::map<T, V>::const_iterator
itr = objects.begin(); itr != objects.end(); ++itr)
2016-10-08 22:11:25 +08:00
{
const char *tag = get_value(itr->first);
2016-10-11 18:03:26 +08:00
if (check_nullptr(itr->second))
{
node.add_child(json.create_node().add_null(tag));
}
else
{
acl::json_node &item = gson(json, itr->second);
node.add_child(json.create_node().add_child(tag, item));
}
2016-10-08 22:11:25 +08:00
}
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return node;
}
template<class T, class V>
typename std::enable_if<is_object<V>::value, acl::json_node &>::type
static inline gson(acl::json &json, const std::map<T, V> *objects)
{
acl::json_node &node = json.create_array();
2016-10-11 22:04:45 +08:00
for (typename std::map<T, V>::const_iterator
itr = objects->begin(); itr != objects->end(); ++itr)
2016-10-08 22:11:25 +08:00
{
const char *tag = get_value(itr->first);
2016-10-11 18:03:26 +08:00
if (check_nullptr(itr->second))
{
node.add_child(json.create_node().add_null(tag));
}
else
{
acl::json_node &item = gson(json, itr->second);
node.add_child(json.create_node().add_child(tag, item));
}
2016-10-08 22:11:25 +08:00
}
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return node;
}
//////////////////////////////////////////////////////////////////////
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
template <class T>
typename std::enable_if<std::is_class<T>::value,
std::pair<bool, std::string>>::type
2016-10-11 22:04:45 +08:00
static inline gson(acl::json_node &node, T **obj);
2016-10-08 22:11:25 +08:00
template<class T>
static inline void del(T **obj)
{
delete *obj;
*obj = NULL;
}
template<class T>
static inline void del(T *obj)
{
(void) obj;
}
//bool
static inline std::pair<bool, std::string> gson(acl::json_node &node, bool *obj)
{
2016-10-11 22:04:45 +08:00
if (node.is_bool() == false)
2016-10-08 22:11:25 +08:00
return std::make_pair(false, "get bool failed");
*obj = *node.get_bool();
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return std::make_pair(true, "");
}
static inline std::pair<bool, std::string> gson(acl::json_node &node, bool **obj)
{
2016-10-11 22:04:45 +08:00
if (node.is_bool() == false)
2016-10-08 22:11:25 +08:00
return std::make_pair(false, "get bool failed");
*obj = new bool;
**obj = *node.get_bool();
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return std::make_pair(true, "");
}
//double
template <class T>
typename std::enable_if<std::is_floating_point<T>::value,
std::pair<bool, std::string>>::type
2016-10-11 22:04:45 +08:00
static inline gson(acl::json_node &node, T *obj)
2016-10-08 22:11:25 +08:00
{
2016-10-11 22:04:45 +08:00
if (node.is_double() == false)
2016-10-08 22:11:25 +08:00
return std::make_pair(false, "get double failed");
*obj = static_cast<T>(*node.get_double());
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return std::make_pair(true, "");
}
template <class T>
typename std::enable_if<std::is_floating_point<T>::value,
std::pair<bool, std::string>>::type
static inline gson(acl::json_node &node, T **obj)
{
2016-10-11 22:04:45 +08:00
if (node.is_double() == false)
2016-10-08 22:11:25 +08:00
return std::make_pair(false, "get double failed");;
*obj = new T;
**obj = static_cast<T>(*node.get_double());
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return std::make_pair(true, "");
}
//intergral
template <class T>
typename std::enable_if<is_number<T>::value, std::pair<bool, std::string>>::type
static inline gson(acl::json_node &node, T *obj)
{
2016-10-11 22:04:45 +08:00
if (node.is_number() == false)
2016-10-08 22:11:25 +08:00
return std::make_pair(false, "get number failed");
*obj = static_cast<T>(*node.get_int64());
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return std::make_pair(true, "");
}
template <class T>
typename std::enable_if<is_number<T>::value, std::pair<bool, std::string>>::type
static inline gson(acl::json_node &node, T **obj)
{
2016-10-11 22:04:45 +08:00
if (node.is_number() == false)
2016-10-08 22:11:25 +08:00
return std::make_pair(false, "get number failed");;
*obj = new T;
**obj = static_cast<T>(*node.get_int64());
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return std::make_pair(true, "");
}
//string
template<class T>
typename std::enable_if<std::is_same<T,char>::value,
std::pair<bool, std::string>>::type
static inline gson(acl::json_node &node, T **obj)
{
2016-10-11 22:04:45 +08:00
if (node.is_string() == false)
2016-10-08 22:11:25 +08:00
return std::make_pair(false, "get char * string failed");
int len = strlen(node.get_string());
*obj = new T[len + 1];
memcpy(*obj, node.get_string(), len);
(*obj)[len] = 0;
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return std::make_pair(true, "");
}
template<class T>
2016-10-13 17:38:45 +08:00
typename std::enable_if<is_string<T>::value
2016-10-11 22:04:45 +08:00
&& !std::is_pointer<T>::value, std::pair<bool, std::string>>::type
2016-10-08 22:11:25 +08:00
static inline gson(acl::json_node &node, T *obj)
{
2016-10-11 22:04:45 +08:00
if (node.is_string() == false)
2016-10-08 22:11:25 +08:00
return std::make_pair(false, "get string failed");
obj->append(node.get_string());
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return std::make_pair(true, "");
}
template<class T>
typename std::enable_if<is_string<T>::value, std::pair<bool, std::string>>::type
static inline gson(acl::json_node &node, std::string **obj)
{
2016-10-11 22:04:45 +08:00
if (node.is_string() == false)
2016-10-08 22:11:25 +08:00
return std::make_pair(false, "get string failed");
*obj = new std::string;
(*obj)->append(node.get_string());
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return std::make_pair(true, "");
}
template<class T>
static inline std::pair<bool, std::string>
gson(acl::json_node &node, std::list<T> *objs)
{
std::pair<bool, std::string> result;
std::string error_string;
acl::json_node *itr = node.first_child();
2016-10-11 22:04:45 +08:00
while (itr)
2016-10-08 22:11:25 +08:00
{
T obj;
result = gson(*itr, &obj);
2016-10-11 22:04:45 +08:00
if (result.first)
2016-10-08 22:11:25 +08:00
objs->push_back(obj);
else
error_string.append(result.second);
itr = node.next_child();
}
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return std::make_pair(!!!objs->empty(), error_string);
}
//vector
template<class T>
std::pair<bool, std::string>
2016-10-11 22:04:45 +08:00
static inline gson(acl::json_node &node, std::vector<T> *objs)
2016-10-08 22:11:25 +08:00
{
std::pair<bool, std::string> result;
acl::json_node *itr = node.first_child();
std::string error_string;
2016-10-11 22:04:45 +08:00
while (itr)
2016-10-08 22:11:25 +08:00
{
T obj;
result = gson(*itr, &obj);
if(result.first)
objs->push_back(obj);
else
error_string.append(result.second);
itr = node.next_child();
//todo delete obj when failed
}
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return std::make_pair(!!!objs->empty(), error_string);
}
template <class T>
typename std::enable_if<std::is_class<T>::value,
std::pair<bool, std::string>>::type
2016-10-11 22:04:45 +08:00
static inline gson(acl::json_node &node, T **obj)
2016-10-08 22:11:25 +08:00
{
*obj = new T();
std::pair<bool, std::string> result = gson(node, *obj);
2016-10-11 22:04:45 +08:00
if (result.first == false)
2016-10-08 22:11:25 +08:00
{
delete *obj;
*obj = NULL;
}
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return result;
}
/////////////////////////////////////map///////////////////////////////
//int map
template<class K,class T>
2016-10-11 22:04:45 +08:00
typename std::enable_if
<is_string<T>::value
|| is_bool<T>::value
|| is_number<T>::value
|| is_double<T>::value
|| is_char_ptr<T>::value,
std::pair<bool, std::string>>::type
2016-10-08 22:11:25 +08:00
static inline expand(acl::json_node &node, std::map<K, T> *objs)
{
std::pair<bool, std::string> result;
acl::json_node *itr = node.first_child();
2016-10-11 22:04:45 +08:00
while (itr)
2016-10-08 22:11:25 +08:00
{
T obj;
result = gson(*itr, &obj);
2016-10-11 22:04:45 +08:00
if (result.first)
objs->insert(std::make_pair(K(itr->tag_name()), obj));
2016-10-08 22:11:25 +08:00
else
break;
itr = node.next_child();
}
2016-10-11 22:04:45 +08:00
if (result.first == false)
2016-10-08 22:11:25 +08:00
{
2016-10-11 22:04:45 +08:00
for (typename std::map<K, T>::iterator it = objs->begin();
2016-10-08 22:11:25 +08:00
it != objs->end(); ++it)
{
del(&it->second);
}
objs->clear();
return result;
}
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return std::make_pair(true, "");
}
template<class K,class T>
2016-10-11 22:04:45 +08:00
typename std::enable_if
<std::is_class<typename std::remove_pointer<T>::type>::value
&& !is_string<T>::value, std::pair<bool, std::string>>::type
2016-10-08 22:11:25 +08:00
static inline expand(acl::json_node &node, std::map<K, T> *objs)
{
std::pair<bool, std::string> result;
acl::json_node *itr = node.first_child();
2016-10-11 22:04:45 +08:00
while (itr && itr->get_obj())
2016-10-08 22:11:25 +08:00
{
T obj;
result = gson(*(itr->get_obj()), &obj);
2016-10-11 22:04:45 +08:00
if (result.first)
objs->insert(std::make_pair(K(itr->tag_name()), obj));
2016-10-08 22:11:25 +08:00
else
break;
itr = node.next_child();
}
2016-10-11 22:04:45 +08:00
if (result.first == false)
2016-10-08 22:11:25 +08:00
{
2016-10-11 22:04:45 +08:00
for (typename std::map<K, T>::iterator itr2 = objs->begin();
itr2 != objs->end(); ++itr2)
2016-10-08 22:11:25 +08:00
{
del(&itr2->second);
}
objs->clear();
return result;
}
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return std::make_pair(true, "");
}
//map
template<class K,class V>
std::pair<bool, std::string>
static inline gson(acl::json_node &node, std::map<K, V> *objs)
{
std::pair<bool, std::string> result;
acl::json_node *itr = node.first_child();
std::string error_string;
2016-10-11 22:04:45 +08:00
while (itr)
2016-10-08 22:11:25 +08:00
{
result = expand(*itr, objs);
2016-10-11 22:04:45 +08:00
if (result.first == false)
2016-10-08 22:11:25 +08:00
error_string.append(result.second);
itr = node.next_child();
}
2016-10-11 22:04:45 +08:00
2016-10-08 22:11:25 +08:00
return std::make_pair(!!!objs->empty(), error_string);
}
} // namespace acl