* Store maps with enum keys as objects (opt-in) Maps with enum keys, such as std::map<E, T>, are stored as arrays of [key, value] pairs, because enums are not convertible to the string type of object keys - even if NLOHMANN_JSON_SERIALIZE_ENUM maps them to strings (#4378). The new JSON_USE_OBJECTS_FOR_ENUM_KEYED_MAPS macro stores them as objects instead, converting each key with the enum's to_json. It applies to any map-like type with enum keys (std::map with any comparator, std::unordered_map, ...). A key that does not convert to a string throws type_error.302, and two keys converting to the same string throw the new type_error.318, rather than losing an entry. The macro changes the output of inline functions, so it is part of the ABI tag (_ekmo). Reading needs no macro: std::map and std::unordered_map with enum keys are now also read from objects, converting each key with the enum's from_json. That input was rejected before, and arrays of pairs are still read, so data written either way can be read. This supersedes #4531, which first proposed storing these maps as objects. Co-authored-by: Muhammad Amir bin Mohamad Ghazaly <amirghaz@umich.edu> Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Keep multimaps with enum keys as arrays of pairs With JSON_USE_OBJECTS_FOR_ENUM_KEYED_MAPS, is_enum_keyed_map also matched std::multimap and std::unordered_multimap. Storing them as objects throws type_error.318 as soon as a key occurs twice, which is the normal case for a multimap, so such values could no longer be serialized at all once the macro was enabled, although they are stored losslessly as arrays of [key, value] pairs without it. Exclude maps with non-unique keys from is_enum_keyed_map. They are detected by insert(value_type) returning an iterator rather than a pair<iterator, bool>. Map-like types without such an insert() are still treated as before. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Move the default enum-keyed map tests out of unit-conversions.cpp The Windows clang 20.1.8 job (MinGW, Debug) failed to link test-conversions_cpp17 with "relocation truncated to fit: IMAGE_REL_AMD64_REL32 against .rdata": the object file of unit-conversions.cpp was already close to the limit, and the new "maps with enum keys" test case pushed it over. windows.yml asks to keep these objects small by splitting test files. Move the test case unchanged into unit-enum_keyed_maps_default.cpp, with the three enums it needs. It still honors a -D flag for JSON_USE_OBJECTS_FOR_ENUM_KEYED_MAPS, as before. unit-conversions.cpp is back to its state on develop. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Build the enum-keyed map test object instead of parsing it ci_test_diagnostic_positions failed in unit-enum_keyed_maps_default.cpp: with JSON_DIAGNOSTIC_POSITIONS, a parsed value adds its byte range to the exception message ("(bytes 0-7) type must be array, but is object"), so the exact-message checks did not match. Build the object in memory, like unit-custom-array-type.cpp does. Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me> Co-authored-by: Muhammad Amir bin Mohamad Ghazaly <amirghaz@umich.edu>
8.1 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
Parsing
- JSON_PRECISE_STREAM_POSITION - opt in to leaving an input stream positioned right after a parsed number
- JSON_STRICT_BINARY_UTF8 - opt in to checking strings for valid UTF-8 in the CBOR, UBJSON, BJData, and BSON writers
- JSON_STRICT_NUL_HANDLING - opt in to rejecting a NUL byte in the input instead of treating it as end of input
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_STATIC_RTTI - control RTTI (run time type information) support
- JSON_HAS_STD_FORMAT - control
std::format/std::formattersupport - JSON_HAS_THREE_WAY_COMPARISON - control 3-way comparison support
- JSON_NO_AUTOMATIC_UDLS - do not include the user-defined string literals (UDLs) automatically
- JSON_NO_IO - switch off functions relying on certain C++ I/O headers
- JSON_NO_THREAD_LOCAL - switch off the use of
thread_localstorage - 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_DISABLE_TUPLE_REFERENCE_CONVERSION - switch off conversion from a one-element tuple of a JSON reference
- JSON_USE_IMPLICIT_CONVERSIONS - control implicit conversions
- JSON_USE_OBJECTS_FOR_ENUM_KEYED_MAPS - opt in to storing maps with enum keys as objects
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