Merge 93aabac6ef into 700b95f447
This commit is contained in:
commit
cf2a272e6b
@ -34,6 +34,7 @@ option(JSON_BuildTests "Build the unit tests when BUILD_TESTING is enab
|
||||
option(JSON_CI "Enable CI build targets." OFF)
|
||||
option(JSON_Diagnostics "Use extended diagnostic messages." OFF)
|
||||
option(JSON_ImplicitConversions "Enable implicit conversions." ON)
|
||||
option(JSON_StrictContainerSize "Enable strict size checks for container conversions." OFF)
|
||||
option(JSON_Install "Install CMake targets during install step." ${MAIN_PROJECT})
|
||||
option(JSON_MultipleHeaders "Use non-amalgamated version of the library." OFF)
|
||||
option(JSON_SystemInclude "Include as system headers (skip for clang-tidy)." OFF)
|
||||
@ -94,6 +95,7 @@ target_compile_definitions(
|
||||
${NLOHMANN_JSON_TARGET_NAME}
|
||||
INTERFACE
|
||||
JSON_USE_IMPLICIT_CONVERSIONS=$<BOOL:${JSON_ImplicitConversions}>
|
||||
JSON_USE_STRICT_CONTAINER_SIZE=$<BOOL:${JSON_StrictContainerSize}>
|
||||
JSON_DIAGNOSTICS=$<BOOL:${JSON_Diagnostics}>
|
||||
)
|
||||
|
||||
|
||||
@ -186,6 +186,17 @@ template<typename BasicJsonType, typename T, std::size_t N>
|
||||
auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
|
||||
-> decltype(j.template get<T>(), void())
|
||||
{
|
||||
#if JSON_USE_STRICT_CONTAINER_SIZE
|
||||
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
|
||||
{
|
||||
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
|
||||
}
|
||||
if (JSON_HEDLEY_UNLIKELY(j.size() != N))
|
||||
{
|
||||
JSON_THROW(type_error::create(302, "array size must be " + std::to_string(N) + ", but is " + std::to_string(j.size()), j));
|
||||
}
|
||||
#endif
|
||||
|
||||
for (std::size_t i = 0; i < N; ++i)
|
||||
{
|
||||
arr[i] = j.at(i).template get<T>();
|
||||
@ -203,6 +214,14 @@ auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
|
||||
priority_tag<2> /*unused*/)
|
||||
-> decltype(j.template get<T>(), void())
|
||||
{
|
||||
#if JSON_USE_STRICT_CONTAINER_SIZE
|
||||
// array type check done in from_json already, only check size
|
||||
if (JSON_HEDLEY_UNLIKELY(j.size() != N))
|
||||
{
|
||||
JSON_THROW(type_error::create(302, "array size must be " + std::to_string(N) + ", but is " + std::to_string(j.size()), j));
|
||||
}
|
||||
#endif
|
||||
|
||||
for (std::size_t i = 0; i < N; ++i)
|
||||
{
|
||||
arr[i] = j.at(i).template get<T>();
|
||||
@ -279,6 +298,14 @@ 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*/)
|
||||
{
|
||||
#if JSON_USE_STRICT_CONTAINER_SIZE
|
||||
// array type check done in from_json already, only check size
|
||||
if (JSON_HEDLEY_UNLIKELY(j.size() != sizeof...(Idx)))
|
||||
{
|
||||
JSON_THROW(type_error::create(302, "array size must be " + std::to_string(sizeof...(Idx)) + ", but is " + std::to_string(j.size()), j));
|
||||
}
|
||||
#endif
|
||||
|
||||
return { { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... } };
|
||||
}
|
||||
|
||||
@ -378,12 +405,34 @@ void from_json(const BasicJsonType& j, ArithmeticType& val)
|
||||
template<typename BasicJsonType, typename... Args, std::size_t... Idx>
|
||||
std::tuple<Args...> from_json_tuple_impl_base(BasicJsonType&& j, index_sequence<Idx...> /*unused*/)
|
||||
{
|
||||
#if JSON_USE_STRICT_CONTAINER_SIZE
|
||||
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
|
||||
{
|
||||
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
|
||||
}
|
||||
if (JSON_HEDLEY_UNLIKELY(j.size() != sizeof...(Idx)))
|
||||
{
|
||||
JSON_THROW(type_error::create(302, "array size must be " + std::to_string(sizeof...(Idx)) + ", but is " + std::to_string(j.size()), j));
|
||||
}
|
||||
#endif
|
||||
|
||||
return std::make_tuple(std::forward<BasicJsonType>(j).at(Idx).template get<Args>()...);
|
||||
}
|
||||
|
||||
template < typename BasicJsonType, class A1, class A2 >
|
||||
std::pair<A1, A2> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::pair<A1, A2>> /*unused*/, priority_tag<0> /*unused*/)
|
||||
{
|
||||
#if JSON_USE_STRICT_CONTAINER_SIZE
|
||||
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
|
||||
{
|
||||
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
|
||||
}
|
||||
if (JSON_HEDLEY_UNLIKELY(j.size() != 2))
|
||||
{
|
||||
JSON_THROW(type_error::create(302, "array size must be 2, but is " + std::to_string(j.size()), j));
|
||||
}
|
||||
#endif
|
||||
|
||||
return {std::forward<BasicJsonType>(j).at(0).template get<A1>(),
|
||||
std::forward<BasicJsonType>(j).at(1).template get<A2>()};
|
||||
}
|
||||
|
||||
@ -411,6 +411,10 @@
|
||||
#define JSON_EXPLICIT explicit
|
||||
#endif
|
||||
|
||||
#ifndef JSON_USE_STRICT_CONTAINER_SIZE
|
||||
#define JSON_USE_STRICT_CONTAINER_SIZE 0
|
||||
#endif
|
||||
|
||||
#ifndef JSON_DIAGNOSTICS
|
||||
#define JSON_DIAGNOSTICS 0
|
||||
#endif
|
||||
|
||||
@ -2708,6 +2708,10 @@ using is_detected_convertible =
|
||||
#define JSON_EXPLICIT explicit
|
||||
#endif
|
||||
|
||||
#ifndef JSON_USE_STRICT_CONTAINER_SIZE
|
||||
#define JSON_USE_STRICT_CONTAINER_SIZE 0
|
||||
#endif
|
||||
|
||||
#ifndef JSON_DIAGNOSTICS
|
||||
#define JSON_DIAGNOSTICS 0
|
||||
#endif
|
||||
@ -3988,6 +3992,17 @@ template<typename BasicJsonType, typename T, std::size_t N>
|
||||
auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
|
||||
-> decltype(j.template get<T>(), void())
|
||||
{
|
||||
#if JSON_USE_STRICT_CONTAINER_SIZE
|
||||
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
|
||||
{
|
||||
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
|
||||
}
|
||||
if (JSON_HEDLEY_UNLIKELY(j.size() != N))
|
||||
{
|
||||
JSON_THROW(type_error::create(302, "array size must be " + std::to_string(N) + ", but is " + std::to_string(j.size()), j));
|
||||
}
|
||||
#endif
|
||||
|
||||
for (std::size_t i = 0; i < N; ++i)
|
||||
{
|
||||
arr[i] = j.at(i).template get<T>();
|
||||
@ -4005,6 +4020,14 @@ auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
|
||||
priority_tag<2> /*unused*/)
|
||||
-> decltype(j.template get<T>(), void())
|
||||
{
|
||||
#if JSON_USE_STRICT_CONTAINER_SIZE
|
||||
// array type check done in from_json already, only check size
|
||||
if (JSON_HEDLEY_UNLIKELY(j.size() != N))
|
||||
{
|
||||
JSON_THROW(type_error::create(302, "array size must be " + std::to_string(N) + ", but is " + std::to_string(j.size()), j));
|
||||
}
|
||||
#endif
|
||||
|
||||
for (std::size_t i = 0; i < N; ++i)
|
||||
{
|
||||
arr[i] = j.at(i).template get<T>();
|
||||
@ -4081,6 +4104,14 @@ 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*/)
|
||||
{
|
||||
#if JSON_USE_STRICT_CONTAINER_SIZE
|
||||
// array type check done in from_json already, only check size
|
||||
if (JSON_HEDLEY_UNLIKELY(j.size() != sizeof...(Idx)))
|
||||
{
|
||||
JSON_THROW(type_error::create(302, "array size must be " + std::to_string(sizeof...(Idx)) + ", but is " + std::to_string(j.size()), j));
|
||||
}
|
||||
#endif
|
||||
|
||||
return { { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... } };
|
||||
}
|
||||
|
||||
@ -4180,12 +4211,34 @@ void from_json(const BasicJsonType& j, ArithmeticType& val)
|
||||
template<typename BasicJsonType, typename... Args, std::size_t... Idx>
|
||||
std::tuple<Args...> from_json_tuple_impl_base(BasicJsonType&& j, index_sequence<Idx...> /*unused*/)
|
||||
{
|
||||
#if JSON_USE_STRICT_CONTAINER_SIZE
|
||||
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
|
||||
{
|
||||
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
|
||||
}
|
||||
if (JSON_HEDLEY_UNLIKELY(j.size() != sizeof...(Idx)))
|
||||
{
|
||||
JSON_THROW(type_error::create(302, "array size must be " + std::to_string(sizeof...(Idx)) + ", but is " + std::to_string(j.size()), j));
|
||||
}
|
||||
#endif
|
||||
|
||||
return std::make_tuple(std::forward<BasicJsonType>(j).at(Idx).template get<Args>()...);
|
||||
}
|
||||
|
||||
template < typename BasicJsonType, class A1, class A2 >
|
||||
std::pair<A1, A2> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::pair<A1, A2>> /*unused*/, priority_tag<0> /*unused*/)
|
||||
{
|
||||
#if JSON_USE_STRICT_CONTAINER_SIZE
|
||||
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
|
||||
{
|
||||
JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
|
||||
}
|
||||
if (JSON_HEDLEY_UNLIKELY(j.size() != 2))
|
||||
{
|
||||
JSON_THROW(type_error::create(302, "array size must be 2, but is " + std::to_string(j.size()), j));
|
||||
}
|
||||
#endif
|
||||
|
||||
return {std::forward<BasicJsonType>(j).at(0).template get<A1>(),
|
||||
std::forward<BasicJsonType>(j).at(1).template get<A2>()};
|
||||
}
|
||||
|
||||
@ -267,6 +267,7 @@ TEST_CASE("constructors")
|
||||
CHECK(j[1] == std::get<1>(p));
|
||||
}
|
||||
|
||||
#if !(JSON_USE_STRICT_CONTAINER_SIZE)
|
||||
SECTION("std::pair with discarded values")
|
||||
{
|
||||
json j{1, 2.0, "string"};
|
||||
@ -275,6 +276,7 @@ TEST_CASE("constructors")
|
||||
CHECK(p.first == j[0]);
|
||||
CHECK(p.second == j[1]);
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("std::tuple")
|
||||
{
|
||||
@ -291,6 +293,7 @@ TEST_CASE("constructors")
|
||||
CHECK(j[3][1] == 1);
|
||||
}
|
||||
|
||||
#if !(JSON_USE_STRICT_CONTAINER_SIZE)
|
||||
SECTION("std::tuple with discarded values")
|
||||
{
|
||||
json j{1, 2.0, "string", 42};
|
||||
@ -300,9 +303,27 @@ TEST_CASE("constructors")
|
||||
CHECK(std::get<1>(t) == j[1]);
|
||||
CHECK(std::get<2>(t) == j[2]);
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("std::pair/tuple/array failures")
|
||||
{
|
||||
#if JSON_USE_STRICT_CONTAINER_SIZE
|
||||
json j1{1};
|
||||
json j2{1, 2, 3};
|
||||
|
||||
CHECK_THROWS_AS((j1.get<std::pair<int, int>>()), json::type_error&);
|
||||
CHECK_THROWS_WITH((j1.get<std::pair<int, int>>()), "[json.exception.type_error.302] array size must be 2, but is 1");
|
||||
CHECK_THROWS_AS((j2.get<std::pair<int, int>>()), json::type_error&);
|
||||
CHECK_THROWS_WITH((j2.get<std::pair<int, int>>()), "[json.exception.type_error.302] array size must be 2, but is 3");
|
||||
CHECK_THROWS_AS((j1.get<std::tuple<int, int>>()), json::type_error&);
|
||||
CHECK_THROWS_WITH((j1.get<std::tuple<int, int>>()), "[json.exception.type_error.302] array size must be 2, but is 1");
|
||||
CHECK_THROWS_AS((j2.get<std::tuple<int, int>>()), json::type_error&);
|
||||
CHECK_THROWS_WITH((j2.get<std::tuple<int, int>>()), "[json.exception.type_error.302] array size must be 2, but is 3");
|
||||
CHECK_THROWS_AS((j1.get<std::array<int, 3>>()), json::type_error&);
|
||||
CHECK_THROWS_WITH((j1.get<std::array<int, 3>>()), "[json.exception.type_error.302] array size must be 3, but is 1");
|
||||
CHECK_THROWS_AS((j2.get<std::array<int, 1>>()), json::type_error&);
|
||||
CHECK_THROWS_WITH((j2.get<std::array<int, 1>>()), "[json.exception.type_error.302] array size must be 1, but is 3");
|
||||
#else
|
||||
json j{1};
|
||||
|
||||
CHECK_THROWS_AS((j.get<std::pair<int, int>>()), json::out_of_range&);
|
||||
@ -311,6 +332,7 @@ TEST_CASE("constructors")
|
||||
CHECK_THROWS_WITH((j.get<std::tuple<int, int>>()), "[json.exception.out_of_range.401] array index 1 is out of range");
|
||||
CHECK_THROWS_AS((j.get<std::array<int, 3>>()), json::out_of_range&);
|
||||
CHECK_THROWS_WITH((j.get<std::array<int, 3>>()), "[json.exception.out_of_range.401] array index 1 is out of range");
|
||||
#endif
|
||||
}
|
||||
|
||||
SECTION("std::forward_list<json>")
|
||||
|
||||
@ -40,6 +40,8 @@ using nlohmann::json;
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <valarray>
|
||||
#include <utility>
|
||||
#include <tuple>
|
||||
|
||||
#if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
|
||||
#define JSON_HAS_CPP_17
|
||||
@ -397,6 +399,50 @@ TEST_CASE("value conversion")
|
||||
json j2 = nbs;
|
||||
j2.get_to(nbs2);
|
||||
CHECK(std::equal(std::begin(nbs), std::end(nbs), std::begin(nbs2)));
|
||||
|
||||
SECTION("built-in array is larger than JSON")
|
||||
{
|
||||
json j1 = {1, 2, 3, 4};
|
||||
int arr6[] = {1, 2, 3, 4, 5, 6};
|
||||
#if JSON_USE_STRICT_CONTAINER_SIZE
|
||||
CHECK_THROWS_AS(j1.get_to(arr6), json::type_error&);
|
||||
CHECK_THROWS_WITH(j1.get_to(arr6), "[json.exception.type_error.302] "
|
||||
"array size must be 6, but is 4");
|
||||
#else
|
||||
CHECK_THROWS_AS(j1.get_to(arr6), json::out_of_range&);
|
||||
CHECK_THROWS_WITH(j1.get_to(arr6), "[json.exception.out_of_range.401] "
|
||||
"array index 4 is out of range");
|
||||
#endif
|
||||
}
|
||||
|
||||
SECTION("built-in array is smaller than JSON")
|
||||
{
|
||||
json j1 = {1, 2, 3, 4};
|
||||
int arr2[] = {8, 9};
|
||||
#if JSON_USE_STRICT_CONTAINER_SIZE
|
||||
CHECK_THROWS_AS(j1.get_to(arr2), json::type_error&);
|
||||
CHECK_THROWS_WITH(j1.get_to(arr2), "[json.exception.type_error.302] "
|
||||
"array size must be 2, but is 4");
|
||||
#else
|
||||
j1.get_to(arr2);
|
||||
CHECK(arr2[0] == 1);
|
||||
CHECK(arr2[1] == 2);
|
||||
#endif
|
||||
}
|
||||
|
||||
SECTION("built-in array from non-array type")
|
||||
{
|
||||
json j1;
|
||||
int arr2[] = {8, 9};
|
||||
CHECK_THROWS_AS(j1.get_to(arr2), json::type_error&);
|
||||
#if JSON_USE_STRICT_CONTAINER_SIZE
|
||||
CHECK_THROWS_WITH(j1.get_to(arr2), "[json.exception.type_error.302] "
|
||||
"type must be array, but is null");
|
||||
#else
|
||||
CHECK_THROWS_WITH(j1.get_to(arr2), "[json.exception.type_error.304] "
|
||||
"cannot use at() with null");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("std::deque<json>")
|
||||
@ -1487,7 +1533,7 @@ TEST_CASE("value conversion")
|
||||
SECTION("std::array")
|
||||
{
|
||||
j1.get<std::array<int, 4>>();
|
||||
j2.get<std::array<unsigned int, 3>>();
|
||||
j2.get<std::array<unsigned int, 4>>();
|
||||
j3.get<std::array<double, 4>>();
|
||||
j4.get<std::array<bool, 3>>();
|
||||
j5.get<std::array<std::string, 3>>();
|
||||
@ -1495,17 +1541,29 @@ TEST_CASE("value conversion")
|
||||
SECTION("std::array is larger than JSON")
|
||||
{
|
||||
std::array<int, 6> arr6 = {{1, 2, 3, 4, 5, 6}};
|
||||
#if JSON_USE_STRICT_CONTAINER_SIZE
|
||||
CHECK_THROWS_AS(j1.get_to(arr6), json::type_error&);
|
||||
CHECK_THROWS_WITH(j1.get_to(arr6), "[json.exception.type_error.302] "
|
||||
"array size must be 6, but is 4");
|
||||
#else
|
||||
CHECK_THROWS_AS(j1.get_to(arr6), json::out_of_range&);
|
||||
CHECK_THROWS_WITH(j1.get_to(arr6), "[json.exception.out_of_range.401] "
|
||||
"array index 4 is out of range");
|
||||
#endif
|
||||
}
|
||||
|
||||
SECTION("std::array is smaller than JSON")
|
||||
{
|
||||
std::array<int, 2> arr2 = {{8, 9}};
|
||||
#if JSON_USE_STRICT_CONTAINER_SIZE
|
||||
CHECK_THROWS_AS(j1.get_to(arr2), json::type_error&);
|
||||
CHECK_THROWS_WITH(j1.get_to(arr2), "[json.exception.type_error.302] "
|
||||
"array size must be 2, but is 4");
|
||||
#else
|
||||
j1.get_to(arr2);
|
||||
CHECK(arr2[0] == 1);
|
||||
CHECK(arr2[1] == 2);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@ -1612,8 +1670,10 @@ TEST_CASE("value conversion")
|
||||
{
|
||||
CHECK_THROWS_AS((json().get<std::list<int>>()), json::type_error&);
|
||||
CHECK_THROWS_AS((json().get<std::vector<int>>()), json::type_error&);
|
||||
CHECK_THROWS_AS((json().get<std::array<int, 2>>()), json::type_error&);
|
||||
CHECK_THROWS_AS((json().get<std::vector<json>>()), json::type_error&);
|
||||
CHECK_THROWS_AS((json().get<std::list<json>>()), json::type_error&);
|
||||
CHECK_THROWS_AS((json().get<std::array<json, 2>>()), json::type_error&);
|
||||
CHECK_THROWS_AS((json().get<std::valarray<int>>()), json::type_error&);
|
||||
|
||||
// does type really must be an array? or it rather must not be null?
|
||||
@ -1624,12 +1684,18 @@ TEST_CASE("value conversion")
|
||||
CHECK_THROWS_WITH(
|
||||
(json().get<std::vector<int>>()),
|
||||
"[json.exception.type_error.302] type must be array, but is null");
|
||||
CHECK_THROWS_WITH(
|
||||
(json().get<std::array<int, 2>>()),
|
||||
"[json.exception.type_error.302] type must be array, but is null");
|
||||
CHECK_THROWS_WITH(
|
||||
(json().get<std::vector<json>>()),
|
||||
"[json.exception.type_error.302] type must be array, but is null");
|
||||
CHECK_THROWS_WITH(
|
||||
(json().get<std::list<json>>()),
|
||||
"[json.exception.type_error.302] type must be array, but is null");
|
||||
CHECK_THROWS_WITH(
|
||||
(json().get<std::array<json, 2>>()),
|
||||
"[json.exception.type_error.302] type must be array, but is null");
|
||||
CHECK_THROWS_WITH(
|
||||
(json().get<std::valarray<int>>()),
|
||||
"[json.exception.type_error.302] type must be array, but is null");
|
||||
@ -1638,6 +1704,115 @@ TEST_CASE("value conversion")
|
||||
"[json.exception.type_error.302] type must be array, but is null");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("other STL containers")
|
||||
{
|
||||
json j1 = {1, 2};
|
||||
json j2 = {1u, 2u};
|
||||
json j3 = {1.2, 2.3};
|
||||
json j4 = {true, false};
|
||||
json j5 = {"one", "two"};
|
||||
json j6 = {1, "2"};
|
||||
json j7 = {1, 2.3};
|
||||
json j8 = {1, false};
|
||||
json j9 = {1, 2, 3};
|
||||
json j10 = {1};
|
||||
|
||||
SECTION("std::pair")
|
||||
{
|
||||
j1.get<std::pair<int, int>>();
|
||||
j2.get<std::pair<unsigned int, unsigned int>>();
|
||||
j3.get<std::pair<double, double>>();
|
||||
j4.get<std::pair<bool, bool>>();
|
||||
j5.get<std::pair<std::string, std::string>>();
|
||||
j6.get<std::pair<int, std::string>>();
|
||||
j7.get<std::pair<int, double>>();
|
||||
j8.get<std::pair<int, bool>>();
|
||||
|
||||
SECTION("std::pair is larger than JSON")
|
||||
{
|
||||
std::pair<int, int> p(8, 9);
|
||||
#if JSON_USE_STRICT_CONTAINER_SIZE
|
||||
CHECK_THROWS_AS(j10.get_to(p), json::type_error&);
|
||||
CHECK_THROWS_WITH(j10.get_to(p), "[json.exception.type_error.302] "
|
||||
"array size must be 2, but is 1");
|
||||
#else
|
||||
CHECK_THROWS_AS(j10.get_to(p), json::out_of_range&);
|
||||
CHECK_THROWS_WITH(j10.get_to(p), "[json.exception.out_of_range.401] "
|
||||
"array index 1 is out of range");
|
||||
#endif
|
||||
}
|
||||
|
||||
SECTION("std::pair is smaller than JSON")
|
||||
{
|
||||
std::pair<int, int> p(8, 9);
|
||||
#if JSON_USE_STRICT_CONTAINER_SIZE
|
||||
CHECK_THROWS_AS(j9.get_to(p), json::type_error&);
|
||||
CHECK_THROWS_WITH(j9.get_to(p), "[json.exception.type_error.302] "
|
||||
"array size must be 2, but is 3");
|
||||
#else
|
||||
j9.get_to(p);
|
||||
CHECK(p.first == 1);
|
||||
CHECK(p.second == 2);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("std::tuple")
|
||||
{
|
||||
j1.get<std::tuple<int, int>>();
|
||||
j2.get<std::tuple<unsigned int, unsigned int>>();
|
||||
j3.get<std::tuple<double, double>>();
|
||||
j4.get<std::tuple<bool, bool>>();
|
||||
j5.get<std::tuple<std::string, std::string>>();
|
||||
j6.get<std::tuple<int, std::string>>();
|
||||
j7.get<std::tuple<int, double>>();
|
||||
j8.get<std::tuple<int, bool>>();
|
||||
j9.get<std::tuple<int, int, int>>();
|
||||
j10.get<std::tuple<int>>();
|
||||
|
||||
SECTION("std::tuple is larger than JSON")
|
||||
{
|
||||
std::tuple<int, int> t(8, 9);
|
||||
#if JSON_USE_STRICT_CONTAINER_SIZE
|
||||
CHECK_THROWS_AS(j10.get_to(t), json::type_error&);
|
||||
CHECK_THROWS_WITH(j10.get_to(t), "[json.exception.type_error.302] "
|
||||
"array size must be 2, but is 1");
|
||||
#else
|
||||
CHECK_THROWS_AS(j10.get_to(t), json::out_of_range&);
|
||||
CHECK_THROWS_WITH(j10.get_to(t), "[json.exception.out_of_range.401] "
|
||||
"array index 1 is out of range");
|
||||
#endif
|
||||
}
|
||||
|
||||
SECTION("std::tuple is smaller than JSON")
|
||||
{
|
||||
std::tuple<int, int> t(8, 9);
|
||||
#if JSON_USE_STRICT_CONTAINER_SIZE
|
||||
CHECK_THROWS_AS(j9.get_to(t), json::type_error&);
|
||||
CHECK_THROWS_WITH(j9.get_to(t), "[json.exception.type_error.302] "
|
||||
"array size must be 2, but is 3");
|
||||
#else
|
||||
j9.get_to(t);
|
||||
CHECK(std::get<0>(t) == 1);
|
||||
CHECK(std::get<1>(t) == 2);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("exception in case of a non-object type")
|
||||
{
|
||||
CHECK_THROWS_AS((json().get<std::pair<int, int>>()), json::type_error&);
|
||||
CHECK_THROWS_AS((json().get<std::tuple<int, int>>()), json::type_error&);
|
||||
|
||||
CHECK_THROWS_WITH(
|
||||
(json().get<std::pair<int, int>>()),
|
||||
"[json.exception.type_error.302] type must be array, but is null");
|
||||
CHECK_THROWS_WITH(
|
||||
(json().get<std::tuple<int, int>>()),
|
||||
"[json.exception.type_error.302] type must be array, but is null");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user