* 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>
* Add implementation to retrieve start and end positions of json during parse
* Add more unit tests and add start/stop parsing for arrays
* Add raw value for all types
* Add more tests and fix compiler warning
* Amalgamate
* Fix CLang GCC warnings
* Fix error in build
* Style using astyle 3.1
* Fix whitespace changes
* revert
* more whitespace reverts
* Address PR comments
* Fix failing issues
* More whitespace reverts
* Address remaining PR comments
* Address comments
* Switch to using custom base class instead of default basic_json
* Adding a basic using for a json using the new base class. Also address PR comments and fix CI failures
* Address decltype comments
* Diagnostic positions macro (#4)
Co-authored-by: Sush Shringarputale <sushring@linux.microsoft.com>
* Fix missed include deletion
* Add docs and address other PR comments (#5)
* Add docs and address other PR comments
---------
Co-authored-by: Sush Shringarputale <sushring@linux.microsoft.com>
* Address new PR comments and fix CI tests for documentation
* Update documentation based on feedback (#6)
---------
Co-authored-by: Sush Shringarputale <sushring@linux.microsoft.com>
* Address std::size_t and other comments
* Fix new CI issues
* Fix lcov
* Improve lcov case with update to handle_diagnostic_positions call for discarded values
* Fix indentation of LCOV_EXCL_STOP comments
* fix amalgamation astyle issue
---------
Co-authored-by: Sush Shringarputale <sushring@linux.microsoft.com>
* CMake: configure pkg-config with @only
And use @ replacement instead of {}, since ${} is significant in
pkg-config's format
* CMake: use conventional prefix and incluedir variables in pkg-config
This is more standard, and has some advantages when cross compiling.
This also means that the pkg-config files generated by Meson and CMake
now match.
Starting with CMake 3.27, deprecation warnings are issued
when asking for policy settings for CMake 3.4 or earlier.
The cmake_minimum_required() command accepts a version
range, which allows NEW policy settings up to the upper end
of that range to be used, but without raising the minimum
CMake version above the bottom of that range. This means
NEW policy settings will be used where available, without
requiring them. This change updates the project's
cmake_minimum_required() calls to use a version range to
extend the upper policy version to 3.14 where it wasn't already
at that version or higher. This prevents the deprecation warning
from CMake 3.27, and gives breathing space before a future
CMake release will start issuing similar deprecation warnings
again.
* Move UDLs into nlohmann::literals::json_literals namespace
* Add 'using namespace' to unit tests
* Add 'using namespace' to examples
* Add 'using namespace' to README
* Move UDL mkdocs pages out of basic_json/
* Update documentation
* Update docset index
* Add JSON_GlobalUDLs CMake option
* Add unit test
* Build examples without global UDLs
* Add CI target
As nlohmann_json is a header-only library, its pkg-config and cmake
config files should be installed to `share/` (GNUInstallDirs' DATADIR),
as `share/` is the canonical directory where architecture-independent
files should be stored, while `lib/` is for architecture-dependent stuff
(see the [FHS][]).
Apart from being technically correct, installing to `share/` can help
with cross-compiling, for example in Debian-based environments. If
you're interested, I'd suggest reading this [thread][].
Related to #1697
[FHS]: https://refspecs.linuxfoundation.org/FHS_3.0/fhs-3.0.html#usrshareArchitectureindependentData
[thread]: https://github.com/marzer/tomlplusplus/pull/165#issuecomment-1193146210
* Add C++20 3-way comparison operator and fix broken comparisons
Fixes#3207.
Fixes#3409.
* Fix iterators to meet (more) std::ranges requirements
Fixes#3130.
Related discussion: #3408
* Add note about CMake standard version selection to unit tests
Document how CMake chooses which C++ standard version to use when
building tests.
* Update documentation
* CI: add legacy discarded value comparison
* Fix internal linkage errors when building a module
* Refactor unit test creation
Add functions for creating tests and to supply test- and
standard-specific build settings.
Raises minimum CMake version to 3.13 in test directory.
json_test_add_test_for(
<file>
MAIN <main>
[CXX_STANDARDS <version_number>...] [FORCE])
Given a <file> unit-foo.cpp, produces
test-foo_cpp<version_number>
if C++ standard <version_number> is supported by the compiler and
thesource file contains JSON_HAS_CPP_<version_number>. Use FORCE to
create the test regardless of the file containing
JSON_HAS_CPP_<version_number>. Test targets are linked against <main>.
CXX_STANDARDS defaults to "11".
json_test_set_test_options(
all|<tests>
[CXX_STANDARDS all|<args>...]
[COMPILE_DEFINITIONS <args>...]
[COMPILE_FEATURES <args>...]
[COMPILE_OPTIONS <args>...]
[LINK_LIBRARIES <args>...]
[LINK_OPTIONS <args>...])
Supply test- and standard-specific build settings.
Specify multiple tests using a list e.g., "test-foo;test-bar".
Must be called BEFORE the test is created.
* Use CMAKE_MODULE_PATH
* Don't undef some macros if JSON_TEST_KEEP_MACROS is defined
* Use JSON_TEST_KEEP_MACROS
Incidentally enables the regression tests for #2546 and #3070.
A CHECK_THROWS_WITH_AS in #3070 was disabled which is tracked in #3377
and a line in from_json(..., std_fs::path&) was marked with LCOV_EXCL_LINE.
* Add three-way comparison feature test macro
* Disable broken comparison if JSON_HAS_THREE_WAY_COMPARISON
* Fix redefinition of inline constexpr statics
Redelcaration of inline constexpr static data members in namespace scope
was deprecated in C++17. Fixes -Werror=deprecated compilation failures.
* Fix more test build failures due to missing noexcept
* CI: update cmake_flags test to use CMake 3.13 in test directory
Also change default for JSON_BuildTests option to depend on CMake
version.
* CI: turn *_CXXFLAGS into CMake lists
* CI: use JSON_TestStandards to set CXX_STANDARD
* CI: pass extra CXXFLAGS to standards tests
Without SYSTEM static analysis like clang-tidy raise warnings on
json.hpp. This is not desirable for projects embedding nlohmann_json
Tests:
- Validated that without the option OFF the warnings are raised as
before
- Validated that with the option ON the warnings are silenced
When the testsuite is enabled, the "cmake_add_subdirectory" test
adds a second copy of the project into the build configuration, and the
project files are installed twice.
This becomes super problematic when it tries to install a file from
CMAKE_BINARY_DIR which is only available in CMAKE_CURRENT_BINARY_DIR
and bombs out with the following error message:
```
[...]
-- Installing: <DESTDIR>/usr/lib/cmake/nlohmann_json/nlohmann_jsonTargets.cmake
CMake Error at test/cmake_add_subdirectory/nlohmann_json/cmake_install.cmake:73 (file):
file INSTALL cannot find
"......./nlohmann-json/builddir/test/cmake_add_subdirectory/nlohmann_json.pc":
No such file or directory.
Call Stack (most recent call first):
test/cmake_add_subdirectory/cmake_install.cmake:47 (include)
test/cmake_install.cmake:49 (include)
cmake_install.cmake:94 (include)
```