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