Move the user-defined string literals to <nlohmann/json_literals.hpp> and add JSON_NO_AUTOMATIC_UDLS - #5610
Conversation
The bodies of operator""_json and operator""_json_pointer call the parser, so every translation unit including the library instantiates it, even if it never parses anything. Defining JSON_NO_UDLS leaves the literals out entirely, which saves 15-35% compile time for such translation units (#5294). Nothing changes if the macro is not defined. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
|
See comments at #5294 (comment) for minor tweaks. |
Following the review in #5294, the literals now live in their own header instead of being removed entirely: <nlohmann/json.hpp> includes it at the end unless JSON_NO_AUTOMATIC_UDLS (renamed from JSON_NO_UDLS) is defined, so a project can opt out globally and include the header only where the literals are used. The header only uses public and standard macros, because the library's internal macros are undefined at the end of json.hpp and the amalgamation inlines macro_scope.hpp only once. For the same reason, the library no longer defines and undefines JSON_USE_GLOBAL_UDLS, so a user's definition is still visible to the header. The single-header copy is identical to the multi-header one, as it only includes <nlohmann/json.hpp>. The module always exports the literals. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Adding a new header introduces a lot of moving parts. What do you think? |
|
I think if your goal is reducing compile times without breaking anyone, then this is the best way to do that. It allows an opt-in even in the case where someone wants to include this library in a precompiled header, so the base is available everywhere, and then allows the literals to be added where needed. I think this could be one of those v4.0.0 macros that preserves the current behavior until that release, and then those that need the literals can just include the extra header. The question is whether people would still want it in the single header. I can see things on both sides of this. |
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
…DLs off in the JSON_NO_AUTOMATIC_UDLS test - Suppress clang-tidy misc-header-include-cycle on the intentional mutual include of json.hpp and json_literals.hpp. - Use operator"" _json with a space for GCC 4.8 in the test's detection aliases, as the header does. - Only test the global literal operators when JSON_USE_GLOBAL_UDLS is on. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
The GCC 4.8 spacing condition was repeated for both operator definitions and the global using-declarations. NLOHMANN_JSON_LITERAL_OPERATOR(suffix) now selects operator""##suffix or operator"" suffix in one place and is undefined at the end of json_literals.hpp. Suggested by gregmarr in review. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
The `release` target signed and copied json_fwd.hpp into release_files alongside json.hpp, but the shasum line that writes hashes.txt only listed json.hpp, include.zip and json.tar.xz. Users could not verify the published json_fwd.hpp against hashes.txt. Hash every file in release_files except the .asc signatures instead of naming files by hand, so a newly shipped header (such as the json_literals.hpp that #5610 adds to this target) cannot be missed again. Only affects the generated hashes.txt release artifact; the library itself is unaffected. Overlaps #5610, which touches the same lines to add json_literals.hpp to the release target. #5717 item 1 Signed-off-by: Niels Lohmann <mail@nlohmann.me>
The pragma (added in #5164) claimed to work around the C++ modules redefinition errors of #5103, but #5103 is about hard errors (e.g. "redefinition of std::__is_constant_evaluated()", conflicting std::integral_constant) that ignoring a warning cannot suppress; they are traced to GCC PR 124430 and reproduce with <map> or <string> instead of json.hpp too. A GCC 16.2 -std=gnu++20 -fmodules build following #5103's repro steps still fails with the pragma in place, and a build of all test TUs with GCC_CXXFLAGS (which enable -Wignored-attributes) and the pragma removed produces no such warning. The block only hid a warning class from GCC C++20 users while suggesting #5103 was handled. Overlaps #5610, whose hunks touch the closing half of this pragma to insert the json_literals.hpp include. Signed-off-by: Niels Lohmann <mail@nlohmann.me> #5725 item 9
…zel (#5715 item 5) The amalgamation/format check existed three times with three different file sets: the Makefile's pretty/check-amalgamation, the pull_request-only check_amalgamation.yml workflow, and the ci_test_amalgamation CMake target that also runs on direct pushes to develop/master/release/*. The CMake target's glob was a strict subset of the workflow's (missing the docs/mkdocs/docs/examples/*.hpp headers, tests/abi/, tests/cmake_*/project/, tests/cuda_example/, tests/fmt_formatter/, and tests/module_cpp20/), and it never checked BUILD.bazel at all, so a misformatted file in any of those paths, or a stale BUILD.bazel, could reach develop through a direct push even though the PR-only workflow would have caught it. Make ci_test_amalgamation glob the same roots (docs/mkdocs/docs/examples, include, tests) and extensions (*.hpp, *.cpp, *.cu) as check_amalgamation.yml, excluding tests/thirdparty/ and tests/abi/include/nlohmann/ the same way, and regenerate and diff BUILD.bazel next to json.hpp/json_fwd.hpp. Also add docs/mkdocs/docs/examples/*.hpp to the Makefile's pretty/pretty_format targets, which were missing the four custom_*_type.hpp example headers, and drop the stale "called by Travis" comment on check-amalgamation (Travis is gone; nothing currently calls that Makefile target from CI). Leaves the workflow itself untouched: it deliberately runs amalgamate.py from a fresh develop checkout so a PR cannot change the tool that checks it. Overlaps #5610 and #5621, which each add a new amalgamated header and touch the same INDENT_FILES/ci_test_amalgamation/check_amalgamation.yml hunks. Verified with `make check-amalgamation` on this branch: clean, no diff. #5715 item 5. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Conflicts: - include/nlohmann/json.hpp: took develop's json_literals.hpp include (#5610) without the GCC C++20 -Wignored-attributes pragma pop this branch removes - single_include/nlohmann/json.hpp: regenerated with make amalgamate Also dropped the redundant semicolon from six CAPTURE() call sites that develop added in unit-allocator, unit-class_lexer, and unit-comparison, as this branch removes -Wno-extra-semi-stmt. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
…er (#5735) * Remove dead doctest help entry and pretty_format target from Makefile The top-level Makefile still carried three leftovers: - The help text listed a "doctest" target that was removed in #4560, so "make doctest" fails with "No rule to make target". The example check now runs as "make check_output -C docs". - "pretty_format" ran clang-format on all sources, but .clang-format was deleted in #4573, so the target reformatted everything in the default LLVM style, against the Artistic Style formatting that "make pretty" applies and CI enforces. - "clean" removed benchmarks/files/numbers/*.json, a directory that no longer exists since the benchmarks moved to tests/benchmarks (#3462). Only maintainer tooling changes; the library is not affected. Part of #5717 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Document tools/macro_builder and tidy up serve_header.py tools/macro_builder generates the NLOHMANN_JSON_EXPAND, NLOHMANN_JSON_GET_MACRO and NLOHMANN_JSON_PASTE* macros in macro_scope.hpp, but nothing referred to it. Add a README that explains what it generates, how to run it and where the output goes, and which dependent tables (NLOHMANN_JSON_DOUBLE_PASTE, NLOHMANN_JSON_TYPE_BODY) are maintained by hand. Point to it from a comment above NLOHMANN_JSON_EXPAND. The generator itself is unchanged; following the README reproduces the header byte for byte. In serve_header.py, drop the LGTM suppression (LGTM.com shut down in 2022), replace the """.""" placeholder docstrings with real ones, and import socket and ssl at module level. DualStackServer.server_bind uses socket, which was only imported under __main__; when the module was imported instead, the NameError was swallowed and IPV6_V6ONLY was not cleared. The header change is a comment only; behavior, API and ABI are unchanged. Part of #5717 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Hash every release_files artifact, not a hardcoded subset The `release` target signed and copied json_fwd.hpp into release_files alongside json.hpp, but the shasum line that writes hashes.txt only listed json.hpp, include.zip and json.tar.xz. Users could not verify the published json_fwd.hpp against hashes.txt. Hash every file in release_files except the .asc signatures instead of naming files by hand, so a newly shipped header (such as the json_literals.hpp that #5610 adds to this target) cannot be missed again. Only affects the generated hashes.txt release artifact; the library itself is unaffected. Overlaps #5610, which touches the same lines to add json_literals.hpp to the release target. #5717 item 1 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Remove the broken fuzz_testing* Makefile targets fuzz_testing and fuzz_testing_{bon8,bson,cbor,msgpack,ubjson} seeded fuzz-testing/testcases from tests/data, which was removed in dbf1a1f (2020) when the test data moved to the external json_test_data repo. The find command found nothing, but the pipeline's exit status was that of xargs, so the recipe still reported success with an empty corpus, and the printed afl-fuzz command would refuse to start. The recipes were also six near-identical copies with unquoted -name patterns, used the legacy CXX=afl-clang++, and fuzzing-start/stop were missing from both the help output and .PHONY. tests/fuzzing.md already documents the working flow (download json_test_data, then `make -C tests fuzzers`), so replace the six broken targets and their help lines with a single pointer to that document instead of trying to keep six copies of a fragile shell pipeline in sync. This does not affect OSS-Fuzz, which builds through tests/Makefile. Overlaps #5621, which adds a seventh copy of the same broken line for fuzz_testing_json_view. #5717 item 2 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Remove stale Travis comment above check-amalgamation check-amalgamation carried "Note: this target is called by Travis", left over from before the project switched off Travis CI. The prior Makefile cleanup commit removed the other stale Travis-era leftovers (the doctest help entry, pretty_format, and the benchmarks/ path in clean) but missed this comment. #5717 item 4 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix stale install/usage instructions in the vendored amalgamate README tools/amalgamate/README.md is the unmodified upstream text and no longer matches how the tool is used here: - It named a Bitbucket origin that no longer exists; CHANGES.md already tracks the GitHub mirror commit this copy is based on. - It asked for Python 2.7, but CI and the Makefile run the script with python3. - It told readers to run ./test.sh (not vendored) and install to /usr/local/bin; in this repository the tool runs through `make amalgamate`. - Its usage synopsis showed `-v` taking no argument, but the script's own argparser requires `choices=["yes", "no"]`, so that form fails with "argument -v/--verbose: expected one argument". The Makefile calls it as `--verbose=yes`. - It pointed at test/source.c.json and test/include.h.json, which are not vendored; the configs actually used are config_json.json and config_json_fwd.json. Rewrote only the Installing and Using sections to match; left the "Here be dragons" caveats and the rest of the vendored code untouched to avoid diverging further from upstream. Overlaps #5615, which edits amalgamate.py, this README and CHANGES.md. #5717 item 6 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Derive generate_natvis.py's ABI tag list and version from abi_macros.hpp generate_natvis.py hard-coded abi_tags = ['_diag', '_ldvcmp', '_dp', '_bics', '_psp', '_snul'] and required --version on the command line. The source of truth is include/nlohmann/detail/abi_macros.hpp: the NLOHMANN_JSON_ABI_TAG_* defines, the argument order of NLOHMANN_JSON_ABI_TAGS_CONCAT, and NLOHMANN_JSON_VERSION_MAJOR/MINOR/ PATCH. Nothing checked that the copies stayed in sync, and they have drifted apart before: _dp was added in #4517 but missed here until #5544, and 3.11.3 shipped json_abi_v3_11_2 namespaces (#4340). Parse the tag list (in NLOHMANN_JSON_ABI_TAGS_CONCAT order) and the version from abi_macros.hpp instead of hard-coding them. Make --version optional (falling back to the parsed version) and default the output directory to the repository root the script lives in. Add a "natvis" Makefile target that runs the script, and extend check-amalgamation to regenerate nlohmann_json.natvis and fail on a diff, the same way it already does for the amalgamated headers and BUILD.bazel. Wire the same regeneration into check_amalgamation.yml, using the tool copy checked out from develop (as the workflow already does for amalgamate.py) and installing jinja2 from tools/generate_natvis/requirements.txt. Update the tool's README to say it must be re-run after adding an ABI tag or bumping the version. Verified: a run against develop produces no diff (with either the default or an explicit --version 3.12.0); adding a dummy NLOHMANN_JSON_ABI_TAG_* without a matching #define makes the script fail loudly instead of silently omitting the tag; xmllint --noout passes on the regenerated file; and running the script from a directory other than the one being checked (simulating the workflow's separate tool checkout) against this repository root also produces no diff. Overlaps #5600, which added _ekmo to the same hand-written abi_tags line and regenerated the file. #5717 item 3 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Strip the leading "./" find(1) prefix from release hashes.txt entries bc6e7db (#5717 item 1) switched the release target's shasum line from naming files by hand to $$(find . -type f -not -name '*.asc' | sort), so a newly shipped header is hashed automatically. Run from inside release_files, that find prints paths as "./json.hpp" instead of "json.hpp", so hashes.txt lists "./json.hpp" etc. instead of the plain filenames it always used. shasum -c still verifies "./json.hpp" fine, but it is a needless cosmetic regression for anyone reading the file or matching it against release notes. Strip the "./" prefix with sed before sorting, keeping the filenames exactly as before while still hashing every artifact automatically. Review fix for #5717 item 1 (PR #5735). Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix check_amalgamation.yml: pass --version to generate_natvis.py bff45f1 (#5717 item 3) made --version optional in tools/generate_natvis/generate_natvis.py and wired the workflow's new "Regenerate nlohmann_json.natvis" step to call it without --version, relying on the script deriving the version from abi_macros.hpp itself. But NATVIS_TOOL_DIR is checked out from develop, the same way TOOL_DIR already is for amalgamate.py, precisely so an in-flight PR's tooling changes cannot mark themselves clean. Until this PR (or an equivalent) merges to develop, that checkout is the old generate_natvis.py, whose --version argument is still required=True. The new step's invocation of "generate_natvis.py $MAIN_DIR" (no --version) then fails argparse on this PR's own CI run with "the following arguments are required: --version", before the check ever gets to compare output. Extract the version from $MAIN_DIR's own abi_macros.hpp in the workflow and always pass it as --version. That satisfies the old script's required argument and is accepted as an explicit override by the new one, so the step behaves the same whether NATVIS_TOOL_DIR holds the pre- or post-merge tool, and stays correct for later PRs that bump the version. Verified by running the workflow step's shell logic locally against both the pre-#5717 generate_natvis.py (checked out at 633de8e) and the new one: both produce the identical nlohmann_json.natvis as the committed file. Review fix for #5717 item 3 (PR #5735). Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Extend tools/macro_builder to also generate DOUBLE_PASTE and TYPE_BODY tools/macro_builder only emitted NLOHMANN_JSON_EXPAND..PASTE64, so two other tables that scale with the same max_args stayed hand-maintained with nothing checking them: NLOHMANN_JSON_DOUBLE_PASTE (added by hand in #4563 for the *_WITH_NAMES macros) and the 64-slot dispatch table of NLOHMANN_JSON_TYPE_BODY (the #4041 zero-member/one-or-more-member switch). Both tables pass one macro name per slot to the same NLOHMANN_JSON_GET_MACRO dispatch as PASTE, so they can drift out of sync with max_args exactly the way _dp did in the ABI tag list fixed by #5544. Extend main.cpp with build_double_paste_code() (same recursive-doubling shape as build_paste_code(), but DOUBLE_PASTE consumes two arguments per member, so an even slot index falls back to the next lower odd DOUBLE_PASTE<N>) and build_type_body_table() (max_args - 1 MEMBERS slots and one trailing EMPTY slot, 8 per line, matching how it is written by hand today). Add a "type_body" argument that selects the TYPE_BODY block, since it lives at a separate location in macro_scope.hpp from the EXPAND..DOUBLE_PASTE63 block; plain invocation is unchanged apart from covering the extended range. No longer emit the tool's old trailing blank line, so its output is directly diffable without post-processing. Verified with c++ -std=c++11: running the tool (with and without "type_body") and piping the raw output through the pinned astyle reproduces both blocks of the current macro_scope.hpp byte for byte. tests/src/unit-udt_macro.cpp (all NLOHMANN_DEFINE_TYPE_*/_WITH_NAMES/ zero-member variants) passes unchanged under -std=c++11 and -std=c++17 with -fsanitize=address,undefined. Add a "macro_builder_check" Makefile target that builds main.cpp, regenerates both blocks into a scratch directory inside the repository (astyle's --project lookup needs the target files under the same tree as .astylerc, unlike an external /tmp directory), and diffs them against the corresponding ranges of macro_scope.hpp; wire it into check-amalgamation next to the natvis check. Wire the same regeneration into check_amalgamation.yml, splicing the (still unindented) generated blocks back into the PR's own macro_scope.hpp before the existing astyle/amalgamation step runs, so that step's own tree-wide astyle pass both indents them and folds any drift into the amalgamation patch/diff the workflow already produces. Unlike amalgamate.py and generate_natvis.py, this step builds tools/macro_builder/main.cpp from the pull request's own checkout ($MAIN_DIR) rather than a separate checkout of tools/ at develop: this tool has no independent source of truth to regenerate against (its README documents that it must reproduce macro_scope.hpp byte for byte), so a develop-pinned copy would only reproduce the generate_natvis.py trap fixed in a previous commit on this branch, where a PR that teaches the tool to cover more of the file fails its own CI until that PR merges and updates the develop copy. Add tools/macro_builder/README.md documentation for both new tables and the two-invocation usage, and a short pointer comment above NLOHMANN_JSON_TYPE_BODY (the EXPAND pointer already covered the first block; extended its wording to include DOUBLE_PASTE63). Closes #5717 item 5 in full, completing what the documentation-only "Document tools/macro_builder..." commit already on this branch left open (that commit's README/pointer-comment half stands; it also covers item 7). Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
) * Fix the ci_cmake_flags wiring so every option is checked The CMake 3.31.6 flag list referred to itself before it was defined, so only JSON_BuildTests was checked with that version. The targets for the CMake running the build ("_2") were created but never added to ci_cmake_flags, and the three versions shared one build directory. JSON_StrictNulHandling was not in the list at all. Use the 3.5.0 list for 3.31.6, add JSON_StrictNulHandling, and create one ci_cmake_flag_<flag> target per option for the running CMake with its own build directory. Also use the function parameter in the COMMENT, refresh the stale version comment, and let ci_clean remove the downloaded cmake-<version> directories instead of the long-gone cmake-3.5.0-Darwin64. Part of #5715 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix ci_test_clang_libcxx_cxx* jobs silently building without warnings CMake only seeds CMAKE_CXX_FLAGS from the CXXFLAGS environment variable when the cache entry is unset, so the explicit -DCMAKE_CXX_FLAGS="-stdlib=libc++" argument made it ignore CXXFLAGS="${CLANG_CXXFLAGS}" entirely. The six ci_test_standards_clang (..., libcxx) jobs therefore compiled without -Weverything/-Werror while their libstdc++ siblings did use them. Pass -stdlib=libc++ through the same CXXFLAGS value instead of a separate -D argument, and give the target its own build directory (build_clang_libcxx_cxx${CXX_STANDARD}) so it no longer shares a CMake cache with the libstdc++ variant. Suppress the resulting -Wthread-safety-negative finding from libc++'s std::mutex annotations, which fires on doctest's reporters in this translation unit only. Part of #5715 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Remove the no-op AppVeyor with_win_header job The with_win_header matrix entry patched Windows.h into single_include/nlohmann/json.hpp before building, but JSON_MultipleHeaders has defaulted to ON since #3532 (2022-06), so CMakeLists.txt points the tests at include/ and the patched single header is never compiled. The job has been a no-op VS2015 build since then. Windows.h coverage already exists through tests/src/unit-windows_h.cpp (#3631), which runs in every MSVC job. Delete the dead matrix entry and its before_build steps, and cite unit-windows_h.cpp from the QA page. Part of #5715 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Remove unused ci_oclint and ci_pvs_studio targets No workflow invokes ci_oclint, ci_pvs_studio, or their tool discovery. ci_oclint also had a side effect on every JSON_CI configure: it copied the single header into src_single/all.cpp and added an add_executable() for it without EXCLUDE_FROM_ALL, so a plain build compiled a 1.2 MB translation unit that only that unused target consumed. ci_pvs_studio duplicates the Makefile's pvs_studio target, which is kept. Also drop the duplicate --check-level=exhaustive flag passed twice to the same ci_cppcheck invocation. Part of #5715 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Stop Dependabot from proposing astyle bumps astyle is deliberately pinned at 3.4.13 because newer versions reformat unrelated lines and this version defines the formatting that check_amalgamation.yml enforces. Without an ignore rule, Dependabot keeps opening PRs for every new astyle release (most recently #4580, #4942, #5445, #5448), each of which fails the amalgamation check and gets closed unmerged. Part of #5715 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Move Linux arm64 CI from dead Cirrus CI to ubuntu-24.04-arm Cirrus CI stopped reporting check runs on develop sometime after d10879b (2026-05-26); every commit since has only github-actions check runs, so .cirrus.yml silently lost its only consumer while README.md, FILES.md and the QA page kept advertising the coverage. Add a ci_test_arm64 job to ubuntu.yml using the same pinned actions/checkout and lukka/get-cmake actions as the other jobs, on the native ubuntu-24.04-arm runner, with a step that confirms uname -m reports aarch64. Delete .cirrus.yml and its README badge and FILES.md section, and update the QA page's arm64 row. Part of #5715 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Make scan-build fail on findings and drop irrelevant checkers (#5715 item 4a) ci_clang_analyze ran scan-build without --status-bugs, so the job passed whenever the ninja build succeeded, no matter what the analyzer found ("No bugs found" in a green run gave no signal either way). It is also missing --use-analyzer=${CLANG_TOOL}, so scan-build picks whichever clang happens to be first on PATH inside the silkeh/clang:dev container instead of the one this file already selected and versioned. Add --status-bugs and --use-analyzer=${CLANG_TOOL} to the scan-build invocation. While here, drop the osx.*, webkit.*, fuchsia.*, and optin.mpi.* checkers from CLANG_ANALYZER_CHECKS: none of them apply to this portable C++ library, and leaving them enabled only adds noise once the job can actually fail on a finding. The job is currently clean (0 bugs), so this alone does not surface any new finding; it only makes the existing "no bugs found" result authoritative. This is 4a of 3 independent steps in #5715 item 4; 4b (Infer) and 4c (IWYU) still need their existing findings triaged before --fail-on-issue/-Xiwyu --error can be added, and are handled in separate commits. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Deduplicate the amalgamation/format check's file set and add BUILD.bazel (#5715 item 5) The amalgamation/format check existed three times with three different file sets: the Makefile's pretty/check-amalgamation, the pull_request-only check_amalgamation.yml workflow, and the ci_test_amalgamation CMake target that also runs on direct pushes to develop/master/release/*. The CMake target's glob was a strict subset of the workflow's (missing the docs/mkdocs/docs/examples/*.hpp headers, tests/abi/, tests/cmake_*/project/, tests/cuda_example/, tests/fmt_formatter/, and tests/module_cpp20/), and it never checked BUILD.bazel at all, so a misformatted file in any of those paths, or a stale BUILD.bazel, could reach develop through a direct push even though the PR-only workflow would have caught it. Make ci_test_amalgamation glob the same roots (docs/mkdocs/docs/examples, include, tests) and extensions (*.hpp, *.cpp, *.cu) as check_amalgamation.yml, excluding tests/thirdparty/ and tests/abi/include/nlohmann/ the same way, and regenerate and diff BUILD.bazel next to json.hpp/json_fwd.hpp. Also add docs/mkdocs/docs/examples/*.hpp to the Makefile's pretty/pretty_format targets, which were missing the four custom_*_type.hpp example headers, and drop the stale "called by Travis" comment on check-amalgamation (Travis is gone; nothing currently calls that Makefile target from CI). Leaves the workflow itself untouched: it deliberately runs amalgamate.py from a fresh develop checkout so a PR cannot change the tool that checks it. Overlaps #5610 and #5621, which each add a new amalgamated header and touch the same INDENT_FILES/ci_test_amalgamation/check_amalgamation.yml hunks. Verified with `make check-amalgamation` on this branch: clean, no diff. #5715 item 5. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Download prebuilt CMake binaries on Linux x86_64 instead of building from source (#5715 item 6) ci_get_cmake() downloaded the source tarball of CMake 3.5.0, 3.31.6, and 4.0.0 and compiled each one completely (including CMake's own test helpers) with -DCMAKE_POLICY_VERSION_MINIMUM=3.5 as a workaround for building old CMake with a newer one. On CI this made the ci_cmake_options (ci_cmake_flags) job take about 11 minutes, most of it spent building CMake itself, even though Kitware has published ready-to-run Linux x86_64 archives for all three of these releases since 3.20 (lowercase platform name). On Linux x86_64, download and unpack the prebuilt cmake-<version>-linux-x86_64.tar.gz archive instead and point the existing ${var} output at its bin/cmake, skipping the configure/build steps and CMAKE_POLICY_VERSION_MINIMUM entirely. Keep the previous source build as a fallback for any other platform (macOS, Linux aarch64), since Kitware does not publish binaries for every CMake/platform combination this project might build on. Verify the downloaded archive against Kitware's own published checksum before unpacking it: download cmake-<version>-SHA-256.txt alongside the archive and run `sha256sum -c` on the matching line. A CI job that wgets and untars a binary from a release page with no integrity check is a supply-chain gap; Kitware has published this file for every release since 3.20, so checking it costs one extra download and one grep. As a separate, mechanical change: the ci_cmake_options job's container only needed to stay on ubuntu:focal for the source build's libssl-dev dependency and its own aging toolchain; now that the Linux/x86_64 path never compiles CMake, drop libssl-dev from its apt install line and move the job to ubuntu:24.04 (Ubuntu 20.04 left standard support in May 2025). ci_clean already removes the cmake-3.5.0/cmake-3.31.6/cmake-4.0.0 directories from the #5715 item 3 fix, and the prebuilt path reuses those same directory names, so no further cleanup changes are needed. Overlaps #5598, which edits the same ci_cmake_options matrix line in ubuntu.yml; a rebase may be needed once that lands. Verified locally: `cmake -S . -B build -DJSON_CI=On` configures cleanly on macOS/arm64 (source-build fallback branch) and on Linux/x86_64 in an ubuntu:24.04 Docker container (47 `ci_cmake_flag_*` targets generated, one built and run successfully); `.github/workflows/ubuntu.yml` still parses as valid YAML; downloaded the real v3.31.6 Linux x86_64 archive and SHA-256 file from Kitware and confirmed the `grep | sha256sum -c` pipeline both accepts the genuine file and is anchored to the exact filename (not a prefix match). CI must confirm: the prebuilt-binary path actually runs on the ubuntu-latest/ubuntu:24.04 x86_64 runner, all `ci_cmake_options` entries still pass with the new container's GCC, and the job's runtime drops from roughly 11 minutes. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Make Infer fail on findings, with a type-level baseline for the ~174 pre-existing ones (#5715 item 4b) ci_infer ran `infer run` without --fail-on-issue, so the job passed regardless of what Pulse found; the last recorded run (35829411620, commit 1054b20) logged "Found 174 issues" and still went green. report.txt was also never uploaded, so the full finding list was only ever visible in the truncated 5-issue console excerpt. Add a repository-root .inferconfig (auto-discovered by Infer; passing --project-root on the `infer run` invocation makes sure it is found even though the analysis runs from build/build_infer) that sets fail-on-issue and disables the six PULSE issue types that made up all 174 findings in that run: PULSE_UNNECESSARY_COPY_ASSIGNMENT (129), PULSE_UNNECESSARY_COPY (22), PULSE_UNNECESSARY_COPY_INTERMEDIATE (15), PULSE_RESOURCE_LEAK (5), PULSE_CONST_REFABLE (2), and PULSE_UNNECESSARY_COPY_OPTIONAL (1). This is a deliberate, narrower fix than "triage and fix everything in this PR": the visible sample is entirely doctest-macro copies in test code (for example tests/src/unit-algorithms.cpp:141 and tests/src/unit-bjdata.cpp:3706), but 169 of the 174 findings were never uploaded anywhere and this PR cannot respectably claim to have fixed issues it never saw, including the resource-leak and const-refable ones that are the most likely to be genuine bugs. Disabling by issue type is a coarser baseline than a per-finding one (Infer has no built-in per-finding baseline short of the two-run `infer reportdiff` workflow, which this repository does not have the CI infrastructure for), but it has the same effect today: the job goes from always green to green-only-when-clean-of-everything-else, so CI now fails the moment a *new* issue type appears, and report.txt is uploaded as a workflow artifact on every run (including failures) so the six disabled types can be triaged and re-enabled incrementally in follow-up PRs. #5715 item 4b. 4a (scan-build) and 4c (IWYU) are handled in separate commits. Verified: .inferconfig parses as JSON, ubuntu.yml still parses as YAML. Infer itself is not available in this environment (v1.3.0 tar.xz requires a Linux x86_64 runner), so CI must confirm that `infer run --project-root ... -- make` picks up .inferconfig, that fail-on-issue takes effect, and that the six disabled types actually suppress the existing findings without also hiding an unrelated new one. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix IWYU findings for json.hpp/json_fwd.hpp/ordered_map.hpp and make CI fail on new ones (#5715 item 4c) ci_single_binaries ran IWYU via CMake's CXX_INCLUDE_WHAT_YOU_USE launcher property, which only printed "Warning: include-what-you-use reported diagnostics" without failing the build: CMake's own __run_co_compile wrapper does not propagate the launched tool's exit code, so even `-Xiwyu --error` could never fail `cmake --build` this way. Verified this empirically by injecting a deliberately-unused #include and confirming the build still exited 0. Fix the findings from the last recorded run (issue #5715 item 4, log 35829411620): - ordered_map.hpp: add <new> (placement new) and nlohmann/detail/abi_macros.hpp; drop <memory> (std::allocator is still visible transitively via <vector>, confirmed by full local and containerized test suite runs). - json_fwd.hpp: drop <memory> (same reasoning). Keep every forward declaration IWYU wanted removed (adl_serializer, basic_json, json_pointer, ordered_map): this file's only job is to forward-declare them for downstream users, so "nothing in this TU uses them" is expected, not a real finding. Mark each with `// IWYU pragma: keep`. - json.hpp: add <cmath>, <cstdint>, <set>, <type_traits>, <unordered_map>, and the detail/abi_macros.hpp, detail/input/json_sax.hpp, detail/meta/detected.hpp, thirdparty/hedley/hedley.hpp includes IWYU says it needs. Do NOT remove adl_serializer.hpp, detail/conversions/from_json.hpp, detail/conversions/to_json.hpp, detail/macro_unscope.hpp, or ordered_map.hpp as IWYU suggests: nothing else in include/nlohmann includes adl_serializer.hpp or ordered_map.hpp, so basic_json<>'s own default template arguments (JSONSerializer = adl_serializer, and ordered_json = basic_json<ordered_map>) would lose their complete type; detail/macro_unscope.hpp is what undoes the JSON_* macros detail/macro_scope.hpp defines earlier in this same file, and removing it leaks those macros into every translation unit that includes <nlohmann/json.hpp>. Verified by actually removing them in a scratch test: the header still "compiles" stand-alone but ordered_json and every macro-using translation unit break. Marked each `// IWYU pragma: keep`. Enforce it with `iwyu_tool` (ships with IWYU, e.g. as /usr/bin/iwyu_tool on Debian/Ubuntu) instead of relying on the launcher property: it reads compile_commands.json (now exported project-wide under JSON_CI) and does return a real exit code for its own analysis, independent of CMake's wrapper. ci_single_binaries now runs it over every src_single/*.cpp with `-Xiwyu --error`, so a *new* finding fails CI. json.hpp itself is excluded from that hard gate: even after every fix above, IWYU's suggestion for one remaining symbol (a container `swap, operator!=` used somewhere via a templated comparator) is not deterministic — repeated, otherwise-identical containerized runs reported <set>, then <unordered_map>, then <map> as "the" header to add/remove for the exact same source. Gating a whole CI job on a nondeterministic suggestion would make ci_single_binaries flaky rather than informative, so json.hpp keeps the existing informational warning (still shown during its normal compile) without failing the build on it. Every other one of the ~50 single-header checks is included in the hard gate. #5715 item 4c. 4a (scan-build) and 4b (Infer) are separate commits. Verified: full local ctest suite (129/129) and the ci_single_binaries target itself both green in a containerized silkeh/clang:dev run (matching the actual CI job) after this fix; a deliberately-reintroduced unused #include in ordered_map.hpp was confirmed to fail `cmake --build ... --target ci_single_binaries` (exit 2) with this change, and to pass without it, on the same container/IWYU version CI uses. `make check-amalgamation` is clean. Compiled with Clang and GCC at -std=c++11/14/17/20 locally with no new warnings. Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
…5737) * Drop stale LCOV_EXCL_LINE from the json_pointer out_of_range.410 throw The comment said the size_type overflow check in array_index() is only triggered on special platforms like 32-bit, and the throw was excluded from coverage. On 64-bit platforms the check is true for SIZE_MAX itself, and unit-json_pointer.cpp has asserted that case four times since #5395, so the line is executed in the coverage job. Reword the comment and remove the exclusion marker so the coverage report notices if the tests stop reaching it. Part of #5725 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Name all three C-array check aliases in the enum-macro NOLINTs NLOHMANN_JSON_SERIALIZE_ENUM(_STRICT) suppressed the c-array warning under modernize-avoid-c-arrays only, but clang-tidy emits the same diagnostic under the aliases cppcoreguidelines-avoid-c-arrays and hicpp-avoid-c-arrays too. Any user running those checks got a false positive at every macro expansion, and our own tests needed a local NOLINT at each call site to work around it. Name all three aliases in the four macro comments instead, and drop the now-redundant c-array names from the five test call-site NOLINTs. Comment-only change; behavior, the public API, and the ABI do not change. Ran make amalgamate. Part of #5725 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Include doctest as a SYSTEM directory instead of disabling warnings for all tests test_main added -Wno-deprecated and -Wno-float-equal as PUBLIC compile options for every non-MSVC compiler, so they were applied to every translation unit, library headers included, and silenced the CI warnings meant to check the library's own -Wfloat-equal pragmas. The only code that actually needed the suppression was the vendored doctest.h, which was included as a normal (non-SYSTEM) directory. Include thirdparty/doctest as SYSTEM for test_main, matching what tests/abi/CMakeLists.txt already does, and drop the two suppressions from both targets. Verified locally that unit-comparison, unit-conversions and unit-constructor1 compile clean with -Werror -Weverything and doctest as -isystem, and that CMake still configures with JSON_BuildTests=ON. Part of #5725 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Remove the no-op ci_clang_analyze target ci_clang_analyze configured the build with the real compiler and only then wrapped ninja with scan-build. scan-build intercepts compiles by overriding CC/CXX, but build.ninja already had the compiler path baked in from the configure step, so every run bypassed the analyzer: CI logs show "No bugs found" after a normal build, never an analysis. The job also used Debian's frozen clang-tools-14 rather than the image's own clang, and CLANG_ANALYZER_CHECKS still named three valist.* checkers that current clang merged into security.VAList. ci_clang_tidy already runs every clang-analyzer-* check (via .clang-tidy's "Checks: '*'") with warnings as errors, so nothing is lost by removing the dead job. Delete ci_clang_analyze, CLANG_ANALYZER_CHECKS and the SCAN_BUILD_TOOL lookup from cmake/ci.cmake, drop it from the ubuntu.yml ci_static_analysis_clang matrix, and drop the now-unused clang-tools apt package (iwyu stays for ci_single_binaries). Reword quality_assurance.md and assurance_case.md, which described the dead job as a working control, to say the Clang Static Analyzer checks run through clang-tidy. Verified that `cmake -DJSON_CI=ON` still configures cleanly and that ci_clang_analyze no longer appears in the generated build or in any CMake/workflow file. Part of #5725 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Re-enable portability-template-virtual-member-function; remove redundant forwards .clang-tidy disabled three checks "to get the CI going" (#4489, 2024-11-13): portability-template-virtual-member-function, bugprone-use-after-move and its alias hicpp-invalid-access-moved. portability-template-virtual-member-function only flagged output_stream_adapter::write_character/write_characters; annotate both with NOLINT and re-enable the check. bugprone-use-after-move flagged several double forwards that have no effect at runtime: - from_json.hpp calls std::forward<BasicJsonType>(j).at(Idx) inside pack expansions; at() has no ref-qualified overloads and always returns an lvalue reference, so the forward is a no-op. Replace with plain j.at(Idx) in all four places. - the move constructor forwards the whole object to its base class and then reads other's members. That is item 9 of #5724 (together with its cppcheck suppressions) and is left to that change. - input_adapters.hpp forwards the container twice on purpose, so the begin/end iterator types match adapter_type; annotate with NOLINT and a comment instead of changing behavior. The check still flags the move constructor (see above) and two sites in at(KeyType&&) (both overloads, json.hpp, in the throw's string_t(std::forward<KeyType>(key)) after find(std::forward<KeyType>(key))). Open PR #5689 rewrites that hunk, so bugprone-use-after-move (and hicpp-invalid-access-moved) stay disabled for now, with a comment explaining why; re-enable them once #5689 and the #5724 move-constructor change have landed. Also resolve the portability-avoid-pragma-once TODO: single_include never has #pragma once (amalgamate.py strips it) and every supported compiler accepts it in include/, so keep it disabled with an explanatory comment instead of a TODO. Fix the stale "json.hpp, around line 1265" comment in unit-class_parser.cpp, which now points at the move constructor's actual line. Behavior, the public API and the ABI do not change. Verified with clang-tidy 22.1.8 that portability-template-virtual-member-function now reports nothing, that bugprone-use-after-move/ hicpp-invalid-access-moved report only the known at(KeyType&&) and move-constructor sites, and that unit-custom-base-class, unit-constructor1, unit-conversions, unit-element_access2, unit-class_parser and unit-diagnostic-positions (JSON_DIAGNOSTIC_POSITIONS=1) compile under ASan/UBSan and pass with the same assertion counts as before. Ran make amalgamate. Part of #5725 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix stale and malformed NOLINT comments json_sax.hpp named "-warnings-as-errors" in the NOLINT list on the two JSON_ASSERT(false) lines; that is the suffix clang-tidy appends to a diagnostic tag under WarningsAsErrors, not a check name, and every other JSON_ASSERT(false) omits it. unit-capacity.cpp carried 30 "// NOLINT(misc-const-correctness)" comments on "json j = ...;" declarations that are all used with non-const members afterwards, so the check has nothing to report there. unit-constructor2.cpp used a blanket "// NOLINT: access after move is OK here" on a use-after-move that hides every check on the line; naming bugprone-use-after-move and hicpp-invalid-access-moved keeps the intent once those checks are re-enabled (#5724). Signed-off-by: Niels Lohmann <mail@nlohmann.me> #5725 item 10 * Remove stale .clang-tidy entries -google-runtime-references disabled a check that neither clang-tidy 22.1.8 nor 23.1.2 lists under --list-checks -checks='*'; it was removed upstream. The commented-out HeaderFilterRegex line has been unused since the active HeaderFilterRegex was introduced in #2561 (2021). Signed-off-by: Niels Lohmann <mail@nlohmann.me> #5725 item 11 * Remove the GCC C++20 -Wignored-attributes pragma in json.hpp The pragma (added in #5164) claimed to work around the C++ modules redefinition errors of #5103, but #5103 is about hard errors (e.g. "redefinition of std::__is_constant_evaluated()", conflicting std::integral_constant) that ignoring a warning cannot suppress; they are traced to GCC PR 124430 and reproduce with <map> or <string> instead of json.hpp too. A GCC 16.2 -std=gnu++20 -fmodules build following #5103's repro steps still fails with the pragma in place, and a build of all test TUs with GCC_CXXFLAGS (which enable -Wignored-attributes) and the pragma removed produces no such warning. The block only hid a warning class from GCC C++20 users while suggesting #5103 was handled. Overlaps #5610, whose hunks touch the closing half of this pragma to insert the json_literals.hpp include. Signed-off-by: Niels Lohmann <mail@nlohmann.me> #5725 item 9 * Fix stale doxygen comments hidden by the -Wdocumentation pragma macro_scope.hpp ignores -Wdocumentation and -Wdocumentation-unknown-command for the whole library, which also hides genuine documentation mistakes: - detail::unescape() documented "@return unescaped string" but returns void and unescapes its argument in place; reworded to "@PARAM[in,out] s string to unescape in place" and dropped the bogus @return. - basic_json::get()'s copy-conversion overload wrote "converted to @tparam ValueType" inside @return, which Doxygen and Clang parse as a second, malformed @tparam; changed to "@A ValueType", matching the two other get() overloads a few lines above that already use it. This narrows the gap the -Wdocumentation pragma needs to cover; fully replacing the Doxygen-only commands it also hides (item 2c) is left for after #5267. Signed-off-by: Niels Lohmann <mail@nlohmann.me> #5725 item 2 * Fix -Wextra-semi-stmt at its actual source, not assert() clang_flags.cmake blamed the global -Wno-extra-semi-stmt on assert(), but assert() expands to an expression under glibc and libc++ and does not trigger this warning. unit-assert_macro.cpp overrides JSON_ASSERT with "{if (!(x)) ++assert_counter; }", a bare block followed by a semicolon at every JSON_ASSERT(...) call site in the library; that was the actual source of 151 of the 208 -Wextra-semi-stmt sites found in a Clang 22 -Weverything sweep of the test suite with the flag removed. Switched to the standard do/while(false) macro idiom, which does not expand to a statement-plus-semicolon, and corrected the comment to name the remaining source instead: vendored Doctest's CAPTURE(x) shim, which already ends in a semicolon. Verified with clang++ -Wextra-semi-stmt (plus the file's other CI ignores) that unit-assert_macro.cpp now compiles without any -Wextra-semi-stmt diagnostic. Signed-off-by: Niels Lohmann <mail@nlohmann.me> #5725 item 8 (step 1 of 2; step 2 covers the CAPTURE() call sites) * Drop the redundant semicolon from CAPTURE() call sites; remove -Wno-extra-semi-stmt doctest_compatibility.h defines CAPTURE(x) as DOCTEST_CAPTURE(x); (with a trailing semicolon baked into the macro), specifically so call sites do not need to add one themselves; most of the ~267 call sites already follow that convention. The remaining 64 call sites across 20 files wrote "CAPTURE(x);" anyway, turning into a statement plus an empty statement and triggering -Wextra-semi-stmt. Dropped the redundant semicolon at each of those sites. With item 6 having already made vendored Doctest a SYSTEM include, and this the last known source of -Wextra-semi-stmt findings, removed the flag from clang_flags.cmake entirely. Verified with clang++ -Wextra-semi-stmt (plus the file's other CI ignores) that all 20 touched files, plus a file with no CAPTURE() use (unit-json_pointer.cpp), compile without any -Wextra-semi-stmt diagnostic. Signed-off-by: Niels Lohmann <mail@nlohmann.me> #5725 item 8 (step 2 of 2) * Switch ci_static_analysis_clang off the frozen LLVM 22 dev image ubuntu.yml pinned the clang-tidy/clang-tidy-sanitizer/single-binaries job to silkeh/clang:dev, a tag last pushed 2026-02-18 that reports "clang version 22.0.0 (...+20251015...)", a pre-release snapshot from before the LLVM 22 release; the maintainer now updates dev-unstable, 22, and latest instead. Switched to silkeh/clang:22, matching the other clang jobs on :latest. Verified with clang-tidy 22.1.8 (the image's actual version) against this repository's .clang-tidy and library headers what the release image newly reports compared to :dev: - readability-redundant-typename fires at ~250 sites across the _cpp20-relevant conversion/to_chars headers; the library targets C++11 and keeps the typenames, so the check is disabled in .clang-tidy, matching how the file already handles checks that don't fit a C++11 codebase. - misc-anonymous-namespace-in-header fires on the two anonymous namespaces in from_json.hpp and to_json.hpp; added the alias to their existing NOLINT (cert-dcl59-cpp, fuchsia-header-anon-namespaces, google-build-namespaces). - bugprone-std-namespace-modification fires on every addition to namespace std: the std::hash, std::formatter and std::swap overloads in json.hpp, and the std::tuple_size/std::tuple_element specializations in iteration_proxy.hpp (this last file is not named in #5725's item 5, found by actually running clang-tidy 22.1.8 against the current tree). All six are legal, deliberate additions to namespace std (explicit/partial specializations of std types, or the pre-C++20 std::swap overload); annotated each with the check name next to its existing cert-dcl58-cpp NOLINT. - modernize-avoid-c-style-cast reported nothing new. Also added clang++-22/21, clang-tidy-22/21, g++-16 and gcov-16 to the find_program search lists in ci.cmake so a local "maximal warnings" configure prefers the current toolchain version over an older one on PATH. #5725 item 5 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Regenerate cmake/gcc_flags.cmake for GCC 16.2.0 GCC_CXXFLAGS was generated for GCC 15.1.0, but ci_test_gcc and ci_test_gcc_cxx{11..26} now run in gcc:latest, currently GCC 16.2.0, so the "maximal warnings" job was missing warnings introduced since 15.1.0 while carrying entries GCC 16 treats as duplicates or no-ops. Regenerated with https://github.com/nlohmann/gcc_flags (patched locally to not crash on an option whose "-x c++ <opt> -" probe fails before it reads stdin, e.g. -Wabi=; the tool otherwise raises BrokenPipeError instead of recording the option as an error) run against g++ 16.2.0 in the official gcc:16 Docker image, keeping the documented -Wno-* exclusions and the same alphabetical placement scheme as before. Also added three GCC 16 warnings the generator cannot discover on its own because it only probes value ranges/lists it finds in the -Q option name itself, not in the enum choices --help=warnings documents separately: - -Wbidi-chars=any, -Wleading-whitespace=spaces: manually verified these compile cleanly with g++ 16.2.0. - -Wstrict-flex-arrays: deliberately NOT added, unlike the other two. Without -fstrict-flex-arrays (which the library does not enable, as it would change codegen for flexible array members), GCC prints "'-Wstrict-flex-arrays' is ignored when '-fstrict-flex-arrays' is not present" on every translation unit, and under our -Werror that note itself aborts the build. This differs from the harmless no-op warnings already kept in the file (-Whsa, -Wsynth, -Wunreachable-code, -Wunsafe-loop-optimizations), which emit nothing; #5725 item 7 named -Wstrict-flex-arrays as one of the flags GCC 16 adds, but did not anticipate this failure mode. Verified: compiled the library header and a representative set of test translation units (including ones touched by items 1, 3, 8, 9, 10 of this issue) with the regenerated GCC_CXXFLAGS plus -Werror under g++ 16.2.0 at -std=c++11 through -std=c++26, with zero warnings; ran the full local test suite (129/129 passing, unrelated to this compiler) as a regression check. CI must still confirm the actual ci_test_gcc / ci_test_standards_gcc targets end to end, since this was verified with direct g++ invocations rather than through the CMake/ CXXFLAGS environment-variable plumbing in ci.cmake. #5725 item 7 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Avoid std::basic_string<CharType> for non-character output_adapter CharType output_adapter<CharType, StringType> defaulted StringType to std::basic_string<CharType>, and (with JSON_NO_IO undefined) always declared a std::basic_ostream<CharType>&-taking constructor. For CharType with no non-deprecated std::char_traits specialization (only std::uint8_t is ever used this way, by the binary writers), simply naming either type - as an unused default template argument, or as an unused, never-called constructor's parameter type - instantiates std::char_traits<CharType> merely to name it, which some standard libraries mark deprecated: with the library-wide -Wdocumentation pragma (item 2's other half, left for a later commit) temporarily removed, an Apple clang 21 / libc++ TU calling json::to_cbor(j, vec) with std::vector<std::uint8_t>& got one -Wdeprecated-declarations warning per binary writer at the old output_adapters.hpp:193. Replaced the eager std::basic_string<CharType> / std::basic_ostream <CharType> defaults with a bool-tagged partial specialization (not std::conditional, which requires naming both branches' types up front regardless of which is selected, reproducing the same warning) that only ever names std::basic_string<CharType> / std::basic_ostream <CharType> when CharType is actually one of char, wchar_t, char16_t, char32_t, or (with __cpp_lib_char8_t) char8_t. For any other CharType, output_adapter's StringType and ostream-constructor parameter fall back to two distinct empty placeholder types, kept distinct so the two constructor overloads do not collide into a single redeclaration. Public API / behavior: passing a std::basic_string<std::uint8_t>& or std::basic_ostream<std::uint8_t>& directly to a binary writer's output_adapter now fails to compile instead of compiling with a deprecation warning; this was neither documented nor tested. All documented uses (std::vector<CharType>, std::basic_ostream<CharType> and StringType for character CharType) are unaffected. Verified with Apple clang 21 / libc++, with the two -Wdocumentation* "ignored" pragma lines in macro_scope.hpp temporarily removed and -std=c++11/c++20 plus the project's -Weverything flag set: calling to_cbor/to_msgpack/to_ubjson/to_bjdata/to_bson/to_bon8 on a std::vector<std::uint8_t> now produces no char_traits<unsigned char> (or any other) deprecation warning, while the char-based string- and ostream-adapter paths, and a to_cbor/from_cbor round trip, still compile and run correctly; also verified with GCC 16.2.0. Ran the full local test suite, including the binary-format unit tests (unit-cbor, unit-msgpack, unit-ubjson, unit-bjdata, unit-bson, unit-bon8, unit-binary_writer_sinks, unit-binary_formats, unit-custom-binary-type): 129/129 passing. #5725 item 2 (step a) Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Remove the library-wide -Wdocumentation pragma; fix what it hid macro_scope.hpp / macro_unscope.hpp pushed and popped a Clang diagnostic region over the entire library that ignored -Wdocumentation and -Wdocumentation-unknown-command. Removed both pragmas and fixed every finding a full -Wdocumentation (which implies -Wdocumentation-unknown-command and -Wdocumentation-deprecated-sync) build reports, so the library now compiles clean under Clang's documentation checks without a blanket suppression. Overlaps #5267, which is still open and edits a nearby doc block (json.hpp's get()/get_impl() @return, already fixed in the item 2 step (b) commit of this branch); this commit does not touch that block again. Unknown Doxygen alias commands (Doxyfile removed in #3071, so these were never rendered by anything) rewritten as plain prose, keeping the same information: - @requirement REQ-JSON-01 / REQ-JSON-02 (iter_impl.hpp, json_reverse_iterator.hpp): now "This class satisfies the following concept requirements (REQ-JSON-0N):". - @liveexample{prose,example-id} (three sites in json.hpp): kept the prose, dropped the command wrapper and the trailing example-id (docs/mkdocs/docs/examples/*.cpp still exist and are used directly by the rendered docs, not through this in-header alias) and unescaped the "\," commas that were only needed for the old alias's comma-separated argument syntax. - @Complexity X (json.hpp x4, json_pointer.hpp x2, serializer.hpp x1): now "Complexity: X". Backslash sequences Clang's comment lexer tried to parse as commands, escaped to render as literal backslashes: - lexer.hpp get_codepoint(): two `\u` occurrences. - binary_reader.hpp get_bson_cstr() / get_bson_cstr_bulk(): two `\x00` occurrences. - serializer.hpp: three `\uXXXX` occurrences (constructor @PARAM, append_codepoint_to_string_buffer() @brief, and the ensure_ascii member comment). One finding remained after all of the above: Clang reports "declaration is marked with '@deprecated' command but does not have a deprecation attribute" on the deprecated sax_parse(span_input_adapter&&, ...) overload, even though JSON_HEDLEY_DEPRECATED_FOR does expand to __attribute__((deprecated(...))) for Clang. Several isolated reproductions of this exact declaration shape - doc comment, template<>, two stacked __attribute__ macros, an overload set sharing the name - did not reproduce the warning, so this looks like a Clang comment/declaration-association quirk specific to this overload inside the much larger basic_json class template, not an actual documentation defect. Rather than keep the pragma library-wide for one Clang false positive, added a tightly scoped -Wdocumentation-deprecated-sync push/pop around just that overload. Verified with Apple clang 21 and the project's actual -Weverything flag set (cmake/clang_flags.cmake) on the full header at -std=c++11 and -std=c++20: zero -Wdocumentation* diagnostics. Also compiled clean with GCC 16.2.0 (the pragmas are already __clang__-gated, so this only confirms no unrelated breakage). Ran make check-amalgamation and the full local test suite: 129/129 passing. #5725 item 2 (step c) Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Take the JSON value by const reference in the array and tuple from_json paths Review feedback on #5737 (gregmarr): once the no-op std::forward calls are gone, the forwarding references have no purpose. from_json_fn passes the value as const BasicJsonType&, so these functions were only ever instantiated with a const lvalue anyway. The std::array, std::pair and std::tuple overloads of from_json and their helpers now take const BasicJsonType& and pass j on unchanged. Because the deduced BasicJsonType is now the plain type, tuple_type and the static_assert name const BasicJsonType& explicitly, so the reference checks are unchanged: get<std::tuple<const std::string&>>() still works, and get<std::tuple<std::string&>>() still fails the same static_assert. from_json_tuple_get_impl keeps its forwarding reference, since tuple_type calls it through std::declval. Behavior, the public API and the ABI do not change. unit-conversions, unit-constructor1, unit-udt, unit-udt_macro, unit-regression1/2/3, unit-deserialization, unit-noexcept, unit-items, unit-allocator, unit-custom-object-type, unit-ordered_json2 and unit-brace-init-copy-semantics pass at C++11, C++17 and C++20 with unchanged assertion counts. Ran make amalgamate. Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
- unit-wstring: with a 16-bit wchar_t (Windows), a lone surrogate is reported as the ill-formed byte 0xFF since #5704; the std::wstring expectations still had the previous <U+0000>. - ci_single_binaries: json_literals.hpp (#5610) and json.hpp include each other on purpose, and IWYU, not following the cycle, asks to replace json.hpp with json_fwd.hpp. Report its findings without failing the build, as already done for json.hpp. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* Keep the serializer conversion for objects whose keys cannot be converted #5591 added a test converting nlohmann::json into a basic_json whose string type cannot be constructed from std::string. That instantiates convert_iteratively(), whose members.emplace_back(next.key(), ...) needs exactly that key conversion, and broke the build of unit-alt-string. Dispatch on the key's constructibility and leave such conversions to the serializers, as the levels above the nesting bound already do (#3425). Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix the remaining CI failures on develop - unit-wstring: with a 16-bit wchar_t (Windows), a lone surrogate is reported as the ill-formed byte 0xFF since #5704; the std::wstring expectations still had the previous <U+0000>. - ci_single_binaries: json_literals.hpp (#5610) and json.hpp include each other on purpose, and IWYU, not following the cycle, asks to replace json.hpp with json_fwd.hpp. Report its findings without failing the build, as already done for json.hpp. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix the library warnings and noexcept specifications from the merged PRs - binary_reader: rename the error_handler constructor parameter, which shadowed the member (-Wshadow, -Wshadow-field-in-constructor; #5746) - basic_json(copy_construct_tag, ...): declare it noexcept when copying the base class is (GCC 16 -Wnoexcept; #5690) - the scalar-on-left legacy comparison operators: noexcept only when converting the scalar is, like their member counterparts (#5682, #5751) - compare_leaves: use std::is_eq/is_lt/is_gt instead of comparing a std::partial_ordering with 0 (-Wzero-as-null-pointer-constant; #5686) - serializer: silence MSVC C4127 for the EnsureAscii template parameter (#5741, #5746) - clang-tidy: return the sanitized reference in binary_writer, take the key of ordered_map::find_impl by const reference (#5727), and mark the switches over parse_array_index (#5728) - ordered_map: keep <memory> for std::allocator (IWYU) Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Split unit-conversions.cpp so MinGW can link it clang 18 with the MinGW linker failed to link test-conversions_cpp17 ("relocation truncated to fit: IMAGE_REL_AMD64_REL32"). As windows.yml recommends, keep the objects small by splitting the test file. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix the tests added by the merged PRs for all CI configurations - discard the results of dump() and from_*() in CHECK_THROWS with utils::ignore_return_value (GCC -Werror=unused-result) - give unit-bson's huge_string_t a default constructor (MSVC C2512, GCC 5, clang 3.5) - unit-disabled_exceptions: use the literals namespace when the global UDLs are off (ci_test_noglobaludls; #5700) - unit-binary_utf8_strict: expect the JSON pointer prefix with JSON_DIAGNOSTICS (#5741) - skip the tests that rely on exceptions under JSON_NOEXCEPTION (#5678, #5732) - clang-tidy and clang -Werror: static test data, CAPTURE(...);, const-correctness, use-after-move alias, unused conversion operator, a missing <iterator> include Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Title the macro examples and add JSON_STRICT_BINARY_UTF8 to the docset The documentation style check requires "Example: ..." titles on pages with several examples (#5741, #5591) and a docset entry for every macro page. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Regenerate BUILD.bazel and nlohmann_json.natvis Signed-off-by: Niels Lohmann <mail@nlohmann.me> #5746 added detail/output/error_handler.hpp and #5741 the json_abi_sbu8 ABI tag. * Install libidn11 for the CMake 3.5.0 binary in ci_cmake_flags Signed-off-by: Niels Lohmann <mail@nlohmann.me> #5733 moved ci_cmake_options from ubuntu:focal to ubuntu:24.04, which no longer ships libidn.so.11; the CMake 3.5.0 release binary links against it, so every ci_cmake_flags run has failed since. Install focal's libidn11 package for that matrix entry only. * Suppress Infer's false STACK_VARIABLE_ADDRESS_ESCAPE in get_impl get_impl() returns its local by value. A test added by the merged PRs instantiates it with a type Infer misreads, so ci_infer reported the 2021 code for the first time. Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Moves
operator""_jsonandoperator""_json_pointerinto a new header<nlohmann/json_literals.hpp>.<nlohmann/json.hpp>still includes it by default, but not whenJSON_NO_AUTOMATIC_UDLSis defined. This follows the design proposed in #5294 (#5294 (comment)).Why
The literals are non-template inline functions whose bodies call
json::parse, so every translation unit that includesjson.hppinstantiates the parser, lexer, and SAX DOM parsers — even if it never parses anything. WithJSON_NO_AUTOMATIC_UDLSset for a whole project, only the translation units that include<nlohmann/json_literals.hpp>pay for that. Nothing changes for anyone who does not define the macro.Changes
include/nlohmann/json_literals.hppcontains the literals and the global using-declarations (controlled byJSON_USE_GLOBAL_UDLS). It includes<nlohmann/json.hpp>itself, so it is self-contained.json.hppincludes it at its very end unlessJSON_NO_AUTOMATIC_UDLSis defined; include guards make either include order work.json.hpp, and the amalgamation inlinesmacro_scope.hpponly once, so the header cannot reopen the macro scope. As a result:__GNUC__/__GNUC_MINOR__instead ofJSON_HEDLEY_GCC_VERSION_CHECK.JSON_HEDLEY_NON_NULL(1)on the twoconst char*overloads is gone. It had no practical effect on a literal operator.JSON_USE_GLOBAL_UDLSor#undefs it at the end ofjson.hpp, since that would also erase a user's-DJSON_USE_GLOBAL_UDLS=0before the new header reads it. The header checks!defined(JSON_USE_GLOBAL_UDLS) || JSON_USE_GLOBAL_UDLS, which keeps the same default.single_include/nlohmann/json_literals.hppis a verbatim copy, because it only includes<nlohmann/json.hpp>. The amalgamatedjson.hppstill contains the literals behind the same include guard. The copy is wired intomake amalgamate/check-amalgamation,ci_test_amalgamation, the amalgamation workflow (acp, because that workflow runs the amalgamation tool fromdevelop),include.zip/release files, and Mesoninstall_headers.BUILD.bazelwas regenerated. CMake and Swift install the whole directory already.json.cppmincludes<nlohmann/json_literals.hpp>explicitly, soimport nlohmann.json;always exports the literals. I checked this with Homebrew LLVM, with and without the macro.tests/src/unit-no_automatic_udls.cpp. With the macro set, it checks viais_detectedthat neither the global nor the namespaced literals are declared (probing argument-dependent lookup with a type in the library namespace). It then includes<nlohmann/json_literals.hpp>and uses the literals, both globally and viausing namespace. The probes are not vacuous: without the macro, all four fail as expected.JSON_NO_AUTOMATIC_UDLSpage, plus cross-references from the macro overview,JSON_USE_GLOBAL_UDLS, both literal pages, the modules page, and the integration page.Also verified locally: the test passes with clang and GCC 16, C++11 and C++20, against both
include/andsingle_include/, with-Wall -Wextra -Wpedantic -Werror.unit-udlstill passes in no-global mode (-DJSON_USE_GLOBAL_UDLS=0, also with-Wundef), andtest-no-macro-leak,test-readme,test-json_pointer, andtest-udtpass.Compile times
Median of 11 runs against the amalgamated header.
empty= only the include,model= a struct withNLOHMANN_DEFINE_TYPE_NON_INTRUSIVEplus ato_jsoncall,parse= onejson::parsecall.-fsyntax-only-c -O0-fsyntax-only-c -O0-fsyntax-only-c -O0-fsyntax-only-c -O0-fsyntax-only-c -O0-fsyntax-only-c -O0-fsyntax-only-c -O0-fsyntax-only-c -O0-fsyntax-only-c -O0-fsyntax-only-c -O0-fsyntax-only-c -O0-fsyntax-only-c -O0Translation units that don't parse get 12–35 % faster. Translation units that parse anyway stay within noise (±5 %), because they instantiate the parser regardless.
Public API
No breaking changes for code that does not define the new macro: the literals, their namespaces, and
JSON_USE_GLOBAL_UDLSbehave as before. Minor observable differences:JSON_USE_GLOBAL_UDLSis no longer#undefined at the end ofjson.hpp, so a user's own definition now survives the include (before, it was erased).nonnullattribute on theconst char*literal overloads is gone.New: header
<nlohmann/json_literals.hpp>and macroJSON_NO_AUTOMATIC_UDLS. A matching CMake option (likeJSON_GlobalUDLs) could follow if wanted.This PR description was written by Claude Code.
🤖 Generated with Claude Code