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
22 namespace nlohmann
23 {
24 namespace detail
25 {
26 template<typename BasicJsonType>
from_json(const BasicJsonType & j,typename std::nullptr_t & n)27 void 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
37 template < 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 >
get_arithmetic_value(const BasicJsonType & j,ArithmeticType & val)41 void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
42 {
43 switch (static_cast<value_t>(j))
44 {
45 case value_t::number_unsigned:
46 {
47 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
48 break;
49 }
50 case value_t::number_integer:
51 {
52 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
53 break;
54 }
55 case value_t::number_float:
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:
65 case value_t::boolean:
66 case value_t::binary:
67 case value_t::discarded:
68 default:
69 JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), j));
70 }
71 }
72
73 template<typename BasicJsonType>
from_json(const BasicJsonType & j,typename BasicJsonType::boolean_t & b)74 void 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
83 template<typename BasicJsonType>
from_json(const BasicJsonType & j,typename BasicJsonType::string_t & s)84 void 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
93 template <
94 typename BasicJsonType, typename ConstructibleStringType,
95 enable_if_t <
96 is_constructible_string_type<BasicJsonType, ConstructibleStringType>::value&&
97 !std::is_same<typename BasicJsonType::string_t,
98 ConstructibleStringType>::value,
99 int > = 0 >
from_json(const BasicJsonType & j,ConstructibleStringType & s)100 void 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
110 template<typename BasicJsonType>
from_json(const BasicJsonType & j,typename BasicJsonType::number_float_t & val)111 void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
112 {
113 get_arithmetic_value(j, val);
114 }
115
116 template<typename BasicJsonType>
from_json(const BasicJsonType & j,typename BasicJsonType::number_unsigned_t & val)117 void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
118 {
119 get_arithmetic_value(j, val);
120 }
121
122 template<typename BasicJsonType>
from_json(const BasicJsonType & j,typename BasicJsonType::number_integer_t & val)123 void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
124 {
125 get_arithmetic_value(j, val);
126 }
127
128 template<typename BasicJsonType, typename EnumType,
129 enable_if_t<std::is_enum<EnumType>::value, int> = 0>
from_json(const BasicJsonType & j,EnumType & e)130 void 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
138 template<typename BasicJsonType, typename T, typename Allocator,
139 enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
from_json(const BasicJsonType & j,std::forward_list<T,Allocator> & l)140 void 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
155 template<typename BasicJsonType, typename T,
156 enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
from_json(const BasicJsonType & j,std::valarray<T> & l)157 void 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
171 template<typename BasicJsonType, typename T, std::size_t N>
from_json(const BasicJsonType & j,T (& arr)[N])172 auto 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
181 template<typename BasicJsonType>
from_json_array_impl(const BasicJsonType & j,typename BasicJsonType::array_t & arr,priority_tag<3>)182 void 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
187 template<typename BasicJsonType, typename T, std::size_t N>
from_json_array_impl(const BasicJsonType & j,std::array<T,N> & arr,priority_tag<2>)188 auto 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
198 template<typename BasicJsonType, typename ConstructibleArrayType,
199 enable_if_t<
200 std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
201 int> = 0>
from_json_array_impl(const BasicJsonType & j,ConstructibleArrayType & arr,priority_tag<1>)202 auto 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
222 template<typename BasicJsonType, typename ConstructibleArrayType,
223 enable_if_t<
224 std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
225 int> = 0>
from_json_array_impl(const BasicJsonType & j,ConstructibleArrayType & arr,priority_tag<0>)226 void 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
243 template < typename BasicJsonType, typename ConstructibleArrayType,
244 enable_if_t <
245 is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value&&
246 !is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value&&
247 !is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
248 !std::is_same<ConstructibleArrayType, typename BasicJsonType::binary_t>::value&&
249 !is_basic_json<ConstructibleArrayType>::value,
250 int > = 0 >
from_json(const BasicJsonType & j,ConstructibleArrayType & arr)251 auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
252 -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
253 j.template get<typename ConstructibleArrayType::value_type>(),
254 void())
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
264 template < typename BasicJsonType, typename T, std::size_t... Idx >
from_json_inplace_array_impl(BasicJsonType && j,identity_tag<std::array<T,sizeof...(Idx)>>,index_sequence<Idx...>)265 std::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
271 template < typename BasicJsonType, typename T, std::size_t N >
from_json(BasicJsonType && j,identity_tag<std::array<T,N>> tag)272 auto 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
283 template<typename BasicJsonType>
from_json(const BasicJsonType & j,typename BasicJsonType::binary_t & bin)284 void 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
294 template<typename BasicJsonType, typename ConstructibleObjectType,
295 enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
from_json(const BasicJsonType & j,ConstructibleObjectType & obj)296 void 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?
320 template < 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 >
from_json(const BasicJsonType & j,ArithmeticType & val)328 void from_json(const BasicJsonType& j, ArithmeticType& val)
329 {
330 switch (static_cast<value_t>(j))
331 {
332 case value_t::number_unsigned:
333 {
334 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
335 break;
336 }
337 case value_t::number_integer:
338 {
339 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
340 break;
341 }
342 case value_t::number_float:
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:
358 case value_t::discarded:
359 default:
360 JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), j));
361 }
362 }
363
364 template<typename BasicJsonType, typename... Args, std::size_t... Idx>
from_json_tuple_impl_base(BasicJsonType && j,index_sequence<Idx...>)365 std::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
370 template < typename BasicJsonType, class A1, class A2 >
from_json_tuple_impl(BasicJsonType && j,identity_tag<std::pair<A1,A2>>,priority_tag<0>)371 std::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
377 template<typename BasicJsonType, typename A1, typename A2>
from_json_tuple_impl(BasicJsonType && j,std::pair<A1,A2> & p,priority_tag<1>)378 void 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
383 template<typename BasicJsonType, typename... Args>
from_json_tuple_impl(BasicJsonType && j,identity_tag<std::tuple<Args...>>,priority_tag<2>)384 std::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
389 template<typename BasicJsonType, typename... Args>
from_json_tuple_impl(BasicJsonType && j,std::tuple<Args...> & t,priority_tag<3>)390 void 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
395 template<typename BasicJsonType, typename TupleRelated>
from_json(BasicJsonType && j,TupleRelated && t)396 auto 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
407 template < 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 >>
from_json(const BasicJsonType & j,std::map<Key,Value,Compare,Allocator> & m)410 void 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
427 template < 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 >>
from_json(const BasicJsonType & j,std::unordered_map<Key,Value,Hash,KeyEqual,Allocator> & m)430 void 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
447 struct from_json_fn
448 {
449 template<typename BasicJsonType, typename T>
operator ()nlohmann::detail::from_json_fn450 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
459 /// namespace to hold default `from_json` function
460 /// to see why this is required:
461 /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
462 namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
463 {
464 constexpr const auto& from_json = detail::static_const<detail::from_json_fn>::value; // NOLINT(misc-definitions-in-headers)
465 } // namespace
466 } // namespace nlohmann
467