2 #include <boost/property_tree/json_parser.hpp>
3 #include <boost/property_tree/ptree.hpp>
4 #include <ff/util/ntobject.h>
5 #include <glog/logging.h>
12 template <
typename T,
typename =
int>
struct has_name : std::false_type {};
14 struct has_name<T, decltype((void)T::name, 0)> : std::true_type {};
16 template <
typename CT,
22 constexpr
static const char *name = ff::util::internal::nt_traits<CT>::name;
25 template <
typename CType,
26 typename UnderlyingType =
27 typename ff::util::internal::nt_traits<CType>::type>
29 template <
typename NtObjTy>
30 static void write(boost::property_tree::ptree &pt,
const NtObjTy &data) {
34 template <
typename CType,
35 typename UnderlyingType =
36 typename ff::util::internal::nt_traits<CType>::type>
38 template <
typename NtObjTy>
39 static void read(
const boost::property_tree::ptree &pt, NtObjTy &data) {
41 data.template set<CType>(
44 LOG(WARNING) <<
"cannot find json item with name: "
46 <<
", use default value: " << UnderlyingType();
51 static boost::property_tree::ptree f(
const T &value) {
52 boost::property_tree::ptree child(std::to_string(value));
58 static T f(
const boost::property_tree::ptree &ptree) {
67 template <
typename CType,
typename T>
69 template <
typename NtObjTy>
70 static void write(boost::property_tree::ptree &pt,
const NtObjTy &data) {
71 boost::property_tree::ptree ptree;
72 const std::vector<T> &vec = data.template get<CType>();
73 for (
auto item : vec) {
75 ptree.push_back(std::make_pair(
"", child));
81 template <
typename CType,
typename... ARGS>
83 template <
typename NtObjTy>
84 static void write(boost::property_tree::ptree &pt,
const NtObjTy &data) {
85 boost::property_tree::ptree ptree =
86 to_ptree<ff::util::ntobject<ARGS...>>::f(data.template get<CType>());
91 template <
typename CType,
typename T>
93 template <
typename NtObjTy>
94 static void read(
const boost::property_tree::ptree &pt, NtObjTy &data) {
96 LOG(WARNING) <<
"cannot find json item with name: "
102 for (
auto it = ptree.begin(); it != ptree.end(); ++it) {
106 data.template set<CType>(std::move(d));
109 template <
typename CType,
typename... ARGS>
111 template <
typename NtObjTy>
112 static void read(
const boost::property_tree::ptree &pt, NtObjTy &data) {
114 LOG(WARNING) <<
"cannot find json item with name: "
119 ff::util::ntobject<ARGS...> d =
120 from_ptree<ff::util::ntobject<ARGS...>>::f(ptree);
121 data.template set<CType>(std::move(d));
126 template <
typename NtObjTy>
127 static auto write(boost::property_tree::ptree &pt,
const NtObjTy &data) ->
128 typename std::enable_if<(NtObjTy::type_list::len > Index),
void>::type {
129 using ctype =
typename ff::util::get_type_at_index_in_typelist<
130 typename NtObjTy::type_list, Index>::type;
134 template <
typename NtObjTy>
135 static auto write(boost::property_tree::ptree &pt,
const NtObjTy &data) ->
136 typename std::enable_if<(NtObjTy::type_list::len <= Index), void>::type {}
139 template <
typename... ARGS>
struct to_ptree<ff::util::ntobject<ARGS...>> {
140 static boost::property_tree::ptree
141 f(
const ff::util::ntobject<ARGS...> &value) {
142 boost::property_tree::ptree child;
149 template <
typename NtObjTy>
150 static auto read(
const boost::property_tree::ptree &pt, NtObjTy &data) ->
151 typename std::enable_if<(NtObjTy::type_list::len > Index),
void>::type {
152 using ctype =
typename ff::util::get_type_at_index_in_typelist<
153 typename NtObjTy::type_list, Index>::type;
154 using vtype =
typename ff::util::internal::nt_traits<ctype>::type;
158 template <
typename NtObjTy>
159 static auto read(
const boost::property_tree::ptree &pt, NtObjTy &data) ->
160 typename std::enable_if<(NtObjTy::type_list::len <= Index), void>::type {}
163 template <
typename... ARGS>
struct from_ptree<ff::util::ntobject<ARGS...>> {
164 static ff::util::ntobject<ARGS...>
165 f(
const boost::property_tree::ptree &ptree) {
166 ff::util::ntobject<ARGS...> ret;
176 template <
typename NtObjTy>
static std::string to_json(
const NtObjTy &data) {
177 boost::property_tree::ptree pt;
179 std::stringstream ss;
180 boost::property_tree::write_json(ss, pt);
183 template <
typename NtObjTy>
184 static void to_json_file(
const NtObjTy &data,
const std::string &path) {
185 boost::property_tree::ptree pt;
187 boost::property_tree::json_parser::write_json(path, pt);
190 template <
typename NtObjTy>
191 static NtObjTy from_json(
const std::string &json_string) {
192 std::stringstream ss;
194 boost::property_tree::ptree pt;
195 boost::property_tree::read_json(ss, pt);
201 template <
typename NtObjTy>
202 static NtObjTy from_json_file(
const std::string &path) {
203 boost::property_tree::ptree pt;
204 boost::property_tree::json_parser::read_json(path, pt);