2016-10-09 22:05:41 +08:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2015-2018
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* AUTHOR(S)
|
2017-05-14 00:16:42 +08:00
|
|
|
* E-mail: "fuwangqini" niukey@qq.com
|
|
|
|
* 2017.5.13 fixed one bug by "lindawei" <672496008@qq.com>
|
2016-10-14 11:46:09 +08:00
|
|
|
*
|
2016-10-09 22:05:41 +08:00
|
|
|
* VERSION
|
|
|
|
* Sat 08 Oct 2016 09:07:14 PM CST
|
|
|
|
*/
|
|
|
|
|
2016-10-08 22:11:25 +08:00
|
|
|
#pragma once
|
2017-06-02 14:47:24 +08:00
|
|
|
#include "../acl_cpp_define.hpp"
|
|
|
|
#include "../stdlib/string.hpp"
|
|
|
|
#include "../stdlib/json.hpp"
|
|
|
|
#include "../stdlib/string.hpp"
|
2017-06-14 13:55:05 +08:00
|
|
|
#include <set>
|
2016-10-08 22:11:25 +08:00
|
|
|
namespace acl
|
|
|
|
{
|
|
|
|
|
2017-05-14 00:16:42 +08:00
|
|
|
///////////////////////////////////type_traits////////////////////////////////
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
struct true_type
|
|
|
|
{
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
struct false_type
|
|
|
|
{
|
|
|
|
static const bool value = false;
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
struct remove_const
|
|
|
|
{
|
|
|
|
typedef T type;
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
struct remove_const<const T>
|
|
|
|
{
|
|
|
|
typedef T type;
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
struct remove_volatile
|
|
|
|
{
|
|
|
|
typedef T type;
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
struct remove_volatile<volatile T>
|
|
|
|
{
|
|
|
|
typedef T type;
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
struct remove_cv
|
|
|
|
{
|
|
|
|
typedef typename remove_const
|
|
|
|
<typename remove_volatile<T>::type>::type type;
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
//remove_pointer
|
|
|
|
template<class T>
|
|
|
|
struct remove_pointer
|
|
|
|
{
|
|
|
|
typedef T type;
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
struct remove_pointer<T *>
|
|
|
|
{
|
|
|
|
typedef T type;
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
struct remove_pointer<T *const>
|
|
|
|
{
|
|
|
|
typedef T type;
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
struct remove_pointer<T *volatile>
|
|
|
|
{
|
|
|
|
typedef T type;
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
struct remove_pointer<T *const volatile>
|
|
|
|
{
|
|
|
|
typedef T type;
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<bool ,
|
|
|
|
class T = void>
|
|
|
|
struct enable_if
|
|
|
|
{
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
struct enable_if<true, T>
|
|
|
|
{
|
|
|
|
typedef T type;
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
//_Is_number
|
|
|
|
template<class T>
|
|
|
|
struct _Is_number : false_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<>
|
|
|
|
struct _Is_number<unsigned short> : true_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-11 18:03:26 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<>
|
|
|
|
struct _Is_number<signed short> : true_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<>
|
|
|
|
struct _Is_number<unsigned int> : true_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<>
|
|
|
|
struct _Is_number<signed int> : true_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<>
|
|
|
|
struct _Is_number<unsigned long> : true_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<>
|
|
|
|
struct _Is_number<signed long> : true_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<>
|
|
|
|
struct _Is_number<long long> : true_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<>
|
|
|
|
struct _Is_number<unsigned long long> : true_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2017-04-17 21:56:13 +08:00
|
|
|
#if 0
|
|
|
|
template<>
|
|
|
|
struct _Is_number<size_t> : true_type
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct _Is_number<ssize_t> : true_type
|
|
|
|
{
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
struct is_number : _Is_number<typename remove_cv<T>::type>
|
|
|
|
{
|
|
|
|
};
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
//string
|
|
|
|
template<class T>
|
|
|
|
struct _Is_string : false_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<>
|
|
|
|
struct _Is_string<std::string> : true_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<>
|
|
|
|
struct _Is_string<acl::string> : true_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
struct is_string : _Is_string<typename remove_cv<T>::type>
|
|
|
|
{
|
|
|
|
};
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
//double
|
|
|
|
template<class T>
|
|
|
|
struct _Is_double : false_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<>
|
|
|
|
struct _Is_double<float> : true_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<>
|
|
|
|
struct _Is_double<double> : true_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<>
|
|
|
|
struct _Is_double<long double> : true_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template <class T>
|
|
|
|
struct is_double : _Is_double<typename remove_const<T>::type>
|
|
|
|
{
|
|
|
|
};
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
//char ptr. c string in cpp
|
|
|
|
template<class T>
|
|
|
|
struct _Is_char_ptr : false_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<>
|
|
|
|
struct _Is_char_ptr<char*> : true_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-11 22:04:45 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
struct is_char_ptr :_Is_char_ptr<typename remove_cv<T>::type>
|
|
|
|
{
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
struct _Is_bool : false_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-11 22:04:45 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<>
|
|
|
|
struct _Is_bool<bool> : true_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
struct is_bool : _Is_bool<typename remove_cv<T>::type>
|
|
|
|
{
|
|
|
|
};
|
2016-10-11 22:04:45 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<bool T>
|
|
|
|
struct _Is_object :false_type
|
|
|
|
{
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<>
|
|
|
|
struct _Is_object<true> : true_type
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct is_object : _Is_object<
|
|
|
|
!is_string<T>::value &&
|
|
|
|
!is_double<T>::value &&
|
|
|
|
!is_number<T>::value &&
|
|
|
|
!is_bool<T>::value &&
|
|
|
|
!is_char_ptr<T>::value>
|
|
|
|
{
|
|
|
|
};
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template <class T>
|
|
|
|
static inline bool check_nullptr(T&)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
static inline bool check_nullptr(T *t)
|
|
|
|
{
|
|
|
|
if (t == NULL)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//acl::string ,std::string
|
|
|
|
template<class T>
|
|
|
|
typename enable_if<is_string<T>::value, const char *>::type
|
|
|
|
static inline get_value(const T &value)
|
|
|
|
{
|
|
|
|
return value.c_str();
|
|
|
|
}
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
typename enable_if<is_string<T>::value, const char *>::type
|
|
|
|
static inline get_value(const T *value)
|
|
|
|
{
|
|
|
|
return value->c_str();
|
|
|
|
}
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
//char *,const char *
|
|
|
|
static inline const char *get_value(const char *value)
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
//bool
|
|
|
|
static inline bool get_value(const bool value)
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
2016-10-11 22:04:45 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
static inline bool get_value(const bool* value)
|
|
|
|
{
|
|
|
|
return *value;
|
|
|
|
}
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
//number
|
|
|
|
template <class T>
|
|
|
|
typename enable_if<is_number<T>::value, T>::type
|
|
|
|
static inline get_value(const T t)
|
|
|
|
{
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
// number pointor -> number
|
|
|
|
// eg: int * -> int .
|
|
|
|
template <class T>
|
|
|
|
typename enable_if<is_number<T>::value, T>::type
|
|
|
|
static inline get_value(const T *t)
|
|
|
|
{
|
|
|
|
return *t;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
typename enable_if<is_double<T>::value, T>::type
|
|
|
|
static inline get_value(const T &t)
|
|
|
|
{
|
|
|
|
return t;
|
|
|
|
}
|
2016-10-11 22:04:45 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template <class T>
|
|
|
|
typename enable_if<is_double<T>::value, T>::type
|
|
|
|
static inline get_value(const T *t)
|
|
|
|
{
|
|
|
|
return *t;
|
|
|
|
}
|
|
|
|
|
|
|
|
// obj
|
|
|
|
template<class T>
|
|
|
|
typename enable_if<is_object<T>::value, void>::type
|
|
|
|
static inline add_item(acl::json &json, acl::json_node &node, const T &obj)
|
|
|
|
{
|
2016-10-17 22:02:31 +08:00
|
|
|
if(check_nullptr(obj))
|
2016-10-14 22:48:32 +08:00
|
|
|
node.add_array_null();
|
|
|
|
else
|
|
|
|
node.add_child(gson(json, obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
// number
|
|
|
|
template<class T>
|
|
|
|
typename enable_if<is_number<T>::value, void>::type
|
|
|
|
static inline add_item(acl::json &, acl::json_node &node, T value)
|
|
|
|
{
|
2016-10-17 22:02:31 +08:00
|
|
|
node.add_array_number(get_value(value));
|
2016-10-14 22:48:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
typename enable_if<is_number<T>::value, void>::type
|
|
|
|
static inline add_item(acl::json &, acl::json_node &node, T *value)
|
|
|
|
{
|
|
|
|
if (check_nullptr(value))
|
|
|
|
node.add_array_null();
|
|
|
|
else
|
|
|
|
node.add_array_number(get_value(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
typename enable_if<is_double<T>::value, void>::type
|
|
|
|
static inline add_item(acl::json &, acl::json_node &node, T value)
|
|
|
|
{
|
2016-10-17 22:02:31 +08:00
|
|
|
node.add_array_double(get_value(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
typename enable_if<is_double<T>::value, void>::type
|
|
|
|
static inline add_item(acl::json &, acl::json_node &node, T* value)
|
|
|
|
{
|
|
|
|
if(check_nullptr(value))
|
2016-10-14 22:48:32 +08:00
|
|
|
node.add_array_null();
|
|
|
|
else
|
|
|
|
node.add_array_double(get_value(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
//bool
|
|
|
|
template<class T>
|
|
|
|
typename enable_if<is_bool<T>::value, void>::type
|
2016-10-17 22:02:31 +08:00
|
|
|
static inline add_item(acl::json &, acl::json_node &node, T *value)
|
2016-10-14 22:48:32 +08:00
|
|
|
{
|
|
|
|
if (check_nullptr(value))
|
|
|
|
node.add_array_null();
|
|
|
|
else
|
|
|
|
node.add_array_bool(get_value(value));
|
|
|
|
}
|
|
|
|
|
2016-10-17 22:02:31 +08:00
|
|
|
template<class T>
|
|
|
|
typename enable_if<is_bool<T>::value, void>::type
|
|
|
|
static inline add_item(acl::json &, acl::json_node &node, T value)
|
|
|
|
{
|
|
|
|
node.add_array_bool(get_value(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
typename enable_if<is_string<T>::value, void>::type
|
|
|
|
static inline add_item(acl::json &, acl::json_node &node, T value)
|
|
|
|
{
|
|
|
|
node.add_array_text(get_value(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
typename enable_if<is_string<T>::value, void>::type
|
|
|
|
static inline add_item(acl::json &, acl::json_node &node, T *value)
|
|
|
|
{
|
2017-05-14 00:16:42 +08:00
|
|
|
if (check_nullptr(value))
|
2016-10-17 22:02:31 +08:00
|
|
|
node.add_array_null();
|
|
|
|
else
|
|
|
|
node.add_array_text(get_value(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void add_item(acl::json &, acl::json_node &node, char *value)
|
|
|
|
{
|
2017-05-14 00:16:42 +08:00
|
|
|
if (check_nullptr(value))
|
2016-10-17 22:02:31 +08:00
|
|
|
node.add_array_null();
|
|
|
|
else
|
|
|
|
node.add_array_text(value);
|
|
|
|
}
|
2017-06-14 13:55:05 +08:00
|
|
|
//list
|
2017-05-14 00:16:42 +08:00
|
|
|
template<class T>
|
|
|
|
static inline acl::json_node &gson(acl::json &json,
|
|
|
|
const std::list<T> &objects)
|
2016-10-14 22:48:32 +08:00
|
|
|
{
|
|
|
|
acl::json_node &node = json.create_array();
|
2017-05-14 00:16:42 +08:00
|
|
|
for (typename std::list<T>::const_iterator
|
2016-10-14 22:48:32 +08:00
|
|
|
itr = objects.begin(); itr != objects.end(); ++itr)
|
2016-10-14 11:46:09 +08:00
|
|
|
{
|
2016-10-14 22:48:32 +08:00
|
|
|
add_item(json, node, *itr);
|
2016-10-14 11:46:09 +08:00
|
|
|
}
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
template<class T>
|
2017-05-14 00:16:42 +08:00
|
|
|
static inline acl::json_node &gson(acl::json &json,
|
|
|
|
const std::list<T> *objects)
|
2016-10-14 22:48:32 +08:00
|
|
|
{
|
2017-05-14 00:16:42 +08:00
|
|
|
return gson(json, *objects);
|
2016-10-14 22:48:32 +08:00
|
|
|
}
|
|
|
|
|
2017-06-14 13:55:05 +08:00
|
|
|
//vector
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
static inline acl::json_node &gson(acl::json &json,
|
|
|
|
const std::vector<T> &objects)
|
|
|
|
{
|
|
|
|
acl::json_node &node = json.create_array();
|
|
|
|
for (typename std::vector<T>::const_iterator
|
|
|
|
itr = objects.begin(); itr != objects.end(); ++itr)
|
2016-10-08 22:11:25 +08:00
|
|
|
{
|
2016-10-14 22:48:32 +08:00
|
|
|
add_item(json, node, *itr);
|
2016-10-14 11:46:09 +08:00
|
|
|
}
|
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
return node;
|
|
|
|
}
|
2016-10-11 22:04:45 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
static inline acl::json_node &gson(acl::json &json,
|
|
|
|
const std::vector<T> *objects)
|
|
|
|
{
|
|
|
|
return gson(json, *objects);
|
|
|
|
}
|
2017-06-14 13:55:05 +08:00
|
|
|
//set
|
|
|
|
template<class T>
|
|
|
|
static inline acl::json_node &gson(acl::json &json, const std::set<T> *objects)
|
|
|
|
{
|
|
|
|
return gson(json, *objects);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
static inline acl::json_node &gson(acl::json &json,
|
|
|
|
const std::set<T> &objects)
|
|
|
|
{
|
|
|
|
acl::json_node &node = json.create_array();
|
|
|
|
for (typename std::set<T>::const_iterator
|
|
|
|
itr = objects.begin(); itr != objects.end(); ++itr)
|
|
|
|
{
|
|
|
|
add_item(json, node, *itr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
//define number map
|
|
|
|
template<class K, class V>
|
|
|
|
typename 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();
|
|
|
|
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-14 22:48:32 +08:00
|
|
|
const char *tag = get_value(itr->first);
|
|
|
|
if (check_nullptr(itr->second))
|
|
|
|
node.add_child(json.create_node().add_null(tag));
|
|
|
|
else
|
2017-05-14 00:16:42 +08:00
|
|
|
node.add_child(json.create_node()
|
|
|
|
.add_number(tag, get_value(itr->second)));
|
2016-10-14 11:46:09 +08:00
|
|
|
}
|
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
//define number map
|
|
|
|
template<class K, class V>
|
|
|
|
typename 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();
|
|
|
|
for (typename std::map<K, V>::const_iterator
|
|
|
|
itr = objects->begin(); itr != objects->end(); ++itr)
|
2016-10-14 11:46:09 +08:00
|
|
|
{
|
2016-10-14 22:48:32 +08:00
|
|
|
const char *tag = get_value(itr->first);
|
|
|
|
if (check_nullptr(itr->second))
|
|
|
|
node.add_child(json.create_node().add_null(tag));
|
|
|
|
else
|
2017-05-14 00:16:42 +08:00
|
|
|
node.add_child(json.create_node()
|
|
|
|
.add_number(tag, get_value(itr->second)));
|
2016-10-08 22:11:25 +08:00
|
|
|
}
|
2016-10-11 22:04:45 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
return node;
|
|
|
|
}
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
//define floating map
|
|
|
|
template<class K, class V>
|
|
|
|
typename 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();
|
|
|
|
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-14 22:48:32 +08:00
|
|
|
const char *tag = get_value(itr->first);
|
|
|
|
if (check_nullptr(itr->second))
|
|
|
|
node.add_child(json.create_node().add_null(tag));
|
|
|
|
else
|
2017-05-14 00:16:42 +08:00
|
|
|
node.add_child(json.create_node()
|
|
|
|
.add_double(tag, get_value(itr->second)));
|
2016-10-14 11:46:09 +08:00
|
|
|
}
|
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class V>
|
|
|
|
typename 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();
|
|
|
|
for (typename std::map<K, V>::const_iterator
|
|
|
|
itr = objects->begin(); itr != objects->end(); ++itr)
|
2016-10-14 11:46:09 +08:00
|
|
|
{
|
2016-10-14 22:48:32 +08:00
|
|
|
const char *tag = get_value(itr->first);
|
|
|
|
if (check_nullptr(itr->second))
|
|
|
|
node.add_child(json.create_node().add_null(tag));
|
|
|
|
else
|
2017-05-14 00:16:42 +08:00
|
|
|
node.add_child(json.create_node()
|
|
|
|
.add_double(tag, get_value(itr->second)));
|
2016-10-14 11:46:09 +08:00
|
|
|
}
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
//define bool map
|
|
|
|
template<class K, class V>
|
|
|
|
typename 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-12-30 17:08:18 +08:00
|
|
|
for (typename std::map<K, V>::const_iterator itr = objects.begin();
|
2016-10-14 22:48:32 +08:00
|
|
|
itr != objects.end(); ++itr)
|
2016-10-08 22:11:25 +08:00
|
|
|
{
|
2016-10-14 22:48:32 +08:00
|
|
|
const char *tag = get_value(itr->first);
|
|
|
|
if (check_nullptr(itr->second))
|
|
|
|
node.add_child(json.create_node().add_null(tag));
|
|
|
|
else
|
2017-05-14 00:16:42 +08:00
|
|
|
node.add_child(json.create_node()
|
|
|
|
.add_bool(tag, itr->second));
|
2016-10-14 11:46:09 +08:00
|
|
|
}
|
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class V>
|
|
|
|
typename enable_if<is_string<V>::value
|
|
|
|
|| is_char_ptr<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();
|
|
|
|
for (typename std::map<K, V>::const_iterator
|
|
|
|
itr = objects.begin(); itr != objects.end(); ++itr)
|
2016-10-14 11:46:09 +08:00
|
|
|
{
|
2016-10-14 22:48:32 +08:00
|
|
|
const char *tag = get_value(itr->first);
|
|
|
|
if (check_nullptr(itr->second))
|
|
|
|
node.add_child(json.create_node().add_null(tag));
|
|
|
|
else
|
2017-05-14 00:16:42 +08:00
|
|
|
node.add_child(json.create_node()
|
|
|
|
.add_text(tag, get_value(itr->second)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class V>
|
|
|
|
typename enable_if<is_string<V>::value
|
|
|
|
|| is_char_ptr<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();
|
|
|
|
for (typename std::map<K, V*>::const_iterator
|
|
|
|
itr = objects.begin(); itr != objects.end(); ++itr)
|
|
|
|
{
|
|
|
|
const char *tag = get_value(itr->first);
|
|
|
|
if (check_nullptr(itr->second))
|
|
|
|
node.add_child(json.create_node().add_null(tag));
|
|
|
|
else
|
|
|
|
node.add_child(json.create_node()
|
|
|
|
.add_text(tag, get_value(itr->second)));
|
2016-10-08 22:11:25 +08:00
|
|
|
}
|
2016-10-11 22:04:45 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, class V>
|
|
|
|
typename 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();
|
|
|
|
for (typename std::map<T, V>::const_iterator
|
|
|
|
itr = objects.begin(); itr != objects.end(); ++itr)
|
2016-10-14 11:46:09 +08:00
|
|
|
{
|
2016-10-14 22:48:32 +08:00
|
|
|
const char *tag = get_value(itr->first);
|
|
|
|
if (check_nullptr(itr->second))
|
|
|
|
node.add_child(json.create_node().add_null(tag));
|
|
|
|
else
|
2016-10-14 11:46:09 +08:00
|
|
|
{
|
2016-10-14 22:48:32 +08:00
|
|
|
acl::json_node &item = gson(json, itr->second);
|
2017-05-14 00:16:42 +08:00
|
|
|
node.add_child(json.create_node()
|
|
|
|
.add_child(tag, item));
|
2016-10-14 11:46:09 +08:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, class V>
|
|
|
|
typename enable_if<is_object<V>::value, acl::json_node &>::type
|
|
|
|
static inline gson(acl::json &json, const std::map<T, V> *objects)
|
2017-05-14 00:16:42 +08:00
|
|
|
{
|
|
|
|
return gson(json, *objects);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, class V>
|
|
|
|
typename enable_if<is_object<V>::value, acl::json_node &>::type
|
|
|
|
static inline gson(acl::json &json, const std::map<T, V*> &objects)
|
2016-10-14 22:48:32 +08:00
|
|
|
{
|
|
|
|
acl::json_node &node = json.create_array();
|
2017-05-14 00:16:42 +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
|
|
|
{
|
2016-10-14 22:48:32 +08:00
|
|
|
const char *tag = get_value(itr->first);
|
|
|
|
if (check_nullptr(itr->second))
|
|
|
|
node.add_child(json.create_node().add_null(tag));
|
|
|
|
else
|
2016-10-11 18:03:26 +08:00
|
|
|
{
|
2016-10-14 22:48:32 +08:00
|
|
|
acl::json_node &item = gson(json, itr->second);
|
2017-05-14 00:16:42 +08:00
|
|
|
node.add_child(json.create_node()
|
|
|
|
.add_child(tag, item));
|
2016-10-11 18:03:26 +08:00
|
|
|
}
|
2016-10-14 11:46:09 +08:00
|
|
|
}
|
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
return node;
|
|
|
|
}
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2017-05-14 00:16:42 +08:00
|
|
|
template<class T, class V>
|
|
|
|
typename enable_if<is_object<V>::value, acl::json_node &>::type
|
|
|
|
static inline gson(acl::json &json, const std::map<T, V*> *objects)
|
|
|
|
{
|
|
|
|
return gson(json, *objects);
|
|
|
|
}
|
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template <class T>
|
|
|
|
typename enable_if<is_object<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
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
static inline void del(T **obj)
|
|
|
|
{
|
|
|
|
delete *obj;
|
|
|
|
*obj = NULL;
|
|
|
|
}
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
static inline void del(T *obj)
|
|
|
|
{
|
|
|
|
(void) obj;
|
|
|
|
}
|
2016-10-11 22:04:45 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
//bool
|
2017-05-14 00:16:42 +08:00
|
|
|
static inline std::pair<bool, std::string>
|
|
|
|
gson(acl::json_node &node, bool *obj)
|
2016-10-14 22:48:32 +08:00
|
|
|
{
|
|
|
|
if (node.is_bool() == false)
|
|
|
|
return std::make_pair(false, "get bool failed");
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
*obj = *node.get_bool();
|
|
|
|
return std::make_pair(true, "");
|
|
|
|
}
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2017-05-14 00:16:42 +08:00
|
|
|
static inline std::pair<bool, std::string>
|
|
|
|
gson(acl::json_node &node, bool **obj)
|
2016-10-14 22:48:32 +08:00
|
|
|
{
|
2016-10-17 22:32:23 +08:00
|
|
|
*obj = NULL;
|
2016-10-14 22:48:32 +08:00
|
|
|
if (node.is_bool() == false)
|
|
|
|
return std::make_pair(false, "get bool failed");
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
*obj = new bool;
|
|
|
|
**obj = *node.get_bool();
|
2016-10-11 22:04:45 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
return std::make_pair(true, "");
|
|
|
|
}
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
//double
|
|
|
|
template <class T>
|
|
|
|
typename enable_if<is_double<T>::value,
|
|
|
|
std::pair<bool, std::string> >::type
|
|
|
|
static inline gson(acl::json_node &node, T *obj)
|
|
|
|
{
|
|
|
|
if (node.is_double() == false)
|
|
|
|
return std::make_pair(false, "get double failed");
|
2016-10-11 22:04:45 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
*obj = static_cast<T>(*node.get_double());
|
|
|
|
return std::make_pair(true, "");
|
|
|
|
}
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template <class T>
|
|
|
|
typename enable_if<is_double<T>::value,
|
|
|
|
std::pair<bool, std::string> >::type
|
|
|
|
static inline gson(acl::json_node &node, T **obj)
|
|
|
|
{
|
2016-10-17 22:32:23 +08:00
|
|
|
*obj = NULL;
|
2016-10-14 22:48:32 +08:00
|
|
|
if (node.is_double() == false)
|
|
|
|
return std::make_pair(false, "get double failed");;
|
2016-10-11 22:04:45 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
*obj = new T;
|
|
|
|
**obj = static_cast<T>(*node.get_double());
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
return std::make_pair(true, "");
|
|
|
|
}
|
2016-10-11 22:04:45 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
//intergral
|
|
|
|
template <class T>
|
|
|
|
typename enable_if<is_number<T>::value,
|
|
|
|
std::pair<bool, std::string> >::type
|
|
|
|
static inline gson(acl::json_node &node, T *obj)
|
|
|
|
{
|
|
|
|
if (node.is_number() == false)
|
|
|
|
return std::make_pair(false, "get number failed");
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
*obj = static_cast<T>(*node.get_int64());
|
|
|
|
return std::make_pair(true, "");
|
|
|
|
}
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template <class T>
|
|
|
|
typename enable_if<is_number<T>::value,
|
|
|
|
std::pair<bool, std::string> >::type
|
|
|
|
static inline gson(acl::json_node &node, T **obj)
|
|
|
|
{
|
2016-10-17 22:32:23 +08:00
|
|
|
*obj = NULL;
|
2016-10-14 22:48:32 +08:00
|
|
|
if (node.is_number() == false)
|
|
|
|
return std::make_pair(false, "get number failed");;
|
2016-10-11 22:04:45 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
*obj = new T;
|
|
|
|
**obj = static_cast<T>(*node.get_int64());
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
return std::make_pair(true, "");
|
|
|
|
}
|
2016-10-11 22:04:45 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
//string
|
2017-05-14 00:16:42 +08:00
|
|
|
static inline std::pair<bool, std::string>
|
|
|
|
gson(acl::json_node &node, char **obj)
|
2016-10-14 22:48:32 +08:00
|
|
|
{
|
2016-10-17 22:32:23 +08:00
|
|
|
*obj = NULL;
|
2016-10-14 22:48:32 +08:00
|
|
|
if (node.is_string() == false)
|
|
|
|
return std::make_pair(false, "get char * string failed");
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2017-03-31 23:04:16 +08:00
|
|
|
int len = (int) strlen(node.get_string());
|
2016-10-14 22:48:32 +08:00
|
|
|
*obj = new char[len + 1];
|
|
|
|
memcpy(*obj, node.get_string(), len);
|
|
|
|
(*obj)[len] = 0;
|
2016-10-11 22:04:45 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
return std::make_pair(true, "");
|
|
|
|
}
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2017-05-14 00:16:42 +08:00
|
|
|
static inline std::pair<bool, std::string>
|
|
|
|
gson(acl::json_node &node, acl::string &obj)
|
|
|
|
{
|
|
|
|
if (node.is_string() == false)
|
|
|
|
return std::make_pair(false, "get string failed");
|
|
|
|
obj.clear();
|
|
|
|
obj.append(node.get_string());
|
|
|
|
|
|
|
|
return std::make_pair(true, "");
|
|
|
|
}
|
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
static inline std::pair<bool, std::string>
|
2017-05-14 00:16:42 +08:00
|
|
|
gson(acl::json_node &node, acl::string *obj)
|
2016-10-14 22:48:32 +08:00
|
|
|
{
|
|
|
|
if (node.is_string() == false)
|
|
|
|
return std::make_pair(false, "get string failed");
|
2017-01-04 16:03:08 +08:00
|
|
|
obj->clear();
|
2016-10-14 22:48:32 +08:00
|
|
|
obj->append(node.get_string());
|
|
|
|
return std::make_pair(true, "");
|
|
|
|
}
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
static inline std::pair<bool, std::string>
|
2017-05-14 00:16:42 +08:00
|
|
|
gson(acl::json_node &node, acl::string **obj)
|
2016-10-14 22:48:32 +08:00
|
|
|
{
|
2016-10-17 22:32:23 +08:00
|
|
|
*obj = NULL;
|
2016-10-14 22:48:32 +08:00
|
|
|
if (node.is_string() == false)
|
|
|
|
return std::make_pair(false, "get string failed");
|
2016-10-11 22:04:45 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
*obj = new acl::string;
|
|
|
|
(*obj)->append(node.get_string());
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
return std::make_pair(true, "");
|
|
|
|
}
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
static inline std::pair<bool, std::string>
|
2017-05-14 00:16:42 +08:00
|
|
|
gson(acl::json_node &node, std::string &obj)
|
|
|
|
{
|
|
|
|
if (node.is_string() == false)
|
|
|
|
return std::make_pair(false, "get string failed");
|
|
|
|
obj.clear();
|
|
|
|
obj.append(node.get_string());
|
|
|
|
return std::make_pair(true, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline std::pair<bool, std::string>
|
|
|
|
gson(acl::json_node &node, std::string *obj)
|
2016-10-14 22:48:32 +08:00
|
|
|
{
|
|
|
|
if (node.is_string() == false)
|
|
|
|
return std::make_pair(false, "get string failed");
|
2017-01-04 16:03:08 +08:00
|
|
|
obj->clear();
|
2016-10-14 22:48:32 +08:00
|
|
|
obj->append(node.get_string());
|
|
|
|
return std::make_pair(true, "");
|
|
|
|
}
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
static inline std::pair<bool, std::string>
|
2017-05-14 00:16:42 +08:00
|
|
|
gson(acl::json_node &node, std::string **obj)
|
2016-10-14 22:48:32 +08:00
|
|
|
{
|
2016-10-17 22:32:23 +08:00
|
|
|
*obj = NULL;
|
2016-10-14 22:48:32 +08:00
|
|
|
if (node.is_string() == false)
|
|
|
|
return std::make_pair(false, "get string failed");
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
*obj = new std::string;
|
|
|
|
(*obj)->append(node.get_string());
|
2016-10-11 22:04:45 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
return std::make_pair(true, "");
|
|
|
|
}
|
|
|
|
|
2017-05-14 00:16:42 +08:00
|
|
|
// list
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
static inline std::pair<bool, std::string>
|
2017-05-14 00:16:42 +08:00
|
|
|
gson(acl::json_node &node, std::list<T> *objs)
|
2016-10-14 22:48:32 +08:00
|
|
|
{
|
|
|
|
std::pair<bool, std::string> result;
|
|
|
|
acl::json_node *itr = node.first_child();
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2018-08-02 23:00:22 +08:00
|
|
|
if (itr == NULL)
|
|
|
|
{
|
|
|
|
// maybe the vector is empty ---zsx
|
|
|
|
objs->clear();
|
|
|
|
return std::make_pair(true, "");
|
|
|
|
}
|
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
while (itr)
|
|
|
|
{
|
2017-05-14 00:16:42 +08:00
|
|
|
// for avoiding object's member pointor copy, the obj can be
|
|
|
|
// put in list first, when error happened just only erase it.
|
|
|
|
// ---lindawei
|
|
|
|
|
2017-06-14 22:01:06 +08:00
|
|
|
objs->push_back(T());
|
2017-05-14 00:16:42 +08:00
|
|
|
typename std::list<T>::iterator it = objs->end();
|
|
|
|
--it;
|
2017-06-14 13:55:05 +08:00
|
|
|
result = gson(*itr, &*it);
|
2017-05-14 00:16:42 +08:00
|
|
|
if (!result.first)
|
|
|
|
{
|
2017-06-14 13:55:05 +08:00
|
|
|
break;
|
2017-05-14 00:16:42 +08:00
|
|
|
}
|
|
|
|
itr = node.next_child();
|
|
|
|
}
|
2017-06-14 13:55:05 +08:00
|
|
|
if (result.first)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
objs->clear();
|
|
|
|
return result;
|
2017-05-14 00:16:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// list
|
|
|
|
template<class T>
|
|
|
|
static inline std::pair<bool, std::string>
|
|
|
|
gson(acl::json_node &node, std::list<T*> *objs)
|
|
|
|
{
|
2017-06-02 14:47:24 +08:00
|
|
|
std::pair<bool, std::string> result;
|
2017-05-14 00:16:42 +08:00
|
|
|
acl::json_node *itr = node.first_child();
|
|
|
|
|
2018-08-02 23:00:22 +08:00
|
|
|
if (itr == NULL)
|
|
|
|
{
|
|
|
|
// maybe the vector is empty ---zsx
|
|
|
|
objs->clear();
|
|
|
|
return std::make_pair(true, "");
|
|
|
|
}
|
|
|
|
|
2017-05-14 00:16:42 +08:00
|
|
|
while (itr)
|
|
|
|
{
|
2017-06-02 14:47:24 +08:00
|
|
|
T* obj = new T;
|
|
|
|
objs->push_back(obj);
|
2017-06-14 13:55:05 +08:00
|
|
|
result = gson(*itr, obj);
|
2017-05-14 00:16:42 +08:00
|
|
|
if (!result.first)
|
|
|
|
{
|
2017-06-14 13:55:05 +08:00
|
|
|
break;
|
2017-05-14 00:16:42 +08:00
|
|
|
}
|
2016-10-14 22:48:32 +08:00
|
|
|
itr = node.next_child();
|
2016-10-08 22:11:25 +08:00
|
|
|
}
|
2017-06-14 13:55:05 +08:00
|
|
|
if(objs->size())
|
|
|
|
{
|
|
|
|
delete *objs->begin();
|
|
|
|
objs->pop_front();
|
|
|
|
}
|
|
|
|
return result;
|
2016-10-14 22:48:32 +08:00
|
|
|
}
|
|
|
|
|
2017-05-14 00:16:42 +08:00
|
|
|
// vector
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class T>
|
|
|
|
std::pair<bool, std::string>
|
|
|
|
static inline gson(acl::json_node &node, std::vector<T> *objs)
|
|
|
|
{
|
2017-06-02 14:47:24 +08:00
|
|
|
std::pair<bool, std::string> result;
|
2016-10-14 22:48:32 +08:00
|
|
|
acl::json_node *itr = node.first_child();
|
|
|
|
|
2018-08-02 23:00:22 +08:00
|
|
|
if (itr == NULL)
|
|
|
|
{
|
|
|
|
// maybe the vector is empty ---zsx
|
|
|
|
objs->clear();
|
|
|
|
return std::make_pair(true, "");
|
|
|
|
}
|
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
while (itr)
|
2016-10-08 22:11:25 +08:00
|
|
|
{
|
2017-05-14 00:16:42 +08:00
|
|
|
// for avoiding object's member pointor copy
|
|
|
|
// ---lindawei
|
|
|
|
|
2017-06-14 22:01:06 +08:00
|
|
|
objs->push_back(T());
|
2017-05-14 00:16:42 +08:00
|
|
|
typename std::vector<T>::iterator it = objs->end();
|
|
|
|
--it;
|
2017-06-14 13:55:05 +08:00
|
|
|
result = gson(*itr, &*it);
|
2017-05-14 00:16:42 +08:00
|
|
|
if (!result.first)
|
2017-06-14 13:55:05 +08:00
|
|
|
break;
|
2017-05-14 00:16:42 +08:00
|
|
|
itr = node.next_child();
|
|
|
|
}
|
2017-06-14 13:55:05 +08:00
|
|
|
if(result.first)
|
|
|
|
return result;
|
|
|
|
objs->clear();
|
|
|
|
return result;
|
2017-05-14 00:16:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// vector
|
|
|
|
template<class T>
|
|
|
|
std::pair<bool, std::string>
|
|
|
|
static inline gson(acl::json_node &node, std::vector<T*> *objs)
|
|
|
|
{
|
2017-06-02 14:47:24 +08:00
|
|
|
std::pair<bool, std::string> result;
|
2017-05-14 00:16:42 +08:00
|
|
|
acl::json_node *itr = node.first_child();
|
|
|
|
|
2018-08-02 23:00:22 +08:00
|
|
|
if (itr == NULL)
|
|
|
|
{
|
|
|
|
// maybe the vector is empty ---zsx
|
|
|
|
objs->clear();
|
|
|
|
return std::make_pair(true, "");
|
|
|
|
}
|
|
|
|
|
2017-05-14 00:16:42 +08:00
|
|
|
while (itr)
|
|
|
|
{
|
|
|
|
T* obj = new T;
|
|
|
|
objs->push_back(obj);
|
|
|
|
typename std::vector<T*>::iterator it = objs->end();
|
|
|
|
--it;
|
2017-06-02 14:47:24 +08:00
|
|
|
result = gson(*itr, *it);
|
2017-06-01 15:55:48 +08:00
|
|
|
if (!result.first)
|
|
|
|
{
|
2017-06-14 13:55:05 +08:00
|
|
|
break;
|
2017-06-01 15:55:48 +08:00
|
|
|
}
|
|
|
|
itr = node.next_child();
|
|
|
|
}
|
2017-06-14 13:55:05 +08:00
|
|
|
if(result.first)
|
|
|
|
return result;
|
2017-06-01 15:55:48 +08:00
|
|
|
|
2017-06-14 13:55:05 +08:00
|
|
|
while(objs->size())
|
|
|
|
{
|
2017-06-16 12:01:46 +08:00
|
|
|
T *obj = (*objs)[0];
|
|
|
|
delete obj;
|
2017-06-14 13:55:05 +08:00
|
|
|
objs->pop_back();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//set
|
|
|
|
template<class T>
|
|
|
|
std::pair<bool, std::string>
|
|
|
|
static inline gson(acl::json_node &node, std::set<T*> *objs)
|
|
|
|
{
|
|
|
|
std::pair<bool, std::string> result;
|
|
|
|
acl::json_node *itr = node.first_child();
|
|
|
|
|
2018-08-02 23:00:22 +08:00
|
|
|
if (itr == NULL)
|
|
|
|
{
|
|
|
|
// maybe the vector is empty ---zsx
|
|
|
|
objs->clear();
|
|
|
|
return std::make_pair(true, "");
|
|
|
|
}
|
|
|
|
|
2017-06-14 13:55:05 +08:00
|
|
|
while (itr)
|
|
|
|
{
|
|
|
|
T* obj = new T;
|
|
|
|
result = gson(*itr, obj);
|
|
|
|
if (!result.first)
|
|
|
|
{
|
|
|
|
delete obj;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
objs->insert(obj);
|
|
|
|
itr = node.next_child();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.first)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
while(objs->size())
|
|
|
|
{
|
|
|
|
T* obj = *objs->begin();
|
|
|
|
objs->erase(objs->begin());
|
|
|
|
delete obj;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
std::pair<bool, std::string>
|
|
|
|
static inline gson(acl::json_node &node, std::set<T> *objs)
|
|
|
|
{
|
|
|
|
std::pair<bool, std::string> result;
|
|
|
|
acl::json_node *itr = node.first_child();
|
|
|
|
|
2018-08-02 23:00:22 +08:00
|
|
|
if (itr == NULL)
|
|
|
|
{
|
|
|
|
// maybe the vector is empty ---zsx
|
|
|
|
objs->clear();
|
|
|
|
return std::make_pair(true, "");
|
|
|
|
}
|
|
|
|
|
2017-06-14 13:55:05 +08:00
|
|
|
while (itr)
|
|
|
|
{
|
2017-06-14 22:01:06 +08:00
|
|
|
T obj = T();
|
2017-06-14 13:55:05 +08:00
|
|
|
result = gson(*itr, &obj);
|
|
|
|
if (!result.first)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
objs->insert(obj);
|
|
|
|
itr = node.next_child();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.first)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
objs->clear();
|
|
|
|
return result;
|
2017-06-01 15:55:48 +08:00
|
|
|
}
|
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
template <class T>
|
|
|
|
typename enable_if<is_object<T>::value,
|
|
|
|
std::pair<bool, std::string> >::type
|
|
|
|
static inline gson(acl::json_node &node, T **obj)
|
|
|
|
{
|
|
|
|
*obj = new T();
|
|
|
|
std::pair<bool, std::string> result = gson(node, *obj);
|
|
|
|
if (result.first == false)
|
|
|
|
{
|
|
|
|
delete *obj;
|
|
|
|
*obj = NULL;
|
2016-10-08 22:11:25 +08:00
|
|
|
}
|
2016-10-11 22:04:45 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
return result;
|
|
|
|
}
|
2016-10-08 22:11:25 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
///////////////////////////////////////////map////////////////////////////////
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2017-05-14 00:16:42 +08:00
|
|
|
// int map
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class K, class T>
|
|
|
|
typename 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
|
|
|
|
static inline expand(acl::json_node &node, std::map<K, T> *objs)
|
|
|
|
{
|
2017-06-02 14:47:24 +08:00
|
|
|
std::pair<bool, std::string> result;
|
2016-10-14 22:48:32 +08:00
|
|
|
acl::json_node *itr = node.first_child();
|
|
|
|
while (itr)
|
2016-10-14 11:46:09 +08:00
|
|
|
{
|
2016-10-14 22:48:32 +08:00
|
|
|
T obj;
|
|
|
|
result = gson(*itr, &obj);
|
|
|
|
if (!result.first)
|
|
|
|
break;
|
|
|
|
|
|
|
|
objs->insert(std::make_pair(K(itr->tag_name()), obj));
|
|
|
|
itr = node.next_child();
|
|
|
|
}
|
2017-06-02 14:47:24 +08:00
|
|
|
|
|
|
|
if (result.first)
|
|
|
|
return std::make_pair(true, "");
|
|
|
|
|
|
|
|
for (typename std::map<K, T>::iterator it = objs->begin();
|
|
|
|
it != objs->end(); ++it)
|
2016-10-14 22:48:32 +08:00
|
|
|
{
|
2017-06-02 14:47:24 +08:00
|
|
|
del(&it->second);
|
2016-10-14 11:46:09 +08:00
|
|
|
}
|
2017-06-02 14:47:24 +08:00
|
|
|
|
|
|
|
objs->clear();
|
2016-10-14 22:48:32 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class T>
|
|
|
|
typename enable_if <is_object<T>::value ,
|
|
|
|
std::pair<bool, std::string > > ::type
|
|
|
|
static inline expand(acl::json_node &node, std::map<K, T> *objs)
|
|
|
|
{
|
2017-06-02 14:47:24 +08:00
|
|
|
std::pair<bool, std::string> result;
|
2016-10-14 22:48:32 +08:00
|
|
|
acl::json_node *itr = node.first_child();
|
|
|
|
|
|
|
|
while (itr && itr->get_obj())
|
2016-10-08 22:11:25 +08:00
|
|
|
{
|
2016-10-14 22:48:32 +08:00
|
|
|
T obj;
|
|
|
|
result = gson(*(itr->get_obj()), &obj);
|
|
|
|
if (!result.first)
|
|
|
|
break;
|
2016-10-14 11:46:09 +08:00
|
|
|
|
2016-10-14 22:48:32 +08:00
|
|
|
objs->insert(std::make_pair(K(itr->tag_name()), obj));
|
|
|
|
itr = node.next_child();
|
2016-10-08 22:11:25 +08:00
|
|
|
}
|
2016-10-11 22:04:45 +08:00
|
|
|
|
2017-06-02 14:47:24 +08:00
|
|
|
if (result.first)
|
|
|
|
return std::make_pair(true, "");
|
|
|
|
|
|
|
|
for (typename std::map<K, T>::iterator itr2 = objs->begin();
|
|
|
|
itr2 != objs->end(); ++itr2)
|
2016-10-14 11:46:09 +08:00
|
|
|
{
|
2017-06-02 14:47:24 +08:00
|
|
|
del(&itr2->second);
|
2016-10-14 22:48:32 +08:00
|
|
|
}
|
2017-06-02 14:47:24 +08:00
|
|
|
|
|
|
|
objs->clear();
|
2016-10-14 22:48:32 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-05-14 00:16:42 +08:00
|
|
|
// map
|
2016-10-14 22:48:32 +08:00
|
|
|
template<class K, class V>
|
|
|
|
std::pair<bool, std::string>
|
|
|
|
static inline gson(acl::json_node &node, std::map<K, V> *objs)
|
|
|
|
{
|
2017-06-02 14:47:24 +08:00
|
|
|
std::pair<bool, std::string> result;
|
2016-10-14 22:48:32 +08:00
|
|
|
acl::json_node *itr = node.first_child();
|
2017-06-02 14:47:24 +08:00
|
|
|
std::string error_string;
|
2016-10-14 22:48:32 +08:00
|
|
|
|
|
|
|
while (itr)
|
|
|
|
{
|
|
|
|
result = expand(*itr, objs);
|
|
|
|
if (result.first == false)
|
2017-06-02 14:47:24 +08:00
|
|
|
error_string.append(result.second);
|
2016-10-14 22:48:32 +08:00
|
|
|
itr = node.next_child();
|
2016-10-14 11:46:09 +08:00
|
|
|
}
|
2017-06-02 14:47:24 +08:00
|
|
|
|
|
|
|
return std::make_pair(!!!objs->empty(), error_string);
|
2016-10-14 22:48:32 +08:00
|
|
|
}
|
|
|
|
|
2017-05-14 00:16:42 +08:00
|
|
|
template<class K, class T>
|
|
|
|
typename 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
|
|
|
|
static inline expand(acl::json_node &node, std::map<K, T*> *objs)
|
|
|
|
{
|
2017-06-02 14:47:24 +08:00
|
|
|
std::pair<bool, std::string> result;
|
2017-05-14 00:16:42 +08:00
|
|
|
acl::json_node *itr = node.first_child();
|
|
|
|
while (itr)
|
|
|
|
{
|
|
|
|
T* obj = new T;
|
|
|
|
result = gson(*itr, obj);
|
|
|
|
if (!result.first)
|
|
|
|
break;
|
|
|
|
|
2017-06-02 14:47:24 +08:00
|
|
|
objs->insert(std::make_pair(K(itr->tag_name()), obj));
|
2017-05-14 00:16:42 +08:00
|
|
|
itr = node.next_child();
|
|
|
|
}
|
|
|
|
|
2017-06-02 14:47:24 +08:00
|
|
|
if (result.first)
|
|
|
|
return std::make_pair(true, "");
|
|
|
|
|
|
|
|
for (typename std::map<K, T*>::iterator it = objs->begin();
|
|
|
|
it != objs->end(); ++it)
|
2017-05-14 00:16:42 +08:00
|
|
|
{
|
2017-06-14 13:55:05 +08:00
|
|
|
del(&it->second);
|
2017-05-14 00:16:42 +08:00
|
|
|
}
|
|
|
|
|
2017-06-02 14:47:24 +08:00
|
|
|
objs->clear();
|
2017-05-14 00:16:42 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class T>
|
|
|
|
typename enable_if <is_object<T>::value ,
|
|
|
|
std::pair<bool, std::string > > ::type
|
|
|
|
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();
|
|
|
|
|
|
|
|
while (itr && itr->get_obj())
|
|
|
|
{
|
|
|
|
T* obj = new T;
|
|
|
|
result = gson(*(itr->get_obj()), obj);
|
|
|
|
if (!result.first)
|
|
|
|
break;
|
|
|
|
|
2017-06-02 14:47:24 +08:00
|
|
|
objs->insert(std::make_pair(K(itr->tag_name()), obj));
|
2017-05-14 00:16:42 +08:00
|
|
|
itr = node.next_child();
|
|
|
|
}
|
2017-06-02 14:47:24 +08:00
|
|
|
|
|
|
|
if (result.first)
|
|
|
|
return std::make_pair(true, "");
|
|
|
|
|
|
|
|
for (typename std::map<K, T*>::iterator itr2 = objs->begin();
|
|
|
|
itr2 != objs->end(); ++itr2)
|
2017-05-14 00:16:42 +08:00
|
|
|
{
|
2017-06-14 13:55:05 +08:00
|
|
|
del(&itr2->second);
|
2017-05-14 00:16:42 +08:00
|
|
|
}
|
2017-06-02 14:47:24 +08:00
|
|
|
|
|
|
|
objs->clear();
|
2017-05-14 00:16:42 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
while (itr)
|
|
|
|
{
|
|
|
|
result = expand(*itr, objs);
|
|
|
|
if (result.first == false)
|
|
|
|
error_string.append(result.second);
|
|
|
|
itr = node.next_child();
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_pair(!!!objs->empty(), error_string);
|
|
|
|
}
|
|
|
|
|
2016-10-08 22:11:25 +08:00
|
|
|
} // namespace acl
|