The bulk string scanner validates UTF-8 straight from a contiguous buffer. The scalar validator caps at ~0.3-0.7 GB/s on non-ASCII text; a SIMD validator reaches several GB/s. Rather than hand-rolling SIMD UTF-8 validation (easy to get subtly wrong - a from-scratch SSE attempt rejected valid CJK), wire in the vetted simdutf library behind an opt-in switch. simdutf is not header-only (it ships simdutf.cpp and uses runtime CPU dispatch), so it is not vendored: defining JSON_USE_SIMDUTF includes <simdutf.h> and routes the bulk validator through simdutf::validate_utf8; the project supplies and links simdutf. Undefined (the default), nothing external is included and the portable C++11 scalar path is used, so the library stays header-only and its baseline behavior is unchanged. Design keeps behavior identical either way: - scan_string_bulk() now finds the run up to the next quote/escape/control byte (non-ASCII allowed) and validates it in one shot; on the rare validation failure it recomputes the exact valid prefix with the scalar helper, so ill-formed input still falls through to the byte path and is reported at the same position with the same message. - the per-sequence scalar path is factored into scalar_string_bulk_run() and is the default backend; the refactor is behavior-preserving and does not change scalar throughput. Verified: default and JSON_USE_SIMDUTF builds accept/reject/parse identically across 2,000,000 arbitrary-byte documents and 1,000,000 mixed-escape/UTF-8 documents (differential fuzz vs the streaming byte path); lexer/parser/diagnostic-position/deserialization suites pass under both configurations (20,188 assertions with the backend enabled); warning-clean on g++ and clang, C++11 and C++20, both configurations. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AXcDtEma2PjxgmPS9cQGzA Signed-off-by: Niels Lohmann <mail@nlohmann.me>
7.0 KiB
Macros
Some aspects of the library can be configured by defining preprocessor macros before including the json.hpp
header. See also the macro overview page.
Runtime assertions
- JSON_ASSERT(x) - control behavior of runtime assertions
Exceptions
- JSON_CATCH_USER(exception)
JSON_THROW_USER(exception)
JSON_TRY_USER - control exceptions - JSON_DIAGNOSTICS - control extended diagnostics
- JSON_DIAGNOSTIC_POSITIONS - access positions of elements
- JSON_NOEXCEPTION - switch off exceptions
Language support
- JSON_HAS_CPP_11
JSON_HAS_CPP_14
JSON_HAS_CPP_17
JSON_HAS_CPP_20 - set supported C++ standard - JSON_HAS_FILESYSTEM
JSON_HAS_EXPERIMENTAL_FILESYSTEM - controlstd::filesystemsupport - JSON_HAS_RANGES - control
std::rangessupport - JSON_HAS_STD_FORMAT - control
std::format/std::formattersupport - JSON_HAS_THREE_WAY_COMPARISON - control 3-way comparison support
- JSON_NO_IO - switch off functions relying on certain C++ I/O headers
- JSON_SKIP_UNSUPPORTED_COMPILER_CHECK - do not warn about unsupported compilers
- JSON_USE_GLOBAL_UDLS - place user-defined string literals (UDLs) into the global namespace
- JSON_USE_SIMDUTF - use the simdutf library to accelerate UTF-8 validation
Library version
- JSON_SKIP_LIBRARY_VERSION_CHECK - skip library version check
- NLOHMANN_JSON_VERSION_MAJOR
NLOHMANN_JSON_VERSION_MINOR
NLOHMANN_JSON_VERSION_PATCH - library version information
Library namespace
- NLOHMANN_JSON_NAMESPACE - full name of the
nlohmannnamespace - NLOHMANN_JSON_NAMESPACE_BEGIN
NLOHMANN_JSON_NAMESPACE_END - open and close the library namespace - NLOHMANN_JSON_NAMESPACE_NO_VERSION - disable the version component of the inline namespace
Type conversions
- JSON_BRACE_INIT_COPY_SEMANTICS - opt in to copy/move semantics for single-element brace initialization
- JSON_DISABLE_ENUM_SERIALIZATION - switch off default serialization/deserialization functions for enums
- JSON_USE_IMPLICIT_CONVERSIONS - control implicit conversions
Comparison behavior
- JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON - control comparison of discarded values
Serialization/deserialization macros
Enums
- NLOHMANN_JSON_SERIALIZE_ENUM - serialize/deserialize an enum
- NLOHMANN_JSON_SERIALIZE_ENUM_STRICT - serialize/deserialize an enum with exceptions
Classes and structs
-
NLOHMANN_DEFINE_TYPE_INTRUSIVE - serialize/deserialize a non-derived class with private members
-
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT - serialize/deserialize a non-derived class with private members; uses default values
-
NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE - serialize a non-derived class with private members
-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE - serialize/deserialize a non-derived class
-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT - serialize/deserialize a non-derived class; uses default values
-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE - serialize a non-derived class
-
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE - serialize/deserialize a derived class with private members
-
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT - serialize/deserialize a derived class with private members; uses default values
-
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE - serialize a derived class with private members
-
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE - serialize/deserialize a derived class
-
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT - serialize/deserialize a derived class; uses default values
-
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE - serialize a derived class
-
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_NAMES - serialize/deserialize a non-derived class with private members; uses custom names
-
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT_WITH_NAMES - serialize/deserialize a non-derived class with private members; uses default values; uses custom names
-
NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE_WITH_NAMES - serialize a non-derived class with private members; uses custom names
-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_NAMES - serialize/deserialize a non-derived class; uses custom names
-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT_WITH_NAMES - serialize/deserialize a non-derived class; uses default values; uses custom names
-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE_WITH_NAMES - serialize a non-derived class; uses custom names
-
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_NAMES - serialize/deserialize a derived class with private members; uses custom names
-
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT_WITH_NAMES - serialize/deserialize a derived class with private members; uses default values; uses custom names
-
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE_WITH_NAMES - serialize a derived class with private members; uses custom names
-
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_NAMES - serialize/deserialize a derived class; uses custom names
-
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT_WITH_NAMES - serialize/deserialize a derived class; uses default values; uses custom names
-
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE_WITH_NAMES - serialize a derived class; uses custom names