Horizon
from_json.hpp
1 #pragma once
2 
3 #include <algorithm> // transform
4 #include <array> // array
5 #include <ciso646> // and, not
6 #include <forward_list> // forward_list
7 #include <iterator> // inserter, front_inserter, end
8 #include <string> // string
9 #include <tuple> // tuple, make_tuple
10 #include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
11 #include <utility> // pair, declval
12 #include <valarray> // valarray
13 
14 #include <nlohmann/detail/exceptions.hpp>
15 #include <nlohmann/detail/macro_scope.hpp>
16 #include <nlohmann/detail/meta.hpp>
17 #include <nlohmann/detail/value_t.hpp>
18 
19 namespace nlohmann
20 {
21 namespace detail
22 {
23 // overloads for basic_json template parameters
24 template<typename BasicJsonType, typename ArithmeticType,
25  enable_if_t<std::is_arithmetic<ArithmeticType>::value and
26  not std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
27  int> = 0>
28 void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
29 {
30  switch (static_cast<value_t>(j))
31  {
33  {
34  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
35  break;
36  }
38  {
39  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
40  break;
41  }
43  {
44  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
45  break;
46  }
47 
48  default:
49  JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name())));
50  }
51 }
52 
53 template<typename BasicJsonType>
54 void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
55 {
56  if (JSON_UNLIKELY(not j.is_boolean()))
57  {
58  JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name())));
59  }
60  b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
61 }
62 
63 template<typename BasicJsonType>
64 void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
65 {
66  if (JSON_UNLIKELY(not j.is_string()))
67  {
68  JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name())));
69  }
70  s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
71 }
72 
73 template<typename BasicJsonType>
74 void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
75 {
76  get_arithmetic_value(j, val);
77 }
78 
79 template<typename BasicJsonType>
80 void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
81 {
82  get_arithmetic_value(j, val);
83 }
84 
85 template<typename BasicJsonType>
86 void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
87 {
88  get_arithmetic_value(j, val);
89 }
90 
91 template<typename BasicJsonType, typename EnumType,
92  enable_if_t<std::is_enum<EnumType>::value, int> = 0>
93 void from_json(const BasicJsonType& j, EnumType& e)
94 {
95  typename std::underlying_type<EnumType>::type val;
96  get_arithmetic_value(j, val);
97  e = static_cast<EnumType>(val);
98 }
99 
100 template<typename BasicJsonType>
101 void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr)
102 {
103  if (JSON_UNLIKELY(not j.is_array()))
104  {
105  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
106  }
107  arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
108 }
109 
110 // forward_list doesn't have an insert method
111 template<typename BasicJsonType, typename T, typename Allocator,
112  enable_if_t<std::is_convertible<BasicJsonType, T>::value, int> = 0>
113 void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
114 {
115  if (JSON_UNLIKELY(not j.is_array()))
116  {
117  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
118  }
119  std::transform(j.rbegin(), j.rend(),
120  std::front_inserter(l), [](const BasicJsonType & i)
121  {
122  return i.template get<T>();
123  });
124 }
125 
126 // valarray doesn't have an insert method
127 template<typename BasicJsonType, typename T,
128  enable_if_t<std::is_convertible<BasicJsonType, T>::value, int> = 0>
129 void from_json(const BasicJsonType& j, std::valarray<T>& l)
130 {
131  if (JSON_UNLIKELY(not j.is_array()))
132  {
133  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
134  }
135  l.resize(j.size());
136  std::copy(j.m_value.array->begin(), j.m_value.array->end(), std::begin(l));
137 }
138 
139 template<typename BasicJsonType, typename CompatibleArrayType>
140 void from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, priority_tag<0> /*unused*/)
141 {
142  using std::end;
143 
144  std::transform(j.begin(), j.end(),
145  std::inserter(arr, end(arr)), [](const BasicJsonType & i)
146  {
147  // get<BasicJsonType>() returns *this, this won't call a from_json
148  // method when value_type is BasicJsonType
149  return i.template get<typename CompatibleArrayType::value_type>();
150  });
151 }
152 
153 template<typename BasicJsonType, typename CompatibleArrayType>
154 auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, priority_tag<1> /*unused*/)
155 -> decltype(
156  arr.reserve(std::declval<typename CompatibleArrayType::size_type>()),
157  void())
158 {
159  using std::end;
160 
161  arr.reserve(j.size());
162  std::transform(j.begin(), j.end(),
163  std::inserter(arr, end(arr)), [](const BasicJsonType & i)
164  {
165  // get<BasicJsonType>() returns *this, this won't call a from_json
166  // method when value_type is BasicJsonType
167  return i.template get<typename CompatibleArrayType::value_type>();
168  });
169 }
170 
171 template<typename BasicJsonType, typename T, std::size_t N>
172 void from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr, priority_tag<2> /*unused*/)
173 {
174  for (std::size_t i = 0; i < N; ++i)
175  {
176  arr[i] = j.at(i).template get<T>();
177  }
178 }
179 
180 template<typename BasicJsonType, typename CompatibleArrayType,
181  enable_if_t<is_compatible_array_type<BasicJsonType, CompatibleArrayType>::value and
182  std::is_convertible<BasicJsonType, typename CompatibleArrayType::value_type>::value and
183  not std::is_same<typename BasicJsonType::array_t, CompatibleArrayType>::value, int> = 0>
184 void from_json(const BasicJsonType& j, CompatibleArrayType& arr)
185 {
186  if (JSON_UNLIKELY(not j.is_array()))
187  {
188  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
189  }
190 
191  from_json_array_impl(j, arr, priority_tag<2> {});
192 }
193 
194 template<typename BasicJsonType, typename CompatibleObjectType,
195  enable_if_t<is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value, int> = 0>
196 void from_json(const BasicJsonType& j, CompatibleObjectType& obj)
197 {
198  if (JSON_UNLIKELY(not j.is_object()))
199  {
200  JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name())));
201  }
202 
203  auto inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
204  using value_type = typename CompatibleObjectType::value_type;
205  std::transform(
206  inner_object->begin(), inner_object->end(),
207  std::inserter(obj, obj.begin()),
208  [](typename BasicJsonType::object_t::value_type const & p)
209  {
210  return value_type(p.first, p.second.template get<typename CompatibleObjectType::mapped_type>());
211  });
212 }
213 
214 // overload for arithmetic types, not chosen for basic_json template arguments
215 // (BooleanType, etc..); note: Is it really necessary to provide explicit
216 // overloads for boolean_t etc. in case of a custom BooleanType which is not
217 // an arithmetic type?
218 template<typename BasicJsonType, typename ArithmeticType,
219  enable_if_t <
220  std::is_arithmetic<ArithmeticType>::value and
221  not std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value and
222  not std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value and
223  not std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value and
224  not std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
225  int> = 0>
226 void from_json(const BasicJsonType& j, ArithmeticType& val)
227 {
228  switch (static_cast<value_t>(j))
229  {
231  {
232  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
233  break;
234  }
236  {
237  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
238  break;
239  }
241  {
242  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
243  break;
244  }
245  case value_t::boolean:
246  {
247  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
248  break;
249  }
250 
251  default:
252  JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name())));
253  }
254 }
255 
256 template<typename BasicJsonType, typename A1, typename A2>
257 void from_json(const BasicJsonType& j, std::pair<A1, A2>& p)
258 {
259  p = {j.at(0).template get<A1>(), j.at(1).template get<A2>()};
260 }
261 
262 template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
263 void from_json_tuple_impl(const BasicJsonType& j, Tuple& t, index_sequence<Idx...>)
264 {
265  t = std::make_tuple(j.at(Idx).template get<typename std::tuple_element<Idx, Tuple>::type>()...);
266 }
267 
268 template<typename BasicJsonType, typename... Args>
269 void from_json(const BasicJsonType& j, std::tuple<Args...>& t)
270 {
271  from_json_tuple_impl(j, t, index_sequence_for<Args...> {});
272 }
273 
275 {
276  private:
277  template<typename BasicJsonType, typename T>
278  auto call(const BasicJsonType& j, T& val, priority_tag<1> /*unused*/) const
279  noexcept(noexcept(from_json(j, val)))
280  -> decltype(from_json(j, val), void())
281  {
282  return from_json(j, val);
283  }
284 
285  template<typename BasicJsonType, typename T>
286  void call(const BasicJsonType& /*unused*/, T& /*unused*/, priority_tag<0> /*unused*/) const noexcept
287  {
288  static_assert(sizeof(BasicJsonType) == 0,
289  "could not find from_json() method in T's namespace");
290 #ifdef _MSC_VER
291  // MSVC does not show a stacktrace for the above assert
292  using decayed = uncvref_t<T>;
293  static_assert(sizeof(typename decayed::force_msvc_stacktrace) == 0,
294  "forcing MSVC stacktrace to show which T we're talking about.");
295 #endif
296  }
297 
298  public:
299  template<typename BasicJsonType, typename T>
300  void operator()(const BasicJsonType& j, T& val) const
301  noexcept(noexcept(std::declval<from_json_fn>().call(j, val, priority_tag<1> {})))
302  {
303  return call(j, val, priority_tag<1> {});
304  }
305 };
306 }
307 
311 namespace
312 {
313 constexpr const auto& from_json = detail::static_const<detail::from_json_fn>::value;
314 }
315 }
nlohmann::detail::priority_tag
Definition: meta.hpp:92
nlohmann::detail::priority_tag< 0 >
Definition: meta.hpp:93
nlohmann
namespace for Niels Lohmann
Definition: adl_serializer.hpp:8
nlohmann::detail::value_t::number_float
number value (floating-point)
nlohmann::detail::value_t::number_integer
number value (signed integer)
nlohmann::detail::value_t::string
string value
nlohmann::detail::index_sequence
Definition: meta.hpp:43
nlohmann::detail::value_t::number_unsigned
number value (unsigned integer)
nlohmann::detail::value_t::boolean
boolean value
nlohmann::detail::from_json_fn
Definition: from_json.hpp:274
nlohmann::detail::make_index_sequence
Definition: meta.hpp:61
nlohmann::detail::static_const
Definition: meta.hpp:250