3 #include <algorithm> // transform
4 #include <array> // array
5 #include <forward_list> // forward_list
6 #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 <unordered_map> // unordered_map
12 #include <utility> // pair, declval
13 #include <valarray> // valarray
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>
22 #if JSON_HAS_EXPERIMENTAL_FILESYSTEM
23 #include <experimental/filesystem>
24 namespace nlohmann::detail
26 namespace std_fs = std::experimental::filesystem;
27 } // namespace nlohmann::detail
28 #elif JSON_HAS_FILESYSTEM
30 namespace nlohmann::detail
32 namespace std_fs = std::filesystem;
33 } // namespace nlohmann::detail
40 template<typename BasicJsonType>
41 void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
43 if (JSON_HEDLEY_UNLIKELY(!j.is_null()))
45 JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()), j));
50 // overloads for basic_json template parameters
51 template < typename BasicJsonType, typename ArithmeticType,
52 enable_if_t < std::is_arithmetic<ArithmeticType>::value&&
53 !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
55 void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
57 switch (static_cast<value_t>(j))
59 case value_t::number_unsigned:
61 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
64 case value_t::number_integer:
66 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
69 case value_t::number_float:
71 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
79 case value_t::boolean:
81 case value_t::discarded:
83 JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), j));
87 template<typename BasicJsonType>
88 void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
90 if (JSON_HEDLEY_UNLIKELY(!j.is_boolean()))
92 JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()), j));
94 b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
97 template<typename BasicJsonType>
98 void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
100 if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
102 JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j));
104 s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
108 typename BasicJsonType, typename ConstructibleStringType,
110 is_constructible_string_type<BasicJsonType, ConstructibleStringType>::value&&
111 !std::is_same<typename BasicJsonType::string_t,
112 ConstructibleStringType>::value,
114 void from_json(const BasicJsonType& j, ConstructibleStringType& s)
116 if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
118 JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j));
121 s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
124 template<typename BasicJsonType>
125 void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
127 get_arithmetic_value(j, val);
130 template<typename BasicJsonType>
131 void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
133 get_arithmetic_value(j, val);
136 template<typename BasicJsonType>
137 void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
139 get_arithmetic_value(j, val);
142 template<typename BasicJsonType, typename EnumType,
143 enable_if_t<std::is_enum<EnumType>::value, int> = 0>
144 void from_json(const BasicJsonType& j, EnumType& e)
146 typename std::underlying_type<EnumType>::type val;
147 get_arithmetic_value(j, val);
148 e = static_cast<EnumType>(val);
151 // forward_list doesn't have an insert method
152 template<typename BasicJsonType, typename T, typename Allocator,
153 enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
154 void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
156 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
158 JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
161 std::transform(j.rbegin(), j.rend(),
162 std::front_inserter(l), [](const BasicJsonType & i)
164 return i.template get<T>();
168 // valarray doesn't have an insert method
169 template<typename BasicJsonType, typename T,
170 enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
171 void from_json(const BasicJsonType& j, std::valarray<T>& l)
173 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
175 JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
178 std::transform(j.begin(), j.end(), std::begin(l),
179 [](const BasicJsonType & elem)
181 return elem.template get<T>();
185 template<typename BasicJsonType, typename T, std::size_t N>
186 auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
187 -> decltype(j.template get<T>(), void())
189 for (std::size_t i = 0; i < N; ++i)
191 arr[i] = j.at(i).template get<T>();
195 template<typename BasicJsonType>
196 void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
198 arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
201 template<typename BasicJsonType, typename T, std::size_t N>
202 auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
203 priority_tag<2> /*unused*/)
204 -> decltype(j.template get<T>(), void())
206 for (std::size_t i = 0; i < N; ++i)
208 arr[i] = j.at(i).template get<T>();
212 template<typename BasicJsonType, typename ConstructibleArrayType,
214 std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
216 auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
218 arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
219 j.template get<typename ConstructibleArrayType::value_type>(),
224 ConstructibleArrayType ret;
225 ret.reserve(j.size());
226 std::transform(j.begin(), j.end(),
227 std::inserter(ret, end(ret)), [](const BasicJsonType & i)
229 // get<BasicJsonType>() returns *this, this won't call a from_json
230 // method when value_type is BasicJsonType
231 return i.template get<typename ConstructibleArrayType::value_type>();
233 arr = std::move(ret);
236 template<typename BasicJsonType, typename ConstructibleArrayType,
238 std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
240 void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
241 priority_tag<0> /*unused*/)
245 ConstructibleArrayType ret;
247 j.begin(), j.end(), std::inserter(ret, end(ret)),
248 [](const BasicJsonType & i)
250 // get<BasicJsonType>() returns *this, this won't call a from_json
251 // method when value_type is BasicJsonType
252 return i.template get<typename ConstructibleArrayType::value_type>();
254 arr = std::move(ret);
257 template < typename BasicJsonType, typename ConstructibleArrayType,
259 is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value&&
260 !is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value&&
261 !is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
262 !std::is_same<ConstructibleArrayType, typename BasicJsonType::binary_t>::value&&
263 !is_basic_json<ConstructibleArrayType>::value,
265 auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
266 -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
267 j.template get<typename ConstructibleArrayType::value_type>(),
270 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
272 JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
275 from_json_array_impl(j, arr, priority_tag<3> {});
278 template < typename BasicJsonType, typename T, std::size_t... Idx >
279 std::array<T, sizeof...(Idx)> from_json_inplace_array_impl(BasicJsonType&& j,
280 identity_tag<std::array<T, sizeof...(Idx)>> /*unused*/, index_sequence<Idx...> /*unused*/)
282 return { { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... } };
285 template < typename BasicJsonType, typename T, std::size_t N >
286 auto from_json(BasicJsonType&& j, identity_tag<std::array<T, N>> tag)
287 -> decltype(from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
289 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
291 JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
294 return from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
297 template<typename BasicJsonType>
298 void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin)
300 if (JSON_HEDLEY_UNLIKELY(!j.is_binary()))
302 JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(j.type_name()), j));
305 bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
308 template<typename BasicJsonType, typename ConstructibleObjectType,
309 enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
310 void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
312 if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
314 JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()), j));
317 ConstructibleObjectType ret;
318 const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
319 using value_type = typename ConstructibleObjectType::value_type;
321 inner_object->begin(), inner_object->end(),
322 std::inserter(ret, ret.begin()),
323 [](typename BasicJsonType::object_t::value_type const & p)
325 return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
327 obj = std::move(ret);
330 // overload for arithmetic types, not chosen for basic_json template arguments
331 // (BooleanType, etc..); note: Is it really necessary to provide explicit
332 // overloads for boolean_t etc. in case of a custom BooleanType which is not
333 // an arithmetic type?
334 template < typename BasicJsonType, typename ArithmeticType,
336 std::is_arithmetic<ArithmeticType>::value&&
337 !std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value&&
338 !std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value&&
339 !std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value&&
340 !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
342 void from_json(const BasicJsonType& j, ArithmeticType& val)
344 switch (static_cast<value_t>(j))
346 case value_t::number_unsigned:
348 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
351 case value_t::number_integer:
353 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
356 case value_t::number_float:
358 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
361 case value_t::boolean:
363 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
368 case value_t::object:
370 case value_t::string:
371 case value_t::binary:
372 case value_t::discarded:
374 JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), j));
378 template<typename BasicJsonType, typename... Args, std::size_t... Idx>
379 std::tuple<Args...> from_json_tuple_impl_base(BasicJsonType&& j, index_sequence<Idx...> /*unused*/)
381 return std::make_tuple(std::forward<BasicJsonType>(j).at(Idx).template get<Args>()...);
384 template < typename BasicJsonType, class A1, class A2 >
385 std::pair<A1, A2> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::pair<A1, A2>> /*unused*/, priority_tag<0> /*unused*/)
387 return {std::forward<BasicJsonType>(j).at(0).template get<A1>(),
388 std::forward<BasicJsonType>(j).at(1).template get<A2>()};
391 template<typename BasicJsonType, typename A1, typename A2>
392 void from_json_tuple_impl(BasicJsonType&& j, std::pair<A1, A2>& p, priority_tag<1> /*unused*/)
394 p = from_json_tuple_impl(std::forward<BasicJsonType>(j), identity_tag<std::pair<A1, A2>> {}, priority_tag<0> {});
397 template<typename BasicJsonType, typename... Args>
398 std::tuple<Args...> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::tuple<Args...>> /*unused*/, priority_tag<2> /*unused*/)
400 return from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
403 template<typename BasicJsonType, typename... Args>
404 void from_json_tuple_impl(BasicJsonType&& j, std::tuple<Args...>& t, priority_tag<3> /*unused*/)
406 t = from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
409 template<typename BasicJsonType, typename TupleRelated>
410 auto from_json(BasicJsonType&& j, TupleRelated&& t)
411 -> decltype(from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {}))
413 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
415 JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
418 return from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {});
421 template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
422 typename = enable_if_t < !std::is_constructible <
423 typename BasicJsonType::string_t, Key >::value >>
424 void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
426 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
428 JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
431 for (const auto& p : j)
433 if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
435 JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), j));
437 m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
441 template < typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
442 typename = enable_if_t < !std::is_constructible <
443 typename BasicJsonType::string_t, Key >::value >>
444 void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
446 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
448 JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
451 for (const auto& p : j)
453 if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
455 JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), j));
457 m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
461 #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
462 template<typename BasicJsonType>
463 void from_json(const BasicJsonType& j, std_fs::path& p)
465 if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
467 JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j));
469 p = *j.template get_ptr<const typename BasicJsonType::string_t*>();
475 template<typename BasicJsonType, typename T>
476 auto operator()(const BasicJsonType& j, T&& val) const
477 noexcept(noexcept(from_json(j, std::forward<T>(val))))
478 -> decltype(from_json(j, std::forward<T>(val)))
480 return from_json(j, std::forward<T>(val));
483 } // namespace detail
485 /// namespace to hold default `from_json` function
486 /// to see why this is required:
487 /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
488 namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
490 constexpr const auto& from_json = detail::static_const<detail::from_json_fn>::value; // NOLINT(misc-definitions-in-headers)
492 } // namespace nlohmann