From b88e5f9107f5fc7f74fc644c26db4751f85ccddc Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 28 Sep 2026 21:26:39 +0200 Subject: [PATCH] Keep the behavior-changing configuration readable after json.hpp json.hpp undefines JSON_STRICT_NUL_HANDLING and JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON at its end. Code that builds on the library after it, such as the planned json_view.hpp, reads them from detail::abi_config instead. The constants live in the ABI namespace, which already encodes both settings, so they always match the basic_json in use. Signed-off-by: Niels Lohmann --- BUILD.bazel | 1 + include/nlohmann/detail/abi_config.hpp | 34 +++++++++++++++++++++++ include/nlohmann/json.hpp | 1 + single_include/nlohmann/json.hpp | 37 ++++++++++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 include/nlohmann/detail/abi_config.hpp diff --git a/BUILD.bazel b/BUILD.bazel index 03f73fa92..61ed0c753 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -20,6 +20,7 @@ cc_library( hdrs = [ "include/nlohmann/adl_serializer.hpp", "include/nlohmann/byte_container_with_subtype.hpp", + "include/nlohmann/detail/abi_config.hpp", "include/nlohmann/detail/abi_macros.hpp", "include/nlohmann/detail/bit_ops.hpp", "include/nlohmann/detail/conversions/from_json.hpp", diff --git a/include/nlohmann/detail/abi_config.hpp b/include/nlohmann/detail/abi_config.hpp new file mode 100644 index 000000000..0e99c81ad --- /dev/null +++ b/include/nlohmann/detail/abi_config.hpp @@ -0,0 +1,34 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.12.0 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann +// SPDX-License-Identifier: MIT + +#pragma once + +#include + +NLOHMANN_JSON_NAMESPACE_BEGIN +namespace detail +{ + +/*! +@brief the configuration macros that change the library's behavior + +json.hpp undefines these macros at its end (see macro_unscope.hpp), so code +that builds on the library after it (json_view.hpp) reads them here. Like the +macros, they are part of the ABI namespace, so they always match the +basic_json they are used with. +*/ +struct abi_config +{ + /// JSON_STRICT_NUL_HANDLING: a null byte is an error, not the end of input + static constexpr bool strict_nul_handling = JSON_STRICT_NUL_HANDLING != 0; + /// JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON + static constexpr bool legacy_discarded_value_comparison = JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON != 0; +}; + +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 78c4e4f62..e72b02c8d 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -45,6 +45,7 @@ #include #include +#include #include #include #include diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 3974f6add..6d58cfbb8 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -7236,6 +7236,43 @@ class byte_container_with_subtype : public BinaryType NLOHMANN_JSON_NAMESPACE_END +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.12.0 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +// #include + + +NLOHMANN_JSON_NAMESPACE_BEGIN +namespace detail +{ + +/*! +@brief the configuration macros that change the library's behavior + +json.hpp undefines these macros at its end (see macro_unscope.hpp), so code +that builds on the library after it (json_view.hpp) reads them here. Like the +macros, they are part of the ABI namespace, so they always match the +basic_json they are used with. +*/ +struct abi_config +{ + /// JSON_STRICT_NUL_HANDLING: a null byte is an error, not the end of input + static constexpr bool strict_nul_handling = JSON_STRICT_NUL_HANDLING != 0; + /// JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON + static constexpr bool legacy_discarded_value_comparison = JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON != 0; +}; + +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END + // #include // #include