mirror of
https://github.com/nlohmann/json.git
synced 2026-11-22 02:44:22 +00:00
Convert one-element tuples to arrays on every compiler
to_json for std::tuple assigns a braced list, j = { std::get<Idx>(t)... }.
With a single element that is itself a basic_json, Apple clang 15 and 16
treat j = {x} as a copy of x, so std::tuple<json>{true} became true
instead of [true]. The macOS jobs (Xcode 15.1, 16.1) failed the new
checks in unit-disable-tuple-reference-conversion and unit-regression2.
The one-element overload that already handles
JSON_BRACE_INIT_COPY_SEMANTICS builds the array (or object, for a
[string, value] element) explicitly, the same way the initializer-list
constructor does. Use it unconditionally. The output is unchanged on
compilers that already wrapped the element.
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
2 files changed
+14
-12
No files matched your search
@@ -471,11 +471,13 @@ inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<
|
|||||||
j = { std::get<Idx>(t)... };
|
j = { std::get<Idx>(t)... };
|
||||||
}
|
}
|
||||||
|
|
||||||
#if JSON_BRACE_INIT_COPY_SEMANTICS
|
// A one-element braced list does not reliably wrap its element: with
|
||||||
// JSON_BRACE_INIT_COPY_SEMANTICS makes a one-element braced list copy its
|
// JSON_BRACE_INIT_COPY_SEMANTICS it copies it, which would serialize
|
||||||
// element instead of wrapping it, which would serialize std::tuple<int>{5} as 5
|
// std::tuple<int>{5} as 5 rather than [5], and some compilers (e.g., Apple clang
|
||||||
// rather than [5]. Build what the default deduction builds instead: an object
|
// 15 and 16) copy an element that is itself a basic_json even without it, so
|
||||||
// if the element is a [string, value] pair, a one-element array otherwise.
|
// std::tuple<json>{true} became true rather than [true]. Build what the default
|
||||||
|
// deduction builds instead: an object if the element is a [string, value] pair,
|
||||||
|
// a one-element array otherwise.
|
||||||
template<typename BasicJsonType, typename Tuple>
|
template<typename BasicJsonType, typename Tuple>
|
||||||
inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<0> /*unused*/)
|
inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<0> /*unused*/)
|
||||||
{
|
{
|
||||||
@@ -493,7 +495,6 @@ inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<
|
|||||||
j = BasicJsonType::array({std::move(element)});
|
j = BasicJsonType::array({std::move(element)});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
template<typename BasicJsonType, typename Tuple>
|
template<typename BasicJsonType, typename Tuple>
|
||||||
inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& /*unused*/, index_sequence<> /*unused*/)
|
inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& /*unused*/, index_sequence<> /*unused*/)
|
||||||
|
|||||||
@@ -7016,11 +7016,13 @@ inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<
|
|||||||
j = { std::get<Idx>(t)... };
|
j = { std::get<Idx>(t)... };
|
||||||
}
|
}
|
||||||
|
|
||||||
#if JSON_BRACE_INIT_COPY_SEMANTICS
|
// A one-element braced list does not reliably wrap its element: with
|
||||||
// JSON_BRACE_INIT_COPY_SEMANTICS makes a one-element braced list copy its
|
// JSON_BRACE_INIT_COPY_SEMANTICS it copies it, which would serialize
|
||||||
// element instead of wrapping it, which would serialize std::tuple<int>{5} as 5
|
// std::tuple<int>{5} as 5 rather than [5], and some compilers (e.g., Apple clang
|
||||||
// rather than [5]. Build what the default deduction builds instead: an object
|
// 15 and 16) copy an element that is itself a basic_json even without it, so
|
||||||
// if the element is a [string, value] pair, a one-element array otherwise.
|
// std::tuple<json>{true} became true rather than [true]. Build what the default
|
||||||
|
// deduction builds instead: an object if the element is a [string, value] pair,
|
||||||
|
// a one-element array otherwise.
|
||||||
template<typename BasicJsonType, typename Tuple>
|
template<typename BasicJsonType, typename Tuple>
|
||||||
inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<0> /*unused*/)
|
inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<0> /*unused*/)
|
||||||
{
|
{
|
||||||
@@ -7038,7 +7040,6 @@ inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<
|
|||||||
j = BasicJsonType::array({std::move(element)});
|
j = BasicJsonType::array({std::move(element)});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
template<typename BasicJsonType, typename Tuple>
|
template<typename BasicJsonType, typename Tuple>
|
||||||
inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& /*unused*/, index_sequence<> /*unused*/)
|
inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& /*unused*/, index_sequence<> /*unused*/)
|
||||||
|
|||||||
Reference in new issue
Block a user