Horizon
Loading...
Searching...
No Matches
from_json.hpp
1#pragma once
2
3#include <algorithm> // transform
4#include <array> // array
5#include <forward_list> // forward_list
6#include <iterator> // inserter, front_inserter, end
7#include <map> // map
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 <unordered_map> // unordered_map
12#include <utility> // pair, declval
13#include <valarray> // valarray
14
15#include <nlohmann/detail/exceptions.hpp>
16#include <nlohmann/detail/macro_scope.hpp>
17#include <nlohmann/detail/meta/cpp_future.hpp>
18#include <nlohmann/detail/meta/identity_tag.hpp>
19#include <nlohmann/detail/meta/type_traits.hpp>
20#include <nlohmann/detail/value_t.hpp>
21
22namespace nlohmann
23{
24namespace detail
25{
26template<typename BasicJsonType>
27void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
28{
29 if (JSON_HEDLEY_UNLIKELY(!j.is_null()))
30 {
31 JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()), j));
32 }
33 n = nullptr;
34}
35
36// overloads for basic_json template parameters
37template < typename BasicJsonType, typename ArithmeticType,
38 enable_if_t < std::is_arithmetic<ArithmeticType>::value&&
39 !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
40 int > = 0 >
41void 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 case value_t::null:
62 case value_t::object:
63 case value_t::array:
64 case value_t::string:
66 case value_t::binary:
68 default:
69 JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), j));
70 }
71}
72
73template<typename BasicJsonType>
74void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
75{
76 if (JSON_HEDLEY_UNLIKELY(!j.is_boolean()))
77 {
78 JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()), j));
79 }
80 b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
81}
82
83template<typename BasicJsonType>
84void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
85{
86 if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
87 {
88 JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j));
89 }
90 s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
91}
92
93template <
94 typename BasicJsonType, typename ConstructibleStringType,
95 enable_if_t <
97 !std::is_same<typename BasicJsonType::string_t,
98 ConstructibleStringType>::value,
99 int > = 0 >
100void from_json(const BasicJsonType& j, ConstructibleStringType& s)
101{
102 if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
103 {
104 JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j));
105 }
106
107 s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
108}
109
110template<typename BasicJsonType>
111void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
112{
113 get_arithmetic_value(j, val);
114}
115
116template<typename BasicJsonType>
117void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
118{
119 get_arithmetic_value(j, val);
120}
121
122template<typename BasicJsonType>
123void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
124{
125 get_arithmetic_value(j, val);
126}
127
128template<typename BasicJsonType, typename EnumType,
129 enable_if_t<std::is_enum<EnumType>::value, int> = 0>
130void from_json(const BasicJsonType& j, EnumType& e)
131{
132 typename std::underlying_type<EnumType>::type val;
133 get_arithmetic_value(j, val);
134 e = static_cast<EnumType>(val);
135}
136
137// forward_list doesn't have an insert method
138template<typename BasicJsonType, typename T, typename Allocator,
139 enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
140void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
141{
142 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
143 {
144 JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
145 }
146 l.clear();
147 std::transform(j.rbegin(), j.rend(),
148 std::front_inserter(l), [](const BasicJsonType & i)
149 {
150 return i.template get<T>();
151 });
152}
153
154// valarray doesn't have an insert method
155template<typename BasicJsonType, typename T,
156 enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
157void from_json(const BasicJsonType& j, std::valarray<T>& l)
158{
159 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
160 {
161 JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
162 }
163 l.resize(j.size());
164 std::transform(j.begin(), j.end(), std::begin(l),
165 [](const BasicJsonType & elem)
166 {
167 return elem.template get<T>();
168 });
169}
170
171template<typename BasicJsonType, typename T, std::size_t N>
172auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
173-> decltype(j.template get<T>(), void())
174{
175 for (std::size_t i = 0; i < N; ++i)
176 {
177 arr[i] = j.at(i).template get<T>();
178 }
179}
180
181template<typename BasicJsonType>
182void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
183{
184 arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
185}
186
187template<typename BasicJsonType, typename T, std::size_t N>
188auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
189 priority_tag<2> /*unused*/)
190-> decltype(j.template get<T>(), void())
191{
192 for (std::size_t i = 0; i < N; ++i)
193 {
194 arr[i] = j.at(i).template get<T>();
195 }
196}
197
198template<typename BasicJsonType, typename ConstructibleArrayType,
199 enable_if_t<
200 std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
201 int> = 0>
202auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
203-> decltype(
204 arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
205 j.template get<typename ConstructibleArrayType::value_type>(),
206 void())
207{
208 using std::end;
209
210 ConstructibleArrayType ret;
211 ret.reserve(j.size());
212 std::transform(j.begin(), j.end(),
213 std::inserter(ret, end(ret)), [](const BasicJsonType & i)
214 {
215 // get<BasicJsonType>() returns *this, this won't call a from_json
216 // method when value_type is BasicJsonType
217 return i.template get<typename ConstructibleArrayType::value_type>();
218 });
219 arr = std::move(ret);
220}
221
222template<typename BasicJsonType, typename ConstructibleArrayType,
223 enable_if_t<
224 std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
225 int> = 0>
226void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
227 priority_tag<0> /*unused*/)
228{
229 using std::end;
230
231 ConstructibleArrayType ret;
232 std::transform(
233 j.begin(), j.end(), std::inserter(ret, end(ret)),
234 [](const BasicJsonType & i)
235 {
236 // get<BasicJsonType>() returns *this, this won't call a from_json
237 // method when value_type is BasicJsonType
238 return i.template get<typename ConstructibleArrayType::value_type>();
239 });
240 arr = std::move(ret);
241}
242
243template < typename BasicJsonType, typename ConstructibleArrayType,
244 enable_if_t <
248 !std::is_same<ConstructibleArrayType, typename BasicJsonType::binary_t>::value&&
250 int > = 0 >
251auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
252-> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
253j.template get<typename ConstructibleArrayType::value_type>(),
254void())
255{
256 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
257 {
258 JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
259 }
260
261 from_json_array_impl(j, arr, priority_tag<3> {});
262}
263
264template < typename BasicJsonType, typename T, std::size_t... Idx >
265std::array<T, sizeof...(Idx)> from_json_inplace_array_impl(BasicJsonType&& j,
266 identity_tag<std::array<T, sizeof...(Idx)>> /*unused*/, index_sequence<Idx...> /*unused*/)
267{
268 return { { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... } };
269}
270
271template < typename BasicJsonType, typename T, std::size_t N >
272auto from_json(BasicJsonType&& j, identity_tag<std::array<T, N>> tag)
273-> decltype(from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
274{
275 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
276 {
277 JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
278 }
279
280 return from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
281}
282
283template<typename BasicJsonType>
284void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin)
285{
286 if (JSON_HEDLEY_UNLIKELY(!j.is_binary()))
287 {
288 JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(j.type_name()), j));
289 }
290
291 bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
292}
293
294template<typename BasicJsonType, typename ConstructibleObjectType,
295 enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
296void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
297{
298 if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
299 {
300 JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()), j));
301 }
302
303 ConstructibleObjectType ret;
304 const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
305 using value_type = typename ConstructibleObjectType::value_type;
306 std::transform(
307 inner_object->begin(), inner_object->end(),
308 std::inserter(ret, ret.begin()),
309 [](typename BasicJsonType::object_t::value_type const & p)
310 {
311 return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
312 });
313 obj = std::move(ret);
314}
315
316// overload for arithmetic types, not chosen for basic_json template arguments
317// (BooleanType, etc..); note: Is it really necessary to provide explicit
318// overloads for boolean_t etc. in case of a custom BooleanType which is not
319// an arithmetic type?
320template < typename BasicJsonType, typename ArithmeticType,
321 enable_if_t <
322 std::is_arithmetic<ArithmeticType>::value&&
323 !std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value&&
324 !std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value&&
325 !std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value&&
326 !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
327 int > = 0 >
328void from_json(const BasicJsonType& j, ArithmeticType& val)
329{
330 switch (static_cast<value_t>(j))
331 {
333 {
334 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
335 break;
336 }
338 {
339 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
340 break;
341 }
343 {
344 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
345 break;
346 }
347 case value_t::boolean:
348 {
349 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
350 break;
351 }
352
353 case value_t::null:
354 case value_t::object:
355 case value_t::array:
356 case value_t::string:
357 case value_t::binary:
359 default:
360 JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), j));
361 }
362}
363
364template<typename BasicJsonType, typename... Args, std::size_t... Idx>
365std::tuple<Args...> from_json_tuple_impl_base(BasicJsonType&& j, index_sequence<Idx...> /*unused*/)
366{
367 return std::make_tuple(std::forward<BasicJsonType>(j).at(Idx).template get<Args>()...);
368}
369
370template < typename BasicJsonType, class A1, class A2 >
371std::pair<A1, A2> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::pair<A1, A2>> /*unused*/, priority_tag<0> /*unused*/)
372{
373 return {std::forward<BasicJsonType>(j).at(0).template get<A1>(),
374 std::forward<BasicJsonType>(j).at(1).template get<A2>()};
375}
376
377template<typename BasicJsonType, typename A1, typename A2>
378void from_json_tuple_impl(BasicJsonType&& j, std::pair<A1, A2>& p, priority_tag<1> /*unused*/)
379{
380 p = from_json_tuple_impl(std::forward<BasicJsonType>(j), identity_tag<std::pair<A1, A2>> {}, priority_tag<0> {});
381}
382
383template<typename BasicJsonType, typename... Args>
384std::tuple<Args...> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::tuple<Args...>> /*unused*/, priority_tag<2> /*unused*/)
385{
386 return from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
387}
388
389template<typename BasicJsonType, typename... Args>
390void from_json_tuple_impl(BasicJsonType&& j, std::tuple<Args...>& t, priority_tag<3> /*unused*/)
391{
392 t = from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
393}
394
395template<typename BasicJsonType, typename TupleRelated>
396auto from_json(BasicJsonType&& j, TupleRelated&& t)
397-> decltype(from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {}))
398{
399 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
400 {
401 JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
402 }
403
404 return from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {});
405}
406
407template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
408 typename = enable_if_t < !std::is_constructible <
409 typename BasicJsonType::string_t, Key >::value >>
410void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
411{
412 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
413 {
414 JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
415 }
416 m.clear();
417 for (const auto& p : j)
418 {
419 if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
420 {
421 JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), j));
422 }
423 m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
424 }
425}
426
427template < typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
428 typename = enable_if_t < !std::is_constructible <
429 typename BasicJsonType::string_t, Key >::value >>
430void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
431{
432 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
433 {
434 JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
435 }
436 m.clear();
437 for (const auto& p : j)
438 {
439 if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
440 {
441 JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), j));
442 }
443 m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
444 }
445}
446
448{
449 template<typename BasicJsonType, typename T>
450 auto operator()(const BasicJsonType& j, T&& val) const
451 noexcept(noexcept(from_json(j, std::forward<T>(val))))
452 -> decltype(from_json(j, std::forward<T>(val)))
453 {
454 return from_json(j, std::forward<T>(val));
455 }
456};
457} // namespace detail
458
462namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
463{
464constexpr const auto& from_json = detail::static_const<detail::from_json_fn>::value; // NOLINT(misc-definitions-in-headers)
465} // namespace
466} // namespace nlohmann
value_t
the JSON type enumeration
Definition value_t.hpp:41
@ number_integer
number value (signed integer)
@ discarded
discarded by the parser callback function
@ binary
binary array (ordered collection of bytes)
@ object
object (unordered set of name/value pairs)
@ number_float
number value (floating-point)
@ number_unsigned
number value (unsigned integer)
@ array
array (ordered collection of values)
@ value
the parser finished reading a JSON value
namespace for Niels Lohmann
Definition adl_serializer.hpp:12
Definition from_json.hpp:448
Definition identity_tag.hpp:8
Definition cpp_future.hpp:57
Definition type_traits.hpp:42
Definition type_traits.hpp:408
Definition cpp_future.hpp:140
Definition cpp_future.hpp:146