from_json.hpp 18 KB

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