Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
to-json-visitor.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License
17  */
18 
25 #ifndef CARGO_JSON_TO_JSON_VISITOR_HPP
26 #define CARGO_JSON_TO_JSON_VISITOR_HPP
27 
28 #include "cargo/is-visitable.hpp"
29 #include "cargo/is-like-tuple.hpp"
30 #include "cargo/exception.hpp"
31 #include "cargo/visit-fields.hpp"
32 
33 #include <array>
34 #include <string>
35 #include <vector>
36 #include <map>
37 #include <utility>
38 
39 #include <json.h>
40 
41 namespace cargo {
42 
44 
45 public:
47  : mObject(json_object_new_object())
48  {
49  }
50 
51  ToJsonVisitor(const ToJsonVisitor& visitor)
52  : mObject(json_object_get(visitor.mObject))
53  {
54  }
55 
57  {
58  json_object_put(mObject);
59  }
60 
61  ToJsonVisitor& operator=(const ToJsonVisitor&) = delete;
62 
63  std::string toString() const
64  {
65  return json_object_to_json_string(mObject);
66  }
67 
68  template<typename T>
69  void visit(const std::string& name, const T& value)
70  {
71  json_object_object_add(mObject, name.c_str(), toJsonObject(value));
72  }
73 private:
74  json_object* mObject;
75 
76 
77  json_object* detach()
78  {
79  json_object* ret = mObject;
80  mObject = nullptr;
81  return ret;
82  }
83 
84  template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
85  static json_object* toJsonObject(T value)
86  {
87  return toJsonObject(static_cast<int64_t>(value));
88  }
89 
90  static json_object* toJsonObject(std::int64_t value)
91  {
92  return json_object_new_int64(value);
93  }
94 
95  static json_object* toJsonObject(std::uint64_t value)
96  {
97  if (value > INT64_MAX) {
98  throw CargoException("Value out of range");
99  }
100  return json_object_new_int64(value);
101  }
102 
103  static json_object* toJsonObject(bool value)
104  {
105  return json_object_new_boolean(value);
106  }
107 
108  static json_object* toJsonObject(double value)
109  {
110 #if JSON_C_MINOR_VERSION >= 12
111  return json_object_new_double_s(value, std::to_string(value).c_str());
112 #else
113  return json_object_new_double(value);
114 #endif
115  }
116 
117  static json_object* toJsonObject(const std::string& value)
118  {
119  return json_object_new_string(value.c_str());
120  }
121 
122  static json_object* toJsonObject(char* value)
123  {
124  return json_object_new_string(value);
125  }
126 
127  template<typename T>
128  static json_object* toJsonObject(const std::vector<T>& value)
129  {
130  json_object* array = json_object_new_array();
131  for (const T& v : value) {
132  json_object_array_add(array, toJsonObject(v));
133  }
134  return array;
135  }
136 
137  template<typename T, std::size_t N>
138  static json_object* toJsonObject(const std::array<T, N>& values)
139  {
140  json_object* array = json_object_new_array();
141  for (const T& v : values) {
142  json_object_array_add(array, toJsonObject(v));
143  }
144  return array;
145  }
146 
147  template<typename V>
148  static json_object* toJsonObject(const std::map<std::string, V>& values)
149  {
150  json_object* newMap = json_object_new_object();
151  for (const auto& item : values) {
152  json_object_object_add(newMap, item.first.c_str(), toJsonObject(item.second));
153  }
154  return newMap;
155  }
156 
158  {
159  template<typename T>
160  static void visit(json_object* array, const T& value)
161  {
162  json_object_array_add(array, toJsonObject(value));
163  }
164  };
165 
166  template<typename T, typename std::enable_if<isLikeTuple<T>::value, int>::type = 0>
167  static json_object* toJsonObject(const T& values)
168  {
169  json_object* array = json_object_new_array();
170 
171  HelperVisitor visitor;
172  visitFields(values, &visitor, array);
173  return array;
174  }
175 
176  template<typename T, typename std::enable_if<isVisitable<T>::value, int>::type = 0>
177  static json_object* toJsonObject(const T& value)
178  {
179  ToJsonVisitor visitor;
180  value.accept(visitor);
181  return visitor.detach();
182  }
183 
184  template<typename T, typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
185  static json_object* toJsonObject(const T& value)
186  {
187  return toJsonObject(static_cast<const typename std::underlying_type<T>::type>(value));
188  }
189 
190 };
191 
192 } // namespace cargo
193 
194 #endif // CARGO_JSON_TO_JSON_VISITOR_HPP
static json_object * toJsonObject(const std::map< std::string, V > &values)
Definition: to-json-visitor.hpp:148
Internal configuration helper.
static json_object * toJsonObject(const std::array< T, N > &values)
Definition: to-json-visitor.hpp:138
void visit(const std::string &name, const T &value)
Definition: to-json-visitor.hpp:69
Helper function for iterating tuples, pairs and arrays.
ToJsonVisitor(const ToJsonVisitor &visitor)
Definition: to-json-visitor.hpp:51
json_object * mObject
Definition: to-json-visitor.hpp:74
Definition: to-json-visitor.hpp:157
Tuple or pair checker.
static void visit(json_object *array, const T &value)
Definition: to-json-visitor.hpp:160
static json_object * toJsonObject(T value)
Definition: to-json-visitor.hpp:85
ToJsonVisitor()
Definition: to-json-visitor.hpp:46
ToJsonVisitor & operator=(const ToJsonVisitor &)=delete
static json_object * toJsonObject(const T &values)
Definition: to-json-visitor.hpp:167
static json_object * toJsonObject(char *value)
Definition: to-json-visitor.hpp:122
Exceptions for libcargo.
json_object * detach()
Definition: to-json-visitor.hpp:77
Base class for exceptions in libcargo.
Definition: exception.hpp:35
static json_object * toJsonObject(const std::string &value)
Definition: to-json-visitor.hpp:117
static json_object * toJsonObject(std::uint64_t value)
Definition: to-json-visitor.hpp:95
static json_object * toJsonObject(const std::vector< T > &value)
Definition: to-json-visitor.hpp:128
static json_object * toJsonObject(const T &value)
Definition: to-json-visitor.hpp:177
std::string toString() const
Definition: to-json-visitor.hpp:63
static json_object * toJsonObject(std::int64_t value)
Definition: to-json-visitor.hpp:90
static json_object * toJsonObject(double value)
Definition: to-json-visitor.hpp:108
~ToJsonVisitor()
Definition: to-json-visitor.hpp:56
static json_object * toJsonObject(bool value)
Definition: to-json-visitor.hpp:103
void visitFields(T &t, F f, A...args)
Definition: visit-fields.hpp:58
Definition: to-json-visitor.hpp:43