* Clean up README, contribution guide, and repository metadata - REUSE.toml: fix the Hedley path and SPDX id (CC0-1.0), mark the docset icons as the public-domain JSON logo - CITATION.cff: v3.12.0 was released on 2025-04-11 - FILES.md: list all workflows, describe the Meson option defaults - README: ctest -LE, table of contents, typos, stale Android/MinGW advice, moved links, merge duplicate Thanks entries, list the analysis tools used in CI - CONTRIBUTING: iterative parser, json_literals.hpp is generated, links - .gitignore: backups and release outputs; .gitattributes: mark generated files - labeler: label other build systems and .github documentation - MODULE.bazel: add the module version Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Clean up CI, CMake, Makefile, and Bazel files - CMakeLists.txt: avoid VERSION_GREATER_EQUAL, which CMake < 3.7 lacks - ci.cmake: test JSON_DisableTupleReferenceConversion in ci_cmake_flags, remove unused variables and unreachable per-compiler targets, look up Clang tools consistently, forward CMAKE_CXX_FLAGS to ci_module_cpp20, format json_literals.hpp and remove backups in ci_test_amalgamation - BUILD.bazel: add json_literals.hpp to the single-header target - workflows: format json_literals.hpp before copying it, drop the obsolete natvis --version plumbing, name natvis and macro_builder in failure messages, drop the duplicate amalgamation job, install Valgrind only where needed, republish docs on version bumps, fix stale names - Makefile: complete .PHONY and help, check-amalgamation always restores the checked-in files, natvis uses its own venv, macro_builder_check installs astyle, clean removes the fuzzer binaries - remove tools/amalgamate/config_json_view.json (json_view.hpp is not on develop yet) Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix inaccuracies in the documentation - version history: json_base_class_t (3.11.3), JSON_HAS_CPP_11 (3.10.0), JSON_HAS_RANGES exclusions, define-type macros, ABI tags - releases: 3.12.0 raised the minimum CMake version - from_*: the (ptr, len) overloads are deleted, not removed, in 4.0.0 - contains/count/find: document the deleted integral overloads - add JSON_HAS_RANGE_VIEW_CONVERSION and list the JSON_HAS_* macros in the macro overview - mention BON8 and error_handler where binary formats are listed - broken links, outdated URLs, warning count, Hunter v0.26.12 - copy_markdown_source hook: expand snippets in the Markdown copies Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Pass /EHsc to the Windows C++20 module build ci_module_cpp20 now forwards CMAKE_CXX_FLAGS to the module build. The Windows workflow sets CMAKE_CXX_FLAGS, which replaces CMake's MSVC defaults including /EHsc, so <chrono> failed with C4530 under /WX. Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
16 KiB
Supported Macros
Some aspects of the library can be configured by defining preprocessor macros before including the json.hpp header.
See also the API documentation for macros for examples and more information.
JSON_ASSERT(x)
This macro controls which code is executed for runtime assertions of the library.
See full documentation of JSON_ASSERT(x).
JSON_BRACE_INIT_COPY_SEMANTICS
When defined to 1, single-element brace initialization of a basic_json value (e.g., #!cpp json j{value};) is
treated as a copy/move of the element rather than wrapping it in a single-element array. The default value is 0, which
preserves the existing behavior.
See full documentation of JSON_BRACE_INIT_COPY_SEMANTICS.
JSON_CATCH_USER(exception)
This macro overrides #!cpp catch calls inside the library.
See full documentation of JSON_CATCH_USER(exception).
JSON_DELETE_DEPRECATED_FUNCTIONS
When defined to 1, all deprecated functions are declared as deleted instead of only being marked as deprecated, so
code that still calls them no longer compiles. This way, you can find all calls that need to be replaced before version
4.0.0 removes these functions.
The macro can also be set with the CMake option
JSON_DeleteDeprecatedFunctions (OFF by default).
See full documentation of JSON_DELETE_DEPRECATED_FUNCTIONS.
JSON_DIAGNOSTICS
This macro enables extended diagnostics for exception messages. Possible values are 1 to enable or 0 to disable
(default).
When enabled, exception messages contain a JSON Pointer to the JSON value that triggered the exception, see Extended diagnostic messages for an example. Note that enabling this macro increases the size of every JSON value by one pointer and adds some runtime overhead.
The diagnostics messages can also be controlled with the CMake option
JSON_Diagnostics (OFF by default) which sets JSON_DIAGNOSTICS
accordingly.
See full documentation of JSON_DIAGNOSTICS.
JSON_DIAGNOSTIC_POSITIONS
When enabled, two new member functions start_pos() and
end_pos() are added to basic_json values. If the value
was created by calling theparse function, then these functions allow querying the byte
positions of the value in the input it was parsed from. The byte positions are also used in exceptions to help locate
errors.
The diagnostics positions can also be controlled with the CMake option
JSON_Diagnostic_Positions (OFF by default) which sets
JSON_DIAGNOSTIC_POSITIONS accordingly.
See full documentation of JSON_DIAGNOSTIC_POSITIONS
JSON_HAS_CPP_11, JSON_HAS_CPP_14, JSON_HAS_CPP_17, JSON_HAS_CPP_20, JSON_HAS_CPP_23, JSON_HAS_CPP_26
The library targets C++11, but also supports some features introduced in later C++ versions (e.g., std::string_view
support for C++17). For these new features, the library implements some preprocessor checks to determine the C++
standard. By defining any of these symbols, the internal check is overridden and the provided C++ version is
unconditionally assumed. This can be helpful for compilers that only implement parts of the standard and would be
detected incorrectly.
JSON_HAS_FILESYSTEM, JSON_HAS_EXPERIMENTAL_FILESYSTEM
When compiling with C++17, the library provides conversions from and to std::filesystem::path. As compiler support
for filesystem is limited, the library tries to detect whether <filesystem>/std::filesystem (JSON_HAS_FILESYSTEM)
or <experimental/filesystem>/std::experimental::filesystem (JSON_HAS_EXPERIMENTAL_FILESYSTEM) should be used.
To override the built-in check, define JSON_HAS_FILESYSTEM or JSON_HAS_EXPERIMENTAL_FILESYSTEM to 1.
See full documentation of JSON_HAS_FILESYSTEM and JSON_HAS_EXPERIMENTAL_FILESYSTEM.
JSON_HAS_RANGES
The library uses std::ranges (and concepts) where available, for example, to parse from C++20 ranges and to
construct JSON arrays from range views. The library detects whether the standard library supports ranges and
disables the support on toolchains with an incomplete implementation. To override the built-in check, define
JSON_HAS_RANGES to 1 or 0.
See full documentation of JSON_HAS_RANGES.
JSON_HAS_RANGE_VIEW_CONVERSION
When JSON_HAS_RANGES is enabled (and the compiler is not MinGW), a JSON array can be constructed directly from a C++20
range view such as std::views::filter(...). To override the built-in check, define JSON_HAS_RANGE_VIEW_CONVERSION to
1 or 0.
See full documentation of JSON_HAS_RANGE_VIEW_CONVERSION.
JSON_HAS_STATIC_RTTI
The library detects whether the compiler supports run time type information (RTTI), which it needs, for instance, to
exclude std::any from the candidate types of the implicit conversion on C++17. To override the built-in check, define
JSON_HAS_STATIC_RTTI to 1 or 0.
See full documentation of JSON_HAS_STATIC_RTTI.
JSON_HAS_STD_FORMAT
When compiling with C++20 and a standard library that provides <format>, the library provides a std::formatter
specialization for JSON values. To override the built-in check, define JSON_HAS_STD_FORMAT to 1 or 0.
See full documentation of JSON_HAS_STD_FORMAT.
JSON_HAS_THREE_WAY_COMPARISON
When the compiler and standard library support 3-way comparison (the spaceship operator <=>), the library provides it
for JSON values. To override the built-in check, define JSON_HAS_THREE_WAY_COMPARISON to 1 or 0.
See full documentation of JSON_HAS_THREE_WAY_COMPARISON.
JSON_NOEXCEPTION
Exceptions can be switched off by defining the symbol JSON_NOEXCEPTION.
See full documentation of JSON_NOEXCEPTION.
JSON_DISABLE_ENUM_SERIALIZATION
When defined, default parse and serialize functions for enums are excluded and have to be provided by the user, for example, using NLOHMANN_JSON_SERIALIZE_ENUM.
See full documentation of JSON_DISABLE_ENUM_SERIALIZATION.
JSON_DISABLE_TUPLE_REFERENCE_CONVERSION
When defined to 1, a JSON value can no longer be created from a one-element std::tuple holding a reference to a JSON
value, such as the result of std::forward_as_tuple(j). This lets std::tuple convert such tuples element-wise. This
is planned to become the default in version 4.0.0.
See full documentation of JSON_DISABLE_TUPLE_REFERENCE_CONVERSION.
JSON_NO_AUTOMATIC_UDLS
When defined, <nlohmann/json.hpp> does not include <nlohmann/json_literals.hpp> with the user-defined string literals
operator""_json and operator""_json_pointer. This reduces the compile time of translation units that do not use
them, because the literals instantiate the parser in every translation unit that includes them. Include
<nlohmann/json_literals.hpp> where the literals are needed.
See full documentation of JSON_NO_AUTOMATIC_UDLS.
JSON_NO_IO
When defined, headers <cstdio>, <ios>, <iosfwd>, <istream>, and <ostream> are not included and parse functions
relying on these headers are excluded. This is relevant for environment where these I/O functions are disallowed for
security reasons (e.g., Intel Software Guard Extensions (SGX)).
See full documentation of JSON_NO_IO.
JSON_NO_THREAD_LOCAL
When defined, the library does not use #!cpp thread_local storage. Copying a value and comparing two values then
always avoid the call stack rather than descending into a bounded number of levels first, which is slower but yields the
same values and the same comparisons.
See full documentation of JSON_NO_THREAD_LOCAL.
JSON_PRECISE_STREAM_POSITION
When defined to 1, operator>> and non-strict
sax_parse leave an input stream positioned right after the parsed value, instead of
also consuming the character that terminates a number. The default value is 0, which preserves the existing behavior;
this is planned to become the default in version 4.0.0.
See full documentation of JSON_PRECISE_STREAM_POSITION.
JSON_SKIP_LIBRARY_VERSION_CHECK
When defined, the library will not create a compiler warning when a different version of the library was already included.
See full documentation of JSON_SKIP_LIBRARY_VERSION_CHECK.
JSON_SKIP_UNSUPPORTED_COMPILER_CHECK
When defined, the library will not create a compile error when a known unsupported compiler is detected. This allows using the library with compilers that do not fully support C++11 and may only work if unsupported features are not used.
See full documentation of JSON_SKIP_UNSUPPORTED_COMPILER_CHECK.
JSON_STRICT_BINARY_UTF8
When defined to 1, to_cbor, to_ubjson,
to_bjdata, and to_bson throw
type_error.316 for a string value or object key that is not
valid UTF-8. The default value is 0, which writes the bytes unchanged as before version 3.13.0; this is planned to
become the default in version 4.0.0.
The check can also be enabled with the CMake option
JSON_StrictBinaryUTF8 (OFF by default) which sets
JSON_STRICT_BINARY_UTF8 accordingly.
See full documentation of JSON_STRICT_BINARY_UTF8.
JSON_STRICT_NUL_HANDLING
When defined to 1, a '\0' (NUL) byte anywhere in the input is rejected with parse_error.101, like any other
unexpected byte, instead of being silently treated as end of input (see the
FAQ entry for background). The default value is 0, which preserves the
existing behavior; this is planned to become the default in version 4.0.0.
The strict handling can also be enabled with the CMake option
JSON_StrictNulHandling (OFF by default) which sets
JSON_STRICT_NUL_HANDLING accordingly.
See full documentation of JSON_STRICT_NUL_HANDLING.
JSON_THROW_USER(exception)
This macro overrides #!cpp throw calls inside the library. The argument is the exception to be thrown.
See full documentation of JSON_THROW_USER(exception).
JSON_TRY_USER
This macro overrides #!cpp try calls inside the library.
See full documentation of JSON_TRY_USER.
JSON_USE_IMPLICIT_CONVERSIONS
When defined to 0, implicit conversions are switched off. By default, implicit conversions are switched on.
See full documentation of JSON_USE_IMPLICIT_CONVERSIONS.
JSON_USE_GLOBAL_UDLS
When defined to 1 (default), the user-defined string literals operator""_json and operator""_json_pointer are
placed into the global namespace instead of nlohmann::literals::json_literals.
See full documentation of JSON_USE_GLOBAL_UDLS.
JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
When defined to 1, the library restores the legacy behavior in which a discarded value compared equal to itself. This
behavior is deprecated and switched off (0) by
default.
See full documentation of JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON.
JSON_USE_OBJECTS_FOR_ENUM_KEYED_MAPS
When defined to 1, maps with enum keys (e.g., std::map<E, T>) are stored as objects, using the enum's conversion for
the keys, instead of arrays of [key, value] pairs. It is switched off (0) by default.
See full documentation of JSON_USE_OBJECTS_FOR_ENUM_KEYED_MAPS.
JSON_USE_SIMDUTF
When defined, UTF-8 validation of JSON strings read from contiguous byte input is delegated to the simdutf library instead of the built-in scalar validator. This is an opt-in external dependency and is not defined by default.
See full documentation of JSON_USE_SIMDUTF.
NLOHMANN_DEFINE_TYPE_*(...), NLOHMANN_DEFINE_DERIVED_TYPE_*(...)
The library defines 12 macros to simplify the serialization/deserialization of types. See the page on arbitrary type conversion for a detailed discussion.
NLOHMANN_JSON_NAMESPACE, NLOHMANN_JSON_NAMESPACE_BEGIN, NLOHMANN_JSON_NAMESPACE_END, NLOHMANN_JSON_NAMESPACE_NO_VERSION
These macros relate to the versioned, inline nlohmann namespace:
NLOHMANN_JSON_NAMESPACEevaluates to the full name of thenlohmannnamespace (including the inline ABI namespace).NLOHMANN_JSON_NAMESPACE_BEGIN/NLOHMANN_JSON_NAMESPACE_ENDopen and close the namespace (for example, to add specializations).NLOHMANN_JSON_NAMESPACE_NO_VERSION, when defined to1, omits the version component from the inline namespace.
See the nlohmann Namespace page, and the full documentation of
NLOHMANN_JSON_NAMESPACE,
NLOHMANN_JSON_NAMESPACE_BEGIN / NLOHMANN_JSON_NAMESPACE_END, and
NLOHMANN_JSON_NAMESPACE_NO_VERSION.
NLOHMANN_JSON_SERIALIZE_ENUM(type, ...)
This macro simplifies the serialization/deserialization of enum types. See Specializing enum conversion for more information.
See full documentation of NLOHMANN_JSON_SERIALIZE_ENUM.
A strict variant NLOHMANN_JSON_SERIALIZE_ENUM_STRICT throws an
exception on undefined input instead of falling back to the first mapping.
NLOHMANN_JSON_VERSION_MAJOR, NLOHMANN_JSON_VERSION_MINOR, NLOHMANN_JSON_VERSION_PATCH
These macros are defined by the library and contain the version numbers according to Semantic Versioning 2.0.0.