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 <map> // map
9 #include <string> // string
10 #include <tuple> // tuple, make_tuple
11 #include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
12 #include <unordered_map> // unordered_map
13 #include <utility> // pair, declval
14 #include <valarray> // valarray
15 
16 #include <nlohmann/detail/exceptions.hpp>
17 #include <nlohmann/detail/macro_scope.hpp>
18 #include <nlohmann/detail/meta/cpp_future.hpp>
19 #include <nlohmann/detail/meta/type_traits.hpp>
20 #include <nlohmann/detail/value_t.hpp>
21 
22 namespace nlohmann
23 {
24 namespace detail
25 {
26 template<typename BasicJsonType>
27 void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
28 {
29  if (JSON_HEDLEY_UNLIKELY(not j.is_null()))
30  {
31  JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name())));
32  }
33  n = nullptr;
34 }
35 
36 // overloads for basic_json template parameters
37 template<typename BasicJsonType, typename ArithmeticType,
38  enable_if_t<std::is_arithmetic<ArithmeticType>::value and
39  not std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
40  int> = 0>
41 void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
42 {
43  switch (static_cast<value_t>(j))
44  {
46  {
47  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
48  break;
49  }
51  {
52  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
53  break;
54  }
56  {
57  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
58  break;
59  }
60 
61  default:
62  JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name())));
63  }
64 }
65 
66 template<typename BasicJsonType>
67 void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
68 {
69  if (JSON_HEDLEY_UNLIKELY(not j.is_boolean()))
70  {
71  JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name())));
72  }
73  b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
74 }
75 
76 template<typename BasicJsonType>
77 void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
78 {
79  if (JSON_HEDLEY_UNLIKELY(not j.is_string()))
80  {
81  JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name())));
82  }
83  s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
84 }
85 
86 template <
87  typename BasicJsonType, typename ConstructibleStringType,
88  enable_if_t <
89  is_constructible_string_type<BasicJsonType, ConstructibleStringType>::value and
90  not std::is_same<typename BasicJsonType::string_t,
91  ConstructibleStringType>::value,
92  int > = 0 >
93 void from_json(const BasicJsonType& j, ConstructibleStringType& s)
94 {
95  if (JSON_HEDLEY_UNLIKELY(not j.is_string()))
96  {
97  JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name())));
98  }
99 
100  s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
101 }
102 
103 template<typename BasicJsonType>
104 void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
105 {
106  get_arithmetic_value(j, val);
107 }
108 
109 template<typename BasicJsonType>
110 void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
111 {
112  get_arithmetic_value(j, val);
113 }
114 
115 template<typename BasicJsonType>
116 void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
117 {
118  get_arithmetic_value(j, val);
119 }
120 
121 template<typename BasicJsonType, typename EnumType,
122  enable_if_t<std::is_enum<EnumType>::value, int> = 0>
123 void from_json(const BasicJsonType& j, EnumType& e)
124 {
125  typename std::underlying_type<EnumType>::type val;
126  get_arithmetic_value(j, val);
127  e = static_cast<EnumType>(val);
128 }
129 
130 // forward_list doesn't have an insert method
131 template<typename BasicJsonType, typename T, typename Allocator,
132  enable_if_t<std::is_convertible<BasicJsonType, T>::value, int> = 0>
133 void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
134 {
135  if (JSON_HEDLEY_UNLIKELY(not j.is_array()))
136  {
137  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
138  }
139  l.clear();
140  std::transform(j.rbegin(), j.rend(),
141  std::front_inserter(l), [](const BasicJsonType & i)
142  {
143  return i.template get<T>();
144  });
145 }
146 
147 // valarray doesn't have an insert method
148 template<typename BasicJsonType, typename T,
149  enable_if_t<std::is_convertible<BasicJsonType, T>::value, int> = 0>
150 void from_json(const BasicJsonType& j, std::valarray<T>& l)
151 {
152  if (JSON_HEDLEY_UNLIKELY(not j.is_array()))
153  {
154  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
155  }
156  l.resize(j.size());
157  std::copy(j.begin(), j.end(), std::begin(l));
158 }
159 
160 template <typename BasicJsonType, typename T, std::size_t N>
161 auto from_json(const BasicJsonType& j, T (&arr)[N])
162 -> decltype(j.template get<T>(), void())
163 {
164  for (std::size_t i = 0; i < N; ++i)
165  {
166  arr[i] = j.at(i).template get<T>();
167  }
168 }
169 
170 template<typename BasicJsonType>
171 void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
172 {
173  arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
174 }
175 
176 template <typename BasicJsonType, typename T, std::size_t N>
177 auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
178  priority_tag<2> /*unused*/)
179 -> decltype(j.template get<T>(), void())
180 {
181  for (std::size_t i = 0; i < N; ++i)
182  {
183  arr[i] = j.at(i).template get<T>();
184  }
185 }
186 
187 template<typename BasicJsonType, typename ConstructibleArrayType>
188 auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
189 -> decltype(
190  arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
191  j.template get<typename ConstructibleArrayType::value_type>(),
192  void())
193 {
194  using std::end;
195 
196  ConstructibleArrayType ret;
197  ret.reserve(j.size());
198  std::transform(j.begin(), j.end(),
199  std::inserter(ret, end(ret)), [](const BasicJsonType & i)
200  {
201  // get<BasicJsonType>() returns *this, this won't call a from_json
202  // method when value_type is BasicJsonType
203  return i.template get<typename ConstructibleArrayType::value_type>();
204  });
205  arr = std::move(ret);
206 }
207 
208 template <typename BasicJsonType, typename ConstructibleArrayType>
209 void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
210  priority_tag<0> /*unused*/)
211 {
212  using std::end;
213 
214  ConstructibleArrayType ret;
215  std::transform(
216  j.begin(), j.end(), std::inserter(ret, end(ret)),
217  [](const BasicJsonType & i)
218  {
219  // get<BasicJsonType>() returns *this, this won't call a from_json
220  // method when value_type is BasicJsonType
221  return i.template get<typename ConstructibleArrayType::value_type>();
222  });
223  arr = std::move(ret);
224 }
225 
226 template <typename BasicJsonType, typename ConstructibleArrayType,
227  enable_if_t <
228  is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value and
229  not is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value and
230  not is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value and
231  not is_basic_json<ConstructibleArrayType>::value,
232  int > = 0 >
233 
234 auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
235 -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
236 j.template get<typename ConstructibleArrayType::value_type>(),
237 void())
238 {
239  if (JSON_HEDLEY_UNLIKELY(not j.is_array()))
240  {
241  JSON_THROW(type_error::create(302, "type must be array, but is " +
242  std::string(j.type_name())));
243  }
244 
245  from_json_array_impl(j, arr, priority_tag<3> {});
246 }
247 
248 template<typename BasicJsonType, typename ConstructibleObjectType,
249  enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
250 void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
251 {
252  if (JSON_HEDLEY_UNLIKELY(not j.is_object()))
253  {
254  JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name())));
255  }
256 
257  ConstructibleObjectType ret;
258  auto inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
259  using value_type = typename ConstructibleObjectType::value_type;
260  std::transform(
261  inner_object->begin(), inner_object->end(),
262  std::inserter(ret, ret.begin()),
263  [](typename BasicJsonType::object_t::value_type const & p)
264  {
265  return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
266  });
267  obj = std::move(ret);
268 }
269 
270 // overload for arithmetic types, not chosen for basic_json template arguments
271 // (BooleanType, etc..); note: Is it really necessary to provide explicit
272 // overloads for boolean_t etc. in case of a custom BooleanType which is not
273 // an arithmetic type?
274 template<typename BasicJsonType, typename ArithmeticType,
275  enable_if_t <
276  std::is_arithmetic<ArithmeticType>::value and
277  not std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value and
278  not std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value and
279  not std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value and
280  not std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
281  int> = 0>
282 void from_json(const BasicJsonType& j, ArithmeticType& val)
283 {
284  switch (static_cast<value_t>(j))
285  {
287  {
288  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
289  break;
290  }
292  {
293  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
294  break;
295  }
297  {
298  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
299  break;
300  }
301  case value_t::boolean:
302  {
303  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
304  break;
305  }
306 
307  default:
308  JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name())));
309  }
310 }
311 
312 template<typename BasicJsonType, typename A1, typename A2>
313 void from_json(const BasicJsonType& j, std::pair<A1, A2>& p)
314 {
315  p = {j.at(0).template get<A1>(), j.at(1).template get<A2>()};
316 }
317 
318 template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
319 void from_json_tuple_impl(const BasicJsonType& j, Tuple& t, index_sequence<Idx...> /*unused*/)
320 {
321  t = std::make_tuple(j.at(Idx).template get<typename std::tuple_element<Idx, Tuple>::type>()...);
322 }
323 
324 template<typename BasicJsonType, typename... Args>
325 void from_json(const BasicJsonType& j, std::tuple<Args...>& t)
326 {
327  from_json_tuple_impl(j, t, index_sequence_for<Args...> {});
328 }
329 
330 template <typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
331  typename = enable_if_t<not std::is_constructible<
332  typename BasicJsonType::string_t, Key>::value>>
333 void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
334 {
335  if (JSON_HEDLEY_UNLIKELY(not j.is_array()))
336  {
337  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
338  }
339  m.clear();
340  for (const auto& p : j)
341  {
342  if (JSON_HEDLEY_UNLIKELY(not p.is_array()))
343  {
344  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name())));
345  }
346  m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
347  }
348 }
349 
350 template <typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
351  typename = enable_if_t<not std::is_constructible<
352  typename BasicJsonType::string_t, Key>::value>>
353 void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
354 {
355  if (JSON_HEDLEY_UNLIKELY(not j.is_array()))
356  {
357  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
358  }
359  m.clear();
360  for (const auto& p : j)
361  {
362  if (JSON_HEDLEY_UNLIKELY(not p.is_array()))
363  {
364  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name())));
365  }
366  m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
367  }
368 }
369 
370 struct from_json_fn
371 {
372  template<typename BasicJsonType, typename T>
373  auto operator()(const BasicJsonType& j, T& val) const
374  noexcept(noexcept(from_json(j, val)))
375  -> decltype(from_json(j, val), void())
376  {
377  return from_json(j, val);
378  }
379 };
380 } // namespace detail
381 
385 namespace
386 {
387 constexpr const auto& from_json = detail::static_const<detail::from_json_fn>::value;
388 } // namespace
389 } // namespace nlohmann
nlohmann::detail::value_t
value_t
the JSON type enumeration
Definition: value_t.hpp:42
nlohmann
namespace for Niels Lohmann
Definition: adl_serializer.hpp:9
nlohmann::detail::value_t::number_float
@ number_float
number value (floating-point)
nlohmann::detail::value_t::number_integer
@ number_integer
number value (signed integer)
nlohmann::detail::value_t::number_unsigned
@ number_unsigned
number value (unsigned integer)
nlohmann::detail::value_t::boolean
@ boolean
boolean value