* Add JSON_DISABLE_TUPLE_REFERENCE_CONVERSION to fix std::tuple conversions basic_json can be constructed from std::tuple<json&>, which it turns into a one-element array. Because of this, std::tuple picks its converting constructor that converts the whole source tuple instead of the element-wise one. As a result, std::tuple<const json&> built from std::forward_as_tuple(j) binds to a temporary (a compile error with libc++, a dangling reference with other standard libraries), and std::tuple<json> built the same way holds [j] instead of a copy of j. The new opt-in macro JSON_DISABLE_TUPLE_REFERENCE_CONVERSION (CMake option JSON_DisableTupleReferenceConversion) removes the conversion from a one-element tuple holding a reference to the same basic_json type, so std::tuple converts element-wise. It is off by default, so existing behavior is unchanged. It does not change any function body and therefore is not part of the ABI tag. Fixes #2226 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * 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> * Skip json reference tuple tests on clang < 4 and GCC < 5 ci_test_compilers_gcc_old (4.8) and ci_test_compilers_clang (3.4) could not compile the new tuple tests. Creating a std::tuple of basic_json references, e.g. std::forward_as_tuple(j), makes these compilers instantiate basic_json's conversion operator for libstdc++'s internal tuple bases, which fails hard. This happens with and without JSON_DISABLE_TUPLE_REFERENCE_CONVERSION, so it is a limitation of these compilers, not of the new option. Tested with the CI images: clang 3.4 to 3.9 and GCC 4.8 and 4.9 fail, clang 4, 5, and 6 and GCC 5 and 6 compile all cases. Skip only the checks that create such tuples; the is_constructible checks still run. Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
7.7 KiB
Macros
Some aspects of the library can be configured by defining preprocessor macros before including the json.hpp
header. See also the macro overview page.
Runtime assertions
- JSON_ASSERT(x) - control behavior of runtime assertions
Exceptions
- JSON_CATCH_USER(exception)
JSON_THROW_USER(exception)
JSON_TRY_USER - control exceptions - JSON_DIAGNOSTICS - control extended diagnostics
- JSON_DIAGNOSTIC_POSITIONS - access positions of elements
- JSON_NOEXCEPTION - switch off exceptions
Parsing
- JSON_PRECISE_STREAM_POSITION - opt in to leaving an input stream positioned right after a parsed number
- JSON_STRICT_NUL_HANDLING - opt in to rejecting a NUL byte in the input instead of treating it as end of input
Language support
- JSON_HAS_CPP_11
JSON_HAS_CPP_14
JSON_HAS_CPP_17
JSON_HAS_CPP_20 - set supported C++ standard - JSON_HAS_FILESYSTEM
JSON_HAS_EXPERIMENTAL_FILESYSTEM - controlstd::filesystemsupport - JSON_HAS_RANGES - control
std::rangessupport - JSON_HAS_STD_FORMAT - control
std::format/std::formattersupport - JSON_HAS_THREE_WAY_COMPARISON - control 3-way comparison support
- JSON_NO_AUTOMATIC_UDLS - do not include the user-defined string literals (UDLs) automatically
- JSON_NO_IO - switch off functions relying on certain C++ I/O headers
- JSON_NO_THREAD_LOCAL - switch off the use of
thread_localstorage - JSON_SKIP_UNSUPPORTED_COMPILER_CHECK - do not warn about unsupported compilers
- JSON_USE_GLOBAL_UDLS - place user-defined string literals (UDLs) into the global namespace
- JSON_USE_SIMDUTF - use the simdutf library to accelerate UTF-8 validation
Library version
- JSON_SKIP_LIBRARY_VERSION_CHECK - skip library version check
- NLOHMANN_JSON_VERSION_MAJOR
NLOHMANN_JSON_VERSION_MINOR
NLOHMANN_JSON_VERSION_PATCH - library version information
Library namespace
- NLOHMANN_JSON_NAMESPACE - full name of the
nlohmannnamespace - NLOHMANN_JSON_NAMESPACE_BEGIN
NLOHMANN_JSON_NAMESPACE_END - open and close the library namespace - NLOHMANN_JSON_NAMESPACE_NO_VERSION - disable the version component of the inline namespace
Type conversions
- JSON_BRACE_INIT_COPY_SEMANTICS - opt in to copy/move semantics for single-element brace initialization
- JSON_DISABLE_ENUM_SERIALIZATION - switch off default serialization/deserialization functions for enums
- JSON_DISABLE_TUPLE_REFERENCE_CONVERSION - switch off conversion from a one-element tuple of a JSON reference
- JSON_USE_IMPLICIT_CONVERSIONS - control implicit conversions
Comparison behavior
- JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON - control comparison of discarded values
Serialization/deserialization macros
Enums
- NLOHMANN_JSON_SERIALIZE_ENUM - serialize/deserialize an enum
- NLOHMANN_JSON_SERIALIZE_ENUM_STRICT - serialize/deserialize an enum with exceptions
Classes and structs
-
NLOHMANN_DEFINE_TYPE_INTRUSIVE - serialize/deserialize a non-derived class with private members
-
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT - serialize/deserialize a non-derived class with private members; uses default values
-
NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE - serialize a non-derived class with private members
-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE - serialize/deserialize a non-derived class
-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT - serialize/deserialize a non-derived class; uses default values
-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE - serialize a non-derived class
-
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE - serialize/deserialize a derived class with private members
-
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT - serialize/deserialize a derived class with private members; uses default values
-
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE - serialize a derived class with private members
-
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE - serialize/deserialize a derived class
-
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT - serialize/deserialize a derived class; uses default values
-
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE - serialize a derived class
-
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_NAMES - serialize/deserialize a non-derived class with private members; uses custom names
-
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT_WITH_NAMES - serialize/deserialize a non-derived class with private members; uses default values; uses custom names
-
NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE_WITH_NAMES - serialize a non-derived class with private members; uses custom names
-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_NAMES - serialize/deserialize a non-derived class; uses custom names
-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT_WITH_NAMES - serialize/deserialize a non-derived class; uses default values; uses custom names
-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE_WITH_NAMES - serialize a non-derived class; uses custom names
-
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_NAMES - serialize/deserialize a derived class with private members; uses custom names
-
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT_WITH_NAMES - serialize/deserialize a derived class with private members; uses default values; uses custom names
-
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE_WITH_NAMES - serialize a derived class with private members; uses custom names
-
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_NAMES - serialize/deserialize a derived class; uses custom names
-
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT_WITH_NAMES - serialize/deserialize a derived class; uses default values; uses custom names
-
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE_WITH_NAMES - serialize a derived class; uses custom names