from_json.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #pragma once
  2. #include <algorithm> // transform
  3. #include <array> // array
  4. #include <forward_list> // forward_list
  5. #include <iterator> // inserter, front_inserter, end
  6. #include <map> // map
  7. #include <string> // string
  8. #include <tuple> // tuple, make_tuple
  9. #include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
  10. #include <unordered_map> // unordered_map
  11. #include <utility> // pair, declval
  12. #include <valarray> // valarray
  13. #include <nlohmann/detail/boolean_operators.hpp>
  14. #include <nlohmann/detail/exceptions.hpp>
  15. #include <nlohmann/detail/macro_scope.hpp>
  16. #include <nlohmann/detail/meta/cpp_future.hpp>
  17. #include <nlohmann/detail/meta/type_traits.hpp>
  18. #include <nlohmann/detail/value_t.hpp>
  19. namespace nlohmann
  20. {
  21. namespace detail
  22. {
  23. template<typename BasicJsonType>
  24. void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
  25. {
  26. if (JSON_HEDLEY_UNLIKELY(not j.is_null()))
  27. {
  28. JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name())));
  29. }
  30. n = nullptr;
  31. }
  32. // overloads for basic_json template parameters
  33. template<typename BasicJsonType, typename ArithmeticType,
  34. enable_if_t<std::is_arithmetic<ArithmeticType>::value and
  35. not std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  36. int> = 0>
  37. void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
  38. {
  39. switch (static_cast<value_t>(j))
  40. {
  41. case value_t::number_unsigned:
  42. {
  43. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  44. break;
  45. }
  46. case value_t::number_integer:
  47. {
  48. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  49. break;
  50. }
  51. case value_t::number_float:
  52. {
  53. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  54. break;
  55. }
  56. default:
  57. JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name())));
  58. }
  59. }
  60. template<typename BasicJsonType>
  61. void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
  62. {
  63. if (JSON_HEDLEY_UNLIKELY(not j.is_boolean()))
  64. {
  65. JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name())));
  66. }
  67. b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
  68. }
  69. template<typename BasicJsonType>
  70. void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
  71. {
  72. if (JSON_HEDLEY_UNLIKELY(not j.is_string()))
  73. {
  74. JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name())));
  75. }
  76. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  77. }
  78. template <
  79. typename BasicJsonType, typename ConstructibleStringType,
  80. enable_if_t <
  81. is_constructible_string_type<BasicJsonType, ConstructibleStringType>::value and
  82. not std::is_same<typename BasicJsonType::string_t,
  83. ConstructibleStringType>::value,
  84. int > = 0 >
  85. void from_json(const BasicJsonType& j, ConstructibleStringType& s)
  86. {
  87. if (JSON_HEDLEY_UNLIKELY(not j.is_string()))
  88. {
  89. JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name())));
  90. }
  91. s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
  92. }
  93. template<typename BasicJsonType>
  94. void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
  95. {
  96. get_arithmetic_value(j, val);
  97. }
  98. template<typename BasicJsonType>
  99. void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
  100. {
  101. get_arithmetic_value(j, val);
  102. }
  103. template<typename BasicJsonType>
  104. void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
  105. {
  106. get_arithmetic_value(j, val);
  107. }
  108. template<typename BasicJsonType, typename EnumType,
  109. enable_if_t<std::is_enum<EnumType>::value, int> = 0>
  110. void from_json(const BasicJsonType& j, EnumType& e)
  111. {
  112. typename std::underlying_type<EnumType>::type val;
  113. get_arithmetic_value(j, val);
  114. e = static_cast<EnumType>(val);
  115. }
  116. // forward_list doesn't have an insert method
  117. template<typename BasicJsonType, typename T, typename Allocator,
  118. enable_if_t<std::is_convertible<BasicJsonType, T>::value, int> = 0>
  119. void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
  120. {
  121. if (JSON_HEDLEY_UNLIKELY(not j.is_array()))
  122. {
  123. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
  124. }
  125. l.clear();
  126. std::transform(j.rbegin(), j.rend(),
  127. std::front_inserter(l), [](const BasicJsonType & i)
  128. {
  129. return i.template get<T>();
  130. });
  131. }
  132. // valarray doesn't have an insert method
  133. template<typename BasicJsonType, typename T,
  134. enable_if_t<std::is_convertible<BasicJsonType, T>::value, int> = 0>
  135. void from_json(const BasicJsonType& j, std::valarray<T>& l)
  136. {
  137. if (JSON_HEDLEY_UNLIKELY(not j.is_array()))
  138. {
  139. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
  140. }
  141. l.resize(j.size());
  142. std::copy(j.begin(), j.end(), std::begin(l));
  143. }
  144. template <typename BasicJsonType, typename T, std::size_t N>
  145. auto from_json(const BasicJsonType& j, T (&arr)[N])
  146. -> decltype(j.template get<T>(), void())
  147. {
  148. for (std::size_t i = 0; i < N; ++i)
  149. {
  150. arr[i] = j.at(i).template get<T>();
  151. }
  152. }
  153. template<typename BasicJsonType>
  154. void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
  155. {
  156. arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
  157. }
  158. template <typename BasicJsonType, typename T, std::size_t N>
  159. auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
  160. priority_tag<2> /*unused*/)
  161. -> decltype(j.template get<T>(), void())
  162. {
  163. for (std::size_t i = 0; i < N; ++i)
  164. {
  165. arr[i] = j.at(i).template get<T>();
  166. }
  167. }
  168. template<typename BasicJsonType, typename ConstructibleArrayType>
  169. auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
  170. -> decltype(
  171. arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
  172. j.template get<typename ConstructibleArrayType::value_type>(),
  173. void())
  174. {
  175. using std::end;
  176. ConstructibleArrayType ret;
  177. ret.reserve(j.size());
  178. std::transform(j.begin(), j.end(),
  179. std::inserter(ret, end(ret)), [](const BasicJsonType & i)
  180. {
  181. // get<BasicJsonType>() returns *this, this won't call a from_json
  182. // method when value_type is BasicJsonType
  183. return i.template get<typename ConstructibleArrayType::value_type>();
  184. });
  185. arr = std::move(ret);
  186. }
  187. template <typename BasicJsonType, typename ConstructibleArrayType>
  188. void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
  189. priority_tag<0> /*unused*/)
  190. {
  191. using std::end;
  192. ConstructibleArrayType ret;
  193. std::transform(
  194. j.begin(), j.end(), std::inserter(ret, end(ret)),
  195. [](const BasicJsonType & i)
  196. {
  197. // get<BasicJsonType>() returns *this, this won't call a from_json
  198. // method when value_type is BasicJsonType
  199. return i.template get<typename ConstructibleArrayType::value_type>();
  200. });
  201. arr = std::move(ret);
  202. }
  203. template <typename BasicJsonType, typename ConstructibleArrayType,
  204. enable_if_t <
  205. is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value and
  206. not is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value and
  207. not is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value and
  208. not std::is_same<ConstructibleArrayType, typename BasicJsonType::binary_t>::value and
  209. not is_basic_json<ConstructibleArrayType>::value,
  210. int > = 0 >
  211. auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
  212. -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
  213. j.template get<typename ConstructibleArrayType::value_type>(),
  214. void())
  215. {
  216. if (JSON_HEDLEY_UNLIKELY(not j.is_array()))
  217. {
  218. JSON_THROW(type_error::create(302, "type must be array, but is " +
  219. std::string(j.type_name())));
  220. }
  221. from_json_array_impl(j, arr, priority_tag<3> {});
  222. }
  223. template <typename BasicJsonType>
  224. void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin)
  225. {
  226. if (JSON_HEDLEY_UNLIKELY(not j.is_binary()))
  227. {
  228. JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(j.type_name())));
  229. }
  230. bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
  231. }
  232. template<typename BasicJsonType, typename ConstructibleObjectType,
  233. enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
  234. void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
  235. {
  236. if (JSON_HEDLEY_UNLIKELY(not j.is_object()))
  237. {
  238. JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name())));
  239. }
  240. ConstructibleObjectType ret;
  241. auto inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
  242. using value_type = typename ConstructibleObjectType::value_type;
  243. std::transform(
  244. inner_object->begin(), inner_object->end(),
  245. std::inserter(ret, ret.begin()),
  246. [](typename BasicJsonType::object_t::value_type const & p)
  247. {
  248. return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
  249. });
  250. obj = std::move(ret);
  251. }
  252. // overload for arithmetic types, not chosen for basic_json template arguments
  253. // (BooleanType, etc..); note: Is it really necessary to provide explicit
  254. // overloads for boolean_t etc. in case of a custom BooleanType which is not
  255. // an arithmetic type?
  256. template<typename BasicJsonType, typename ArithmeticType,
  257. enable_if_t <
  258. std::is_arithmetic<ArithmeticType>::value and
  259. not std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value and
  260. not std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value and
  261. not std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value and
  262. not std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
  263. int> = 0>
  264. void from_json(const BasicJsonType& j, ArithmeticType& val)
  265. {
  266. switch (static_cast<value_t>(j))
  267. {
  268. case value_t::number_unsigned:
  269. {
  270. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
  271. break;
  272. }
  273. case value_t::number_integer:
  274. {
  275. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
  276. break;
  277. }
  278. case value_t::number_float:
  279. {
  280. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
  281. break;
  282. }
  283. case value_t::boolean:
  284. {
  285. val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
  286. break;
  287. }
  288. default:
  289. JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name())));
  290. }
  291. }
  292. template<typename BasicJsonType, typename A1, typename A2>
  293. void from_json(const BasicJsonType& j, std::pair<A1, A2>& p)
  294. {
  295. p = {j.at(0).template get<A1>(), j.at(1).template get<A2>()};
  296. }
  297. template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
  298. void from_json_tuple_impl(const BasicJsonType& j, Tuple& t, index_sequence<Idx...> /*unused*/)
  299. {
  300. t = std::make_tuple(j.at(Idx).template get<typename std::tuple_element<Idx, Tuple>::type>()...);
  301. }
  302. template<typename BasicJsonType, typename... Args>
  303. void from_json(const BasicJsonType& j, std::tuple<Args...>& t)
  304. {
  305. from_json_tuple_impl(j, t, index_sequence_for<Args...> {});
  306. }
  307. template <typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
  308. typename = enable_if_t<not std::is_constructible<
  309. typename BasicJsonType::string_t, Key>::value>>
  310. void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
  311. {
  312. if (JSON_HEDLEY_UNLIKELY(not j.is_array()))
  313. {
  314. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
  315. }
  316. m.clear();
  317. for (const auto& p : j)
  318. {
  319. if (JSON_HEDLEY_UNLIKELY(not p.is_array()))
  320. {
  321. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name())));
  322. }
  323. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  324. }
  325. }
  326. template <typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
  327. typename = enable_if_t<not std::is_constructible<
  328. typename BasicJsonType::string_t, Key>::value>>
  329. void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
  330. {
  331. if (JSON_HEDLEY_UNLIKELY(not j.is_array()))
  332. {
  333. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
  334. }
  335. m.clear();
  336. for (const auto& p : j)
  337. {
  338. if (JSON_HEDLEY_UNLIKELY(not p.is_array()))
  339. {
  340. JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name())));
  341. }
  342. m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
  343. }
  344. }
  345. struct from_json_fn
  346. {
  347. template<typename BasicJsonType, typename T>
  348. auto operator()(const BasicJsonType& j, T& val) const
  349. noexcept(noexcept(from_json(j, val)))
  350. -> decltype(from_json(j, val), void())
  351. {
  352. return from_json(j, val);
  353. }
  354. };
  355. } // namespace detail
  356. /// namespace to hold default `from_json` function
  357. /// to see why this is required:
  358. /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
  359. namespace
  360. {
  361. constexpr const auto& from_json = detail::static_const<detail::from_json_fn>::value;
  362. } // namespace
  363. } // namespace nlohmann