from_json.hpp 21 KB

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