2022-07-20 12:38:07 +02:00
|
|
|
// __ _____ _____ _____
|
|
|
|
// __| | __| | | | JSON for Modern C++
|
2023-11-28 22:36:31 +01:00
|
|
|
// | | |__ | | | | | | version 3.11.3
|
2022-07-20 12:38:07 +02:00
|
|
|
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
|
|
|
//
|
2023-11-26 15:51:19 +01:00
|
|
|
// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
|
2022-07-20 12:38:07 +02:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2018-01-10 10:18:31 +01:00
|
|
|
#pragma once
|
2017-08-14 16:33:55 +02:00
|
|
|
|
2017-08-14 19:28:01 +02:00
|
|
|
#include <algorithm> // transform
|
|
|
|
#include <array> // array
|
|
|
|
#include <forward_list> // forward_list
|
|
|
|
#include <iterator> // inserter, front_inserter, end
|
2018-05-14 11:51:37 +02:00
|
|
|
#include <map> // map
|
2017-08-14 19:28:01 +02:00
|
|
|
#include <string> // string
|
|
|
|
#include <tuple> // tuple, make_tuple
|
|
|
|
#include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
|
2018-06-18 10:53:30 +02:00
|
|
|
#include <unordered_map> // unordered_map
|
2017-08-14 19:28:01 +02:00
|
|
|
#include <utility> // pair, declval
|
|
|
|
#include <valarray> // valarray
|
2017-08-14 16:33:55 +02:00
|
|
|
|
2018-01-29 11:21:11 +01:00
|
|
|
#include <nlohmann/detail/exceptions.hpp>
|
|
|
|
#include <nlohmann/detail/macro_scope.hpp>
|
2018-07-24 13:22:44 +02:00
|
|
|
#include <nlohmann/detail/meta/cpp_future.hpp>
|
2021-01-10 19:23:32 +01:00
|
|
|
#include <nlohmann/detail/meta/identity_tag.hpp>
|
2022-07-30 21:59:13 +02:00
|
|
|
#include <nlohmann/detail/meta/std_fs.hpp>
|
2018-07-24 13:22:44 +02:00
|
|
|
#include <nlohmann/detail/meta/type_traits.hpp>
|
2022-04-12 14:18:16 +02:00
|
|
|
#include <nlohmann/detail/string_concat.hpp>
|
2018-01-29 11:21:11 +01:00
|
|
|
#include <nlohmann/detail/value_t.hpp>
|
2017-08-14 16:33:55 +02:00
|
|
|
|
2022-07-30 21:59:13 +02:00
|
|
|
NLOHMANN_JSON_NAMESPACE_BEGIN
|
2017-08-14 16:33:55 +02:00
|
|
|
namespace detail
|
|
|
|
{
|
2022-07-30 21:59:13 +02:00
|
|
|
|
2018-07-21 10:31:55 +02:00
|
|
|
template<typename BasicJsonType>
|
2022-05-29 13:08:06 +02:00
|
|
|
inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
|
2018-07-21 10:31:55 +02:00
|
|
|
{
|
2020-06-03 14:20:36 +02:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(!j.is_null()))
|
2018-07-21 10:31:55 +02:00
|
|
|
{
|
2022-04-12 14:18:16 +02:00
|
|
|
JSON_THROW(type_error::create(302, concat("type must be null, but is ", j.type_name()), &j));
|
2018-07-21 10:31:55 +02:00
|
|
|
}
|
|
|
|
n = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-14 16:33:55 +02:00
|
|
|
// overloads for basic_json template parameters
|
2020-06-03 14:20:36 +02:00
|
|
|
template < typename BasicJsonType, typename ArithmeticType,
|
|
|
|
enable_if_t < std::is_arithmetic<ArithmeticType>::value&&
|
|
|
|
!std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
|
|
|
|
int > = 0 >
|
2017-08-14 16:33:55 +02:00
|
|
|
void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
|
|
|
|
{
|
|
|
|
switch (static_cast<value_t>(j))
|
|
|
|
{
|
|
|
|
case value_t::number_unsigned:
|
|
|
|
{
|
|
|
|
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case value_t::number_integer:
|
|
|
|
{
|
|
|
|
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case value_t::number_float:
|
|
|
|
{
|
|
|
|
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-08-12 16:33:41 +02:00
|
|
|
case value_t::null:
|
|
|
|
case value_t::object:
|
|
|
|
case value_t::array:
|
|
|
|
case value_t::string:
|
|
|
|
case value_t::boolean:
|
|
|
|
case value_t::binary:
|
|
|
|
case value_t::discarded:
|
2017-08-14 16:33:55 +02:00
|
|
|
default:
|
2022-04-12 14:18:16 +02:00
|
|
|
JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
|
2017-08-14 16:33:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BasicJsonType>
|
2022-05-29 13:08:06 +02:00
|
|
|
inline void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
2020-06-03 14:20:36 +02:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(!j.is_boolean()))
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
2022-04-12 14:18:16 +02:00
|
|
|
JSON_THROW(type_error::create(302, concat("type must be boolean, but is ", j.type_name()), &j));
|
2017-08-14 16:33:55 +02:00
|
|
|
}
|
|
|
|
b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BasicJsonType>
|
2022-05-29 13:08:06 +02:00
|
|
|
inline void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
2020-06-03 14:20:36 +02:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
2022-04-12 14:18:16 +02:00
|
|
|
JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
|
2017-08-14 16:33:55 +02:00
|
|
|
}
|
|
|
|
s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
|
|
|
|
}
|
|
|
|
|
2018-03-15 20:35:00 +01:00
|
|
|
template <
|
2022-04-08 10:22:47 +02:00
|
|
|
typename BasicJsonType, typename StringType,
|
2018-03-15 20:35:00 +01:00
|
|
|
enable_if_t <
|
2022-04-08 10:22:47 +02:00
|
|
|
std::is_assignable<StringType&, const typename BasicJsonType::string_t>::value
|
2022-07-28 21:52:23 +02:00
|
|
|
&& is_detected_exact<typename BasicJsonType::string_t::value_type, value_type_t, StringType>::value
|
2022-04-08 10:22:47 +02:00
|
|
|
&& !std::is_same<typename BasicJsonType::string_t, StringType>::value
|
|
|
|
&& !is_json_ref<StringType>::value, int > = 0 >
|
2022-05-29 13:08:06 +02:00
|
|
|
inline void from_json(const BasicJsonType& j, StringType& s)
|
2018-03-15 20:35:00 +01:00
|
|
|
{
|
2020-06-03 14:20:36 +02:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
|
2018-03-15 20:35:00 +01:00
|
|
|
{
|
2022-04-12 14:18:16 +02:00
|
|
|
JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
|
2018-03-15 20:35:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
|
|
|
|
}
|
|
|
|
|
2017-08-14 16:33:55 +02:00
|
|
|
template<typename BasicJsonType>
|
2022-05-29 13:08:06 +02:00
|
|
|
inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
|
|
|
get_arithmetic_value(j, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BasicJsonType>
|
2022-05-29 13:08:06 +02:00
|
|
|
inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
|
|
|
get_arithmetic_value(j, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BasicJsonType>
|
2022-05-29 13:08:06 +02:00
|
|
|
inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
|
|
|
get_arithmetic_value(j, val);
|
|
|
|
}
|
|
|
|
|
2022-06-16 19:34:32 +02:00
|
|
|
#if !JSON_DISABLE_ENUM_SERIALIZATION
|
2017-08-14 16:33:55 +02:00
|
|
|
template<typename BasicJsonType, typename EnumType,
|
|
|
|
enable_if_t<std::is_enum<EnumType>::value, int> = 0>
|
2022-05-29 13:08:06 +02:00
|
|
|
inline void from_json(const BasicJsonType& j, EnumType& e)
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
|
|
|
typename std::underlying_type<EnumType>::type val;
|
|
|
|
get_arithmetic_value(j, val);
|
|
|
|
e = static_cast<EnumType>(val);
|
|
|
|
}
|
2022-06-16 19:34:32 +02:00
|
|
|
#endif // JSON_DISABLE_ENUM_SERIALIZATION
|
2017-08-14 16:33:55 +02:00
|
|
|
|
|
|
|
// forward_list doesn't have an insert method
|
|
|
|
template<typename BasicJsonType, typename T, typename Allocator,
|
2020-07-16 11:11:35 +02:00
|
|
|
enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
|
2022-05-29 13:08:06 +02:00
|
|
|
inline void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
2020-06-03 14:20:36 +02:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
2022-04-12 14:18:16 +02:00
|
|
|
JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
|
2017-08-14 16:33:55 +02:00
|
|
|
}
|
2019-04-01 13:09:38 +02:00
|
|
|
l.clear();
|
2017-08-14 16:33:55 +02:00
|
|
|
std::transform(j.rbegin(), j.rend(),
|
|
|
|
std::front_inserter(l), [](const BasicJsonType & i)
|
|
|
|
{
|
|
|
|
return i.template get<T>();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// valarray doesn't have an insert method
|
|
|
|
template<typename BasicJsonType, typename T,
|
2020-07-16 11:11:35 +02:00
|
|
|
enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
|
2022-05-29 13:08:06 +02:00
|
|
|
inline void from_json(const BasicJsonType& j, std::valarray<T>& l)
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
2020-06-03 14:20:36 +02:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
2022-04-12 14:18:16 +02:00
|
|
|
JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
|
2017-08-14 16:33:55 +02:00
|
|
|
}
|
|
|
|
l.resize(j.size());
|
2020-07-16 11:11:35 +02:00
|
|
|
std::transform(j.begin(), j.end(), std::begin(l),
|
|
|
|
[](const BasicJsonType & elem)
|
|
|
|
{
|
|
|
|
return elem.template get<T>();
|
|
|
|
});
|
2017-08-14 16:33:55 +02:00
|
|
|
}
|
|
|
|
|
2020-06-03 14:20:36 +02:00
|
|
|
template<typename BasicJsonType, typename T, std::size_t N>
|
2021-10-14 19:19:46 +02:00
|
|
|
auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
|
2019-04-01 13:32:06 +02:00
|
|
|
-> decltype(j.template get<T>(), void())
|
|
|
|
{
|
|
|
|
for (std::size_t i = 0; i < N; ++i)
|
|
|
|
{
|
|
|
|
arr[i] = j.at(i).template get<T>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 17:59:10 +02:00
|
|
|
template<typename BasicJsonType>
|
2022-05-29 13:08:06 +02:00
|
|
|
inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
2018-09-05 17:59:10 +02:00
|
|
|
arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
|
|
|
|
}
|
2017-08-14 16:33:55 +02:00
|
|
|
|
2020-06-03 14:20:36 +02:00
|
|
|
template<typename BasicJsonType, typename T, std::size_t N>
|
2018-09-05 17:59:10 +02:00
|
|
|
auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
|
|
|
|
priority_tag<2> /*unused*/)
|
|
|
|
-> decltype(j.template get<T>(), void())
|
|
|
|
{
|
|
|
|
for (std::size_t i = 0; i < N; ++i)
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
2018-09-05 17:59:10 +02:00
|
|
|
arr[i] = j.at(i).template get<T>();
|
|
|
|
}
|
2017-08-14 16:33:55 +02:00
|
|
|
}
|
|
|
|
|
2021-01-13 20:47:03 +01:00
|
|
|
template<typename BasicJsonType, typename ConstructibleArrayType,
|
|
|
|
enable_if_t<
|
|
|
|
std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
|
|
|
|
int> = 0>
|
2018-10-16 14:00:34 +02:00
|
|
|
auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
|
2017-08-14 16:33:55 +02:00
|
|
|
-> decltype(
|
2018-10-16 14:00:34 +02:00
|
|
|
arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
|
|
|
|
j.template get<typename ConstructibleArrayType::value_type>(),
|
2017-08-14 16:33:55 +02:00
|
|
|
void())
|
|
|
|
{
|
|
|
|
using std::end;
|
|
|
|
|
2019-04-01 13:09:38 +02:00
|
|
|
ConstructibleArrayType ret;
|
|
|
|
ret.reserve(j.size());
|
2017-08-14 16:33:55 +02:00
|
|
|
std::transform(j.begin(), j.end(),
|
2019-04-01 13:09:38 +02:00
|
|
|
std::inserter(ret, end(ret)), [](const BasicJsonType & i)
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
|
|
|
// get<BasicJsonType>() returns *this, this won't call a from_json
|
|
|
|
// method when value_type is BasicJsonType
|
2018-10-16 14:00:34 +02:00
|
|
|
return i.template get<typename ConstructibleArrayType::value_type>();
|
2017-08-14 16:33:55 +02:00
|
|
|
});
|
2019-04-01 13:09:38 +02:00
|
|
|
arr = std::move(ret);
|
2017-08-14 16:33:55 +02:00
|
|
|
}
|
|
|
|
|
2021-01-13 20:47:03 +01:00
|
|
|
template<typename BasicJsonType, typename ConstructibleArrayType,
|
|
|
|
enable_if_t<
|
|
|
|
std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
|
|
|
|
int> = 0>
|
2022-05-29 13:08:06 +02:00
|
|
|
inline void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
|
|
|
|
priority_tag<0> /*unused*/)
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
2018-09-05 17:59:10 +02:00
|
|
|
using std::end;
|
|
|
|
|
2019-04-01 13:09:38 +02:00
|
|
|
ConstructibleArrayType ret;
|
2018-09-05 17:59:10 +02:00
|
|
|
std::transform(
|
2019-04-01 13:09:38 +02:00
|
|
|
j.begin(), j.end(), std::inserter(ret, end(ret)),
|
2018-09-05 17:59:10 +02:00
|
|
|
[](const BasicJsonType & i)
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
2018-09-05 17:59:10 +02:00
|
|
|
// get<BasicJsonType>() returns *this, this won't call a from_json
|
|
|
|
// method when value_type is BasicJsonType
|
2018-10-16 14:00:34 +02:00
|
|
|
return i.template get<typename ConstructibleArrayType::value_type>();
|
2018-09-05 17:59:10 +02:00
|
|
|
});
|
2019-04-01 13:09:38 +02:00
|
|
|
arr = std::move(ret);
|
2017-08-14 16:33:55 +02:00
|
|
|
}
|
|
|
|
|
2020-06-03 14:20:36 +02:00
|
|
|
template < typename BasicJsonType, typename ConstructibleArrayType,
|
|
|
|
enable_if_t <
|
|
|
|
is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value&&
|
|
|
|
!is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value&&
|
|
|
|
!is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
|
|
|
|
!std::is_same<ConstructibleArrayType, typename BasicJsonType::binary_t>::value&&
|
|
|
|
!is_basic_json<ConstructibleArrayType>::value,
|
|
|
|
int > = 0 >
|
2018-10-16 14:00:34 +02:00
|
|
|
auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
|
2018-09-05 17:59:10 +02:00
|
|
|
-> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
|
2018-10-16 14:00:34 +02:00
|
|
|
j.template get<typename ConstructibleArrayType::value_type>(),
|
2018-09-09 18:19:33 +02:00
|
|
|
void())
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
2020-06-03 14:20:36 +02:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
2022-04-12 14:18:16 +02:00
|
|
|
JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
|
2017-08-14 16:33:55 +02:00
|
|
|
}
|
|
|
|
|
2018-09-05 17:59:10 +02:00
|
|
|
from_json_array_impl(j, arr, priority_tag<3> {});
|
2017-08-14 16:33:55 +02:00
|
|
|
}
|
|
|
|
|
2021-01-12 18:28:29 +01:00
|
|
|
template < typename BasicJsonType, typename T, std::size_t... Idx >
|
|
|
|
std::array<T, sizeof...(Idx)> from_json_inplace_array_impl(BasicJsonType&& j,
|
|
|
|
identity_tag<std::array<T, sizeof...(Idx)>> /*unused*/, index_sequence<Idx...> /*unused*/)
|
2021-01-09 00:08:27 +01:00
|
|
|
{
|
2021-03-24 23:33:21 +01:00
|
|
|
return { { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... } };
|
2021-01-09 00:08:27 +01:00
|
|
|
}
|
|
|
|
|
2021-01-11 18:16:15 +01:00
|
|
|
template < typename BasicJsonType, typename T, std::size_t N >
|
2021-01-12 18:28:29 +01:00
|
|
|
auto from_json(BasicJsonType&& j, identity_tag<std::array<T, N>> tag)
|
|
|
|
-> decltype(from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
|
2021-01-09 00:08:27 +01:00
|
|
|
{
|
|
|
|
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
|
|
|
|
{
|
2022-04-12 14:18:16 +02:00
|
|
|
JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
|
2021-01-09 00:08:27 +01:00
|
|
|
}
|
|
|
|
|
2021-01-12 18:28:29 +01:00
|
|
|
return from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
|
2021-01-09 00:08:27 +01:00
|
|
|
}
|
|
|
|
|
2020-06-03 14:20:36 +02:00
|
|
|
template<typename BasicJsonType>
|
2022-05-29 13:08:06 +02:00
|
|
|
inline void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin)
|
2020-05-15 14:12:32 +02:00
|
|
|
{
|
2020-06-03 14:20:36 +02:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(!j.is_binary()))
|
2020-05-15 14:12:32 +02:00
|
|
|
{
|
2022-04-12 14:18:16 +02:00
|
|
|
JSON_THROW(type_error::create(302, concat("type must be binary, but is ", j.type_name()), &j));
|
2020-05-15 14:12:32 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 22:50:27 +02:00
|
|
|
bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
|
2020-05-15 14:12:32 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 14:00:34 +02:00
|
|
|
template<typename BasicJsonType, typename ConstructibleObjectType,
|
|
|
|
enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
|
2022-05-29 13:08:06 +02:00
|
|
|
inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
2020-06-03 14:20:36 +02:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
2022-04-12 14:18:16 +02:00
|
|
|
JSON_THROW(type_error::create(302, concat("type must be object, but is ", j.type_name()), &j));
|
2017-08-14 16:33:55 +02:00
|
|
|
}
|
|
|
|
|
2019-04-01 13:09:38 +02:00
|
|
|
ConstructibleObjectType ret;
|
2021-03-24 07:15:18 +01:00
|
|
|
const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
|
2018-10-16 14:00:34 +02:00
|
|
|
using value_type = typename ConstructibleObjectType::value_type;
|
2017-08-14 16:33:55 +02:00
|
|
|
std::transform(
|
|
|
|
inner_object->begin(), inner_object->end(),
|
2019-04-01 13:09:38 +02:00
|
|
|
std::inserter(ret, ret.begin()),
|
2017-08-14 16:33:55 +02:00
|
|
|
[](typename BasicJsonType::object_t::value_type const & p)
|
|
|
|
{
|
2018-10-16 14:00:34 +02:00
|
|
|
return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
|
2017-08-14 16:33:55 +02:00
|
|
|
});
|
2019-04-01 13:09:38 +02:00
|
|
|
obj = std::move(ret);
|
2017-08-14 16:33:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// overload for arithmetic types, not chosen for basic_json template arguments
|
|
|
|
// (BooleanType, etc..); note: Is it really necessary to provide explicit
|
|
|
|
// overloads for boolean_t etc. in case of a custom BooleanType which is not
|
|
|
|
// an arithmetic type?
|
2020-06-03 14:20:36 +02:00
|
|
|
template < typename BasicJsonType, typename ArithmeticType,
|
|
|
|
enable_if_t <
|
|
|
|
std::is_arithmetic<ArithmeticType>::value&&
|
|
|
|
!std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value&&
|
|
|
|
!std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value&&
|
|
|
|
!std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value&&
|
|
|
|
!std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
|
|
|
|
int > = 0 >
|
2022-05-29 13:08:06 +02:00
|
|
|
inline void from_json(const BasicJsonType& j, ArithmeticType& val)
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
|
|
|
switch (static_cast<value_t>(j))
|
|
|
|
{
|
|
|
|
case value_t::number_unsigned:
|
|
|
|
{
|
|
|
|
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case value_t::number_integer:
|
|
|
|
{
|
|
|
|
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case value_t::number_float:
|
|
|
|
{
|
|
|
|
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case value_t::boolean:
|
|
|
|
{
|
|
|
|
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-08-12 16:33:41 +02:00
|
|
|
case value_t::null:
|
|
|
|
case value_t::object:
|
|
|
|
case value_t::array:
|
|
|
|
case value_t::string:
|
|
|
|
case value_t::binary:
|
|
|
|
case value_t::discarded:
|
2017-08-14 16:33:55 +02:00
|
|
|
default:
|
2022-04-12 14:18:16 +02:00
|
|
|
JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
|
2017-08-14 16:33:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 20:47:03 +01:00
|
|
|
template<typename BasicJsonType, typename... Args, std::size_t... Idx>
|
|
|
|
std::tuple<Args...> from_json_tuple_impl_base(BasicJsonType&& j, index_sequence<Idx...> /*unused*/)
|
|
|
|
{
|
|
|
|
return std::make_tuple(std::forward<BasicJsonType>(j).at(Idx).template get<Args>()...);
|
|
|
|
}
|
|
|
|
|
2021-01-10 19:23:32 +01:00
|
|
|
template < typename BasicJsonType, class A1, class A2 >
|
2021-01-13 20:47:03 +01:00
|
|
|
std::pair<A1, A2> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::pair<A1, A2>> /*unused*/, priority_tag<0> /*unused*/)
|
2021-01-10 19:23:32 +01:00
|
|
|
{
|
|
|
|
return {std::forward<BasicJsonType>(j).at(0).template get<A1>(),
|
|
|
|
std::forward<BasicJsonType>(j).at(1).template get<A2>()};
|
|
|
|
}
|
|
|
|
|
2021-01-12 18:28:29 +01:00
|
|
|
template<typename BasicJsonType, typename A1, typename A2>
|
2022-05-29 13:08:06 +02:00
|
|
|
inline void from_json_tuple_impl(BasicJsonType&& j, std::pair<A1, A2>& p, priority_tag<1> /*unused*/)
|
2021-01-09 00:08:27 +01:00
|
|
|
{
|
2021-01-13 20:47:03 +01:00
|
|
|
p = from_json_tuple_impl(std::forward<BasicJsonType>(j), identity_tag<std::pair<A1, A2>> {}, priority_tag<0> {});
|
2021-01-12 18:28:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BasicJsonType, typename... Args>
|
2021-01-13 20:47:03 +01:00
|
|
|
std::tuple<Args...> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::tuple<Args...>> /*unused*/, priority_tag<2> /*unused*/)
|
2021-01-10 19:23:32 +01:00
|
|
|
{
|
2021-01-12 18:28:29 +01:00
|
|
|
return from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
|
2021-01-10 19:23:32 +01:00
|
|
|
}
|
|
|
|
|
2021-01-12 18:28:29 +01:00
|
|
|
template<typename BasicJsonType, typename... Args>
|
2022-05-29 13:08:06 +02:00
|
|
|
inline void from_json_tuple_impl(BasicJsonType&& j, std::tuple<Args...>& t, priority_tag<3> /*unused*/)
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
2021-01-12 18:28:29 +01:00
|
|
|
t = from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
|
2021-01-09 00:08:27 +01:00
|
|
|
}
|
|
|
|
|
2021-01-12 18:28:29 +01:00
|
|
|
template<typename BasicJsonType, typename TupleRelated>
|
|
|
|
auto from_json(BasicJsonType&& j, TupleRelated&& t)
|
2021-01-13 20:47:03 +01:00
|
|
|
-> decltype(from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {}))
|
2021-01-09 00:08:27 +01:00
|
|
|
{
|
|
|
|
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
|
|
|
|
{
|
2022-04-12 14:18:16 +02:00
|
|
|
JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
|
2021-01-09 00:08:27 +01:00
|
|
|
}
|
|
|
|
|
2021-01-13 20:47:03 +01:00
|
|
|
return from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {});
|
2017-08-14 16:33:55 +02:00
|
|
|
}
|
|
|
|
|
2020-06-03 14:20:36 +02:00
|
|
|
template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
|
|
|
|
typename = enable_if_t < !std::is_constructible <
|
|
|
|
typename BasicJsonType::string_t, Key >::value >>
|
2022-05-29 13:08:06 +02:00
|
|
|
inline void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
|
2018-05-14 11:51:37 +02:00
|
|
|
{
|
2020-06-03 14:20:36 +02:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
|
2018-05-14 11:51:37 +02:00
|
|
|
{
|
2022-04-12 14:18:16 +02:00
|
|
|
JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
|
2018-05-14 11:51:37 +02:00
|
|
|
}
|
2019-04-01 13:09:38 +02:00
|
|
|
m.clear();
|
2018-05-14 11:51:37 +02:00
|
|
|
for (const auto& p : j)
|
|
|
|
{
|
2020-06-03 14:20:36 +02:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
|
2018-05-14 11:51:37 +02:00
|
|
|
{
|
2022-04-12 14:18:16 +02:00
|
|
|
JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
|
2018-05-14 11:51:37 +02:00
|
|
|
}
|
|
|
|
m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 14:20:36 +02:00
|
|
|
template < typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
|
|
|
|
typename = enable_if_t < !std::is_constructible <
|
|
|
|
typename BasicJsonType::string_t, Key >::value >>
|
2022-05-29 13:08:06 +02:00
|
|
|
inline void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
|
2018-06-18 10:53:30 +02:00
|
|
|
{
|
2020-06-03 14:20:36 +02:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
|
2018-06-18 10:53:30 +02:00
|
|
|
{
|
2022-04-12 14:18:16 +02:00
|
|
|
JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
|
2018-06-18 10:53:30 +02:00
|
|
|
}
|
2019-04-01 13:09:38 +02:00
|
|
|
m.clear();
|
2018-06-18 10:53:30 +02:00
|
|
|
for (const auto& p : j)
|
|
|
|
{
|
2020-06-03 14:20:36 +02:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
|
2018-06-18 10:53:30 +02:00
|
|
|
{
|
2022-04-12 14:18:16 +02:00
|
|
|
JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
|
2018-06-18 10:53:30 +02:00
|
|
|
}
|
|
|
|
m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 09:47:05 +01:00
|
|
|
#if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
|
2021-10-14 19:19:46 +02:00
|
|
|
template<typename BasicJsonType>
|
2022-05-29 13:08:06 +02:00
|
|
|
inline void from_json(const BasicJsonType& j, std_fs::path& p)
|
2021-10-14 19:19:46 +02:00
|
|
|
{
|
|
|
|
if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
|
|
|
|
{
|
2022-04-12 14:18:16 +02:00
|
|
|
JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
|
2021-10-14 19:19:46 +02:00
|
|
|
}
|
|
|
|
p = *j.template get_ptr<const typename BasicJsonType::string_t*>();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-08-14 16:33:55 +02:00
|
|
|
struct from_json_fn
|
|
|
|
{
|
|
|
|
template<typename BasicJsonType, typename T>
|
2021-01-10 19:23:32 +01:00
|
|
|
auto operator()(const BasicJsonType& j, T&& val) const
|
|
|
|
noexcept(noexcept(from_json(j, std::forward<T>(val))))
|
|
|
|
-> decltype(from_json(j, std::forward<T>(val)))
|
2021-01-09 00:08:27 +01:00
|
|
|
{
|
2021-01-10 19:23:32 +01:00
|
|
|
return from_json(j, std::forward<T>(val));
|
2021-01-09 00:08:27 +01:00
|
|
|
}
|
2017-08-14 16:33:55 +02:00
|
|
|
};
|
2022-07-30 21:59:13 +02:00
|
|
|
|
2018-10-07 18:39:18 +02:00
|
|
|
} // namespace detail
|
2017-08-14 16:33:55 +02:00
|
|
|
|
2022-05-29 13:08:06 +02:00
|
|
|
#ifndef JSON_HAS_CPP_17
|
2017-08-14 16:33:55 +02:00
|
|
|
/// namespace to hold default `from_json` function
|
|
|
|
/// to see why this is required:
|
|
|
|
/// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
|
2021-03-24 07:15:18 +01:00
|
|
|
namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
|
2017-08-14 16:33:55 +02:00
|
|
|
{
|
2022-05-29 13:08:06 +02:00
|
|
|
#endif
|
|
|
|
JSON_INLINE_VARIABLE constexpr const auto& from_json = // NOLINT(misc-definitions-in-headers)
|
|
|
|
detail::static_const<detail::from_json_fn>::value;
|
|
|
|
#ifndef JSON_HAS_CPP_17
|
2022-07-30 21:59:13 +02:00
|
|
|
} // namespace
|
2022-05-29 13:08:06 +02:00
|
|
|
#endif
|
2022-07-30 21:59:13 +02:00
|
|
|
|
|
|
|
NLOHMANN_JSON_NAMESPACE_END
|