mirror of
https://github.com/nlohmann/json.git
synced 2026-09-24 14:20:11 +01:00
* docs: document that a NUL byte in the input is treated as end of input A NUL byte anywhere in the input - trailing, or embedded ahead of more otherwise well-formed JSON - is currently treated the same as genuine end of input, so parsing silently stops there instead of raising the parse_error.101 any other unexpected byte triggers. This mirrors the NUL-terminated-C-string convention already used when no explicit input length is given (json::parse(const char*) already stops at strlen()), just applied uniformly rather than only when a length is genuinely unavailable. This behavior predates this change and is not being altered here - changing it would be an observable, backwards-incompatible behavior change for any caller that (knowingly or not) depends on it, which is not something to do silently in a patch. Documenting the current, verified behavior as a new FAQ entry instead, so it's an intentional and discoverable part of the contract rather than a surprise. Fixes #5530. Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01N4RQ1Ahan5YAGbnAQGjZTY * Add JSON_STRICT_NUL_HANDLING opt-in macro for issue #5530 A NUL byte anywhere in the input is currently treated the same as real end of input, rather than raising parse_error.101 like any other unexpected byte (documented in the previous commit's FAQ entry). A full unconditional fix was tried in PR #5532 but rejected as too risky to ship by default: any caller could depend on the current behavior, even unknowingly (e.g. a zero-padded buffer). On PR #5534, gregmarr proposed a compile-time opt-in flag instead, and the maintainer agreed, wanting it available now and defaulting to the corrected behavior in 4.0.0. This mirrors the existing JSON_BRACE_INIT_COPY_SEMANTICS precedent as closely as sensible: - JSON_STRICT_NUL_HANDLING defaults to 0 (off); the three lexer sites that treat '\0' as EOF/comment-terminator are gated with `#if !JSON_STRICT_NUL_HANDLING` so the default-off behavior is byte-for-byte identical to today's. - input_adapters.hpp's `T (&array)[N]` overload additionally trims a single trailing '\0' from a `char` array (e.g. a string literal like `json::parse("123")`) when the macro is on, so that case keeps working; every other element type (unsigned char, std::uint8_t, ...) always keeps its full extent. This intentionally does *not* reuse the existing strlen()-based pointer overload via SFINAE-excluding `char` from the array overload, as originally sketched for this change: that approach is ambiguous against the newer generic container overload added since PR #5532, and even where it compiles, strlen()-scanning a `char` array that is not NUL-terminated within its bounds reads past the end of the array (confirmed with AddressSanitizer). Trimming only a single trailing byte, without scanning, avoids both problems. - Documented via docs/mkdocs/docs/api/macros/json_strict_nul_handling.md, linked from the macros index/nav/features page, the FAQ entry, and the parse/accept/operator>> reference pages. - Tested in unit-class_parser.cpp and unit-deserialization.cpp, default state unguarded and opt-in state guarded. Since the library itself #undefs the macro at the end of json.hpp (as JSON_BRACE_INIT_COPY_SEMANTICS already does), a plain `#if defined(JSON_STRICT_NUL_HANDLING)` guard after the include never actually triggers; the tests instead capture the command-line value into a test-local macro before including the header. A few pre-existing fixtures elsewhere (std::array<uint8_t, N> sized one larger than their literal, relying on value-initialization to silently add a trailing zero byte) needed the same one-byte adjustment to keep passing under the opt-in behavior. Unlike the precedent, this adds a proper `JSON_StrictNulHandling` CMake option (rather than a raw -DCMAKE_CXX_FLAGS injection) and wires its ci_test_strict_nul_handling target into the ci_cmake_options job matrix in .github/workflows/ubuntu.yml, so the opt-in build is actually exercised in CI -- closing the one gap in the precedent's own CI setup (ci_test_brace_init_copy_semantics is defined but never referenced by any workflow, so it has never actually run). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Clarify where JSON_STRICT_NUL_HANDLING does not reject NUL bytes Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> Signed-off-by: Niels Lohmann <mail@nlohmann.me> Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
245 lines
8.6 KiB
CMake
245 lines
8.6 KiB
CMake
cmake_minimum_required(VERSION 3.5...4.0)
|
|
|
|
##
|
|
## PROJECT
|
|
## name and version
|
|
##
|
|
project(nlohmann_json VERSION 3.12.0 LANGUAGES CXX)
|
|
|
|
##
|
|
## MAIN_PROJECT CHECK
|
|
## determine if nlohmann_json is built as a subproject (using add_subdirectory) or if it is the main project
|
|
##
|
|
set(MAIN_PROJECT OFF)
|
|
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
|
|
set(MAIN_PROJECT ON)
|
|
endif()
|
|
|
|
##
|
|
## INCLUDE
|
|
##
|
|
##
|
|
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
|
|
|
|
if (POLICY CMP0077)
|
|
# Allow CMake 3.13+ to override options when using FetchContent / add_subdirectory.
|
|
cmake_policy(SET CMP0077 NEW)
|
|
endif ()
|
|
|
|
# ---- C++ Modules Support (optional) ----
|
|
option(NLOHMANN_JSON_BUILD_MODULES "Build C++ modules support" OFF)
|
|
|
|
if(NLOHMANN_JSON_BUILD_MODULES)
|
|
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.28)
|
|
message(STATUS "Building nlohmann.json C++ module")
|
|
add_subdirectory(src/modules)
|
|
else()
|
|
message(WARNING "Skipping nlohmann.json C++ module (requires CMake 3.28+, found ${CMAKE_VERSION})")
|
|
endif()
|
|
endif()
|
|
|
|
##
|
|
## OPTIONS
|
|
##
|
|
|
|
# VERSION_GREATER_EQUAL is not available in older CMake (< 3.7)
|
|
if(${MAIN_PROJECT} AND (${CMAKE_VERSION} VERSION_EQUAL 3.13 OR ${CMAKE_VERSION} VERSION_GREATER 3.13))
|
|
set(JSON_BuildTests_INIT ON)
|
|
else()
|
|
set(JSON_BuildTests_INIT OFF)
|
|
endif()
|
|
option(JSON_BuildTests "Build the unit tests when BUILD_TESTING is enabled." ${JSON_BuildTests_INIT})
|
|
option(JSON_CI "Enable CI build targets." OFF)
|
|
option(JSON_Diagnostics "Use extended diagnostic messages." OFF)
|
|
option(JSON_Diagnostic_Positions "Enable diagnostic positions." OFF)
|
|
option(JSON_GlobalUDLs "Place user-defined string literals in the global namespace." ON)
|
|
option(JSON_ImplicitConversions "Enable implicit conversions." ON)
|
|
option(JSON_DisableEnumSerialization "Disable default integer enum serialization." OFF)
|
|
option(JSON_LegacyDiscardedValueComparison "Enable legacy discarded value comparison." OFF)
|
|
option(JSON_Install "Install CMake targets during install step." ${MAIN_PROJECT})
|
|
option(JSON_MultipleHeaders "Use non-amalgamated version of the library." ON)
|
|
option(JSON_SystemInclude "Include as system headers (skip for clang-tidy)." OFF)
|
|
option(JSON_StrictNulHandling "Build with strict NUL-byte handling enabled." OFF)
|
|
|
|
if (JSON_CI)
|
|
include(ci)
|
|
endif ()
|
|
|
|
##
|
|
## CONFIGURATION
|
|
##
|
|
include(GNUInstallDirs)
|
|
|
|
if (NOT DEFINED NLOHMANN_JSON_TARGET_NAME)
|
|
# Allow overriding the target name when using FetchContent / add_subdirectory.
|
|
set(NLOHMANN_JSON_TARGET_NAME ${PROJECT_NAME})
|
|
endif()
|
|
|
|
set(NLOHMANN_JSON_CONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}" CACHE INTERNAL "")
|
|
set(NLOHMANN_JSON_INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}")
|
|
set(NLOHMANN_JSON_TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets")
|
|
set(NLOHMANN_JSON_CMAKE_CONFIG_TEMPLATE "cmake/config.cmake.in")
|
|
set(NLOHMANN_JSON_CMAKE_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}")
|
|
set(NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}ConfigVersion.cmake")
|
|
set(NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Config.cmake")
|
|
set(NLOHMANN_JSON_CMAKE_PROJECT_TARGETS_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Targets.cmake")
|
|
set(NLOHMANN_JSON_PKGCONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/pkgconfig")
|
|
|
|
if (JSON_MultipleHeaders)
|
|
set(NLOHMANN_JSON_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/include/")
|
|
message(STATUS "Using the multi-header code from ${NLOHMANN_JSON_INCLUDE_BUILD_DIR}")
|
|
else()
|
|
set(NLOHMANN_JSON_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/single_include/")
|
|
message(STATUS "Using the single-header code from ${NLOHMANN_JSON_INCLUDE_BUILD_DIR}")
|
|
endif()
|
|
|
|
if (NOT JSON_ImplicitConversions)
|
|
message(STATUS "Implicit conversions are disabled (JSON_USE_IMPLICIT_CONVERSIONS=0)")
|
|
endif()
|
|
|
|
if (JSON_DisableEnumSerialization)
|
|
message(STATUS "Enum integer serialization is disabled (JSON_DISABLE_ENUM_SERIALIZATION=1)")
|
|
endif()
|
|
|
|
if (JSON_LegacyDiscardedValueComparison)
|
|
message(STATUS "Legacy discarded value comparison enabled (JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON=1)")
|
|
endif()
|
|
|
|
if (JSON_Diagnostics)
|
|
message(STATUS "Diagnostics enabled (JSON_DIAGNOSTICS=1)")
|
|
endif()
|
|
|
|
if (JSON_StrictNulHandling)
|
|
message(STATUS "Strict NUL-byte handling enabled (JSON_STRICT_NUL_HANDLING=1)")
|
|
endif()
|
|
|
|
if (JSON_Diagnostic_Positions)
|
|
message(STATUS "Diagnostic positions enabled (JSON_DIAGNOSTIC_POSITIONS=1)")
|
|
endif()
|
|
|
|
if (NOT JSON_GlobalUDLs)
|
|
message(STATUS "User-defined string literals are not put in the global namespace (JSON_USE_GLOBAL_UDLS=0)")
|
|
endif()
|
|
|
|
if (JSON_SystemInclude)
|
|
set(NLOHMANN_JSON_SYSTEM_INCLUDE "SYSTEM")
|
|
endif()
|
|
|
|
##
|
|
## TARGET
|
|
## create target and add include path
|
|
##
|
|
add_library(${NLOHMANN_JSON_TARGET_NAME} INTERFACE)
|
|
add_library(${PROJECT_NAME}::${NLOHMANN_JSON_TARGET_NAME} ALIAS ${NLOHMANN_JSON_TARGET_NAME})
|
|
if (${CMAKE_VERSION} VERSION_LESS "3.8.0")
|
|
target_compile_features(${NLOHMANN_JSON_TARGET_NAME} INTERFACE cxx_range_for)
|
|
else()
|
|
target_compile_features(${NLOHMANN_JSON_TARGET_NAME} INTERFACE cxx_std_11)
|
|
endif()
|
|
|
|
target_compile_definitions(
|
|
${NLOHMANN_JSON_TARGET_NAME}
|
|
INTERFACE
|
|
$<$<NOT:$<BOOL:${JSON_GlobalUDLs}>>:JSON_USE_GLOBAL_UDLS=0>
|
|
$<$<NOT:$<BOOL:${JSON_ImplicitConversions}>>:JSON_USE_IMPLICIT_CONVERSIONS=0>
|
|
$<$<BOOL:${JSON_DisableEnumSerialization}>:JSON_DISABLE_ENUM_SERIALIZATION=1>
|
|
$<$<BOOL:${JSON_Diagnostics}>:JSON_DIAGNOSTICS=1>
|
|
$<$<BOOL:${JSON_Diagnostic_Positions}>:JSON_DIAGNOSTIC_POSITIONS=1>
|
|
$<$<BOOL:${JSON_LegacyDiscardedValueComparison}>:JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON=1>
|
|
$<$<BOOL:${JSON_StrictNulHandling}>:JSON_STRICT_NUL_HANDLING=1>
|
|
)
|
|
|
|
target_include_directories(
|
|
${NLOHMANN_JSON_TARGET_NAME}
|
|
${NLOHMANN_JSON_SYSTEM_INCLUDE} INTERFACE
|
|
$<BUILD_INTERFACE:${NLOHMANN_JSON_INCLUDE_BUILD_DIR}>
|
|
$<INSTALL_INTERFACE:${NLOHMANN_JSON_INCLUDE_INSTALL_DIR}>
|
|
)
|
|
|
|
## add debug view definition file for msvc (natvis)
|
|
if (MSVC)
|
|
set(NLOHMANN_ADD_NATVIS TRUE)
|
|
set(NLOHMANN_NATVIS_FILE "nlohmann_json.natvis")
|
|
target_sources(
|
|
${NLOHMANN_JSON_TARGET_NAME}
|
|
INTERFACE
|
|
$<INSTALL_INTERFACE:${NLOHMANN_NATVIS_FILE}>
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${NLOHMANN_NATVIS_FILE}>
|
|
)
|
|
endif()
|
|
|
|
# Install a pkg-config file, so other tools can find this.
|
|
configure_file(
|
|
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/pkg-config.pc.in"
|
|
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
|
|
@ONLY
|
|
)
|
|
|
|
##
|
|
## TESTS
|
|
## create and configure the unit test target
|
|
##
|
|
# Only build tests when JSON_BuildTests is set and testing has not been
|
|
# disabled by a parent project via BUILD_TESTING (see the CTest module, which
|
|
# also calls enable_testing()).
|
|
if (JSON_BuildTests AND (NOT DEFINED BUILD_TESTING OR BUILD_TESTING))
|
|
include(CTest)
|
|
add_subdirectory(tests)
|
|
endif()
|
|
|
|
##
|
|
## INSTALL
|
|
## install header files, generate and install cmake config files for find_package()
|
|
##
|
|
include(CMakePackageConfigHelpers)
|
|
# use a custom package version config file instead of
|
|
# write_basic_package_version_file to ensure that it's architecture-independent
|
|
# https://github.com/nlohmann/json/issues/1697
|
|
configure_file(
|
|
"cmake/nlohmann_jsonConfigVersion.cmake.in"
|
|
${NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE}
|
|
@ONLY
|
|
)
|
|
configure_file(
|
|
${NLOHMANN_JSON_CMAKE_CONFIG_TEMPLATE}
|
|
${NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE}
|
|
@ONLY
|
|
)
|
|
|
|
if(JSON_Install)
|
|
install(
|
|
DIRECTORY ${NLOHMANN_JSON_INCLUDE_BUILD_DIR}
|
|
DESTINATION ${NLOHMANN_JSON_INCLUDE_INSTALL_DIR}
|
|
)
|
|
install(
|
|
FILES ${NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE} ${NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE}
|
|
DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR}
|
|
)
|
|
if (NLOHMANN_ADD_NATVIS)
|
|
install(
|
|
FILES ${NLOHMANN_NATVIS_FILE}
|
|
DESTINATION .
|
|
)
|
|
endif()
|
|
export(
|
|
TARGETS ${NLOHMANN_JSON_TARGET_NAME}
|
|
NAMESPACE ${PROJECT_NAME}::
|
|
FILE ${NLOHMANN_JSON_CMAKE_PROJECT_TARGETS_FILE}
|
|
)
|
|
install(
|
|
TARGETS ${NLOHMANN_JSON_TARGET_NAME}
|
|
EXPORT ${NLOHMANN_JSON_TARGETS_EXPORT_NAME}
|
|
INCLUDES DESTINATION ${NLOHMANN_JSON_INCLUDE_INSTALL_DIR}
|
|
)
|
|
install(
|
|
EXPORT ${NLOHMANN_JSON_TARGETS_EXPORT_NAME}
|
|
NAMESPACE ${PROJECT_NAME}::
|
|
DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR}
|
|
)
|
|
install(
|
|
FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
|
|
DESTINATION ${NLOHMANN_JSON_PKGCONFIG_INSTALL_DIR}
|
|
)
|
|
include(CPack)
|
|
endif()
|