* Add BON8 support Add to_bon8/from_bon8 and input_format_t::bon8 for BON8, a binary format that uses the byte values that cannot begin a UTF-8 character as type markers, so strings need no length prefix. It is the most compact of the supported binary formats on the benchmark files. The reader is non-recursive like the other binary readers. A string ends at the first byte that cannot continue it, so the reader hands the one or two bytes it reads past a string back to the value that follows. The writer produces the canonical representation of the specification, except for NFC normalization; its output is identical to that of the reference implementation (HikoGUI) on all files of the test data. The round-trip tests need the .bon8 files of json_test_data 3.2.0. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Address review comments - Reuse detail::validate_one_utf8 to check strings in to_bon8; the error now names the first byte of the invalid sequence. - Document that to_bon8 leaves bytes in the output adapter on an exception, and that string_open is only an output of write_bon8_marker. - Explain why the pushback buffer of the BON8 reader cannot overflow. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Select the BON8 float prefix by type get_bon8_float_prefix only depends on the type of its argument, so make the type a template parameter instead of passing an unused value. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Rename a test variable that Flawfinder mistakes for read() Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix the BON8 CI failures - compare the float in write_bon8_float with number_float_t constants, so GCC does not warn about a float-to-double conversion - mark check_bon8_utf8's context as used when exceptions are disabled - choose the compact float prefix in a helper rather than with nested conditional operators (clang-tidy) - use auto for the cast in the BON8 integer reader (clang-tidy) - write the int32 minimum test values as long long literals (MSVC C4146) Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Amalgamate Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Read BON8 strings in bulk from contiguous input - copy the valid UTF-8 of a string in one step when the input is contiguous (twitter.json is read in 1.68 instead of 2.52 ms, jeopardy.json in 196 instead of 297 ms, close to CBOR and MessagePack) - share the new valid_utf8_prefix() with the writer's UTF-8 check, which now skips ASCII 8 bytes at a time - let the fuzzer check that contiguous and stream input give the same value or error, and test both paths in the unit tests - clarify that a second 0xFF after a string is an empty string Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Link the BON8 functions from the other binary format pages Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Name the bulk scan flag after the input, not BON8 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Read BSON keys in bulk from contiguous input BSON keys (and array indices) are C-style strings, which were read byte by byte. For contiguous input they are now read up to their \x00-byte in one step, using the same bulk_scan flag as BON8 strings: twitter.json is read in 1.46 instead of 2.01 ms, citm_catalog.json in 2.93 instead of 3.33 ms, jeopardy.json in 182 instead of 207 ms. canada.json, whose keys are almost all one-digit array indices, takes 2 % longer. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix the BON8 CI failures of the bulk-read tests - skip the contiguous-versus-stream tests of BON8 strings and BSON keys when exceptions are disabled: they catch the parse errors of invalid input, and without exceptions the library aborts instead - use static_cast for the int64 test value (google-readability-casting) Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Move the explicit basic_json instantiation into its own test file Linking test-regression3_cpp20 with clang and MinGW failed with "relocation truncated to fit: IMAGE_REL_AMD64_REL32 against `.rdata'", as test-regression2 did before #5511. The explicit instantiation of basic_json<> for #4825 compiles every member function, including the BON8 reader and writer, into that object, and it was already close to the limit (2,226,104 bytes on develop, 2,234,960 with BON8; clang -O1, C++20). Give the instantiation a file of its own: unit-regression3 is now 1,594,736 bytes and unit-explicit_instantiation 1,095,064. The new file mentions JSON_HAS_CPP_17 and JSON_HAS_CPP_20 so it keeps being built for the C++17 standard the regression was about. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Convert the bytes of the BON8 test strings explicitly The str() helper constructed a std::string from a byte range, which converts each unsigned char implicitly; -fsanitize=integer reports that for bytes of 0x80 and above (ci_test_clang_sanitizer). Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
345 lines
18 KiB
Markdown
345 lines
18 KiB
Markdown
# <small>nlohmann::</small>basic_json
|
|
|
|
<small>Defined in header `<nlohmann/json.hpp>`</small>
|
|
|
|
```cpp
|
|
template<
|
|
template<typename U, typename V, typename... Args> class ObjectType = std::map,
|
|
template<typename U, typename... Args> class ArrayType = std::vector,
|
|
class StringType = std::string,
|
|
class BooleanType = bool,
|
|
class NumberIntegerType = std::int64_t,
|
|
class NumberUnsignedType = std::uint64_t,
|
|
class NumberFloatType = double,
|
|
template<typename U> class AllocatorType = std::allocator,
|
|
template<typename T, typename SFINAE = void> class JSONSerializer = adl_serializer,
|
|
class BinaryType = std::vector<std::uint8_t>,
|
|
class CustomBaseClass = void
|
|
>
|
|
class basic_json;
|
|
```
|
|
|
|
## Template parameters
|
|
|
|
| Template parameter | Description | Derived type |
|
|
|----------------------|---------------------------------------------------------------------------|---------------------------------------------|
|
|
| `ObjectType` | type for JSON objects | [`object_t`](object_t.md) |
|
|
| `ArrayType` | type for JSON arrays | [`array_t`](array_t.md) |
|
|
| `StringType` | type for JSON strings and object keys | [`string_t`](string_t.md) |
|
|
| `BooleanType` | type for JSON booleans | [`boolean_t`](boolean_t.md) |
|
|
| `NumberIntegerType` | type for JSON integer numbers | [`number_integer_t`](number_integer_t.md) |
|
|
| `NumberUnsignedType` | type for JSON unsigned integer numbers | [`number_unsigned_t`](number_unsigned_t.md) |
|
|
| `NumberFloatType` | type for JSON floating-point numbers | [`number_float_t`](number_float_t.md) |
|
|
| `AllocatorType` | type of the allocator to use | |
|
|
| `JSONSerializer` | the serializer to resolve internal calls to `to_json()` and `from_json()` | [`json_serializer`](json_serializer.md) |
|
|
| `BinaryType` | type for binary arrays | [`binary_t`](binary_t.md) |
|
|
| `CustomBaseClass` | extension point for user code | [`json_base_class_t`](json_base_class_t.md) |
|
|
|
|
The library imposes a number of requirements on these types that are not expressed as C++ concepts, such as the
|
|
container operations `object_t` and `array_t` must provide, or the fact that `StringType` must be `char`-based. They
|
|
are collected in [Template Parameter Requirements](../../features/types/template_parameters.md).
|
|
|
|
## Specializations
|
|
|
|
- [**json**](../json.md) - default specialization
|
|
- [**ordered_json**](../ordered_json.md) - a specialization that maintains the insertion order of object keys
|
|
|
|
## Iterator invalidation
|
|
|
|
All operations that add values to an **array** ([`push_back`](push_back.md) , [`operator+=`](operator+=.md),
|
|
[`emplace_back`](emplace_back.md), [`insert`](insert.md), and [`operator[]`](operator%5B%5D.md) for a non-existing
|
|
index) can yield a reallocation, in which case all iterators (including the [`end()`](end.md) iterator) and all
|
|
references to the elements are invalidated.
|
|
|
|
For [`ordered_json`](../ordered_json.md), also all operations that add a value to an **object**
|
|
([`push_back`](push_back.md), [`operator+=`](operator+=.md), [`emplace`](emplace.md), [`insert`](insert.md),
|
|
[`update`](update.md), and [`operator[]`](operator%5B%5D.md) for a non-existing key) can yield a reallocation, in
|
|
which case all iterators (including the [`end()`](end.md) iterator) and all references to the elements are invalidated.
|
|
|
|
## Requirements
|
|
|
|
The class satisfies the following concept requirements:
|
|
|
|
### Basic
|
|
|
|
- [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible): JSON values can be
|
|
default-constructed. The result will be a JSON null value.
|
|
- [MoveConstructible](https://en.cppreference.com/w/cpp/named_req/MoveConstructible): A JSON value can be constructed
|
|
from an rvalue argument.
|
|
- [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible): A JSON value can be
|
|
copy-constructed from an lvalue expression.
|
|
- [MoveAssignable](https://en.cppreference.com/w/cpp/named_req/MoveAssignable): A JSON value can be assigned from an
|
|
rvalue argument.
|
|
- [CopyAssignable](https://en.cppreference.com/w/cpp/named_req/CopyAssignable): A JSON value can be copy-assigned from
|
|
an lvalue expression.
|
|
- [Destructible](https://en.cppreference.com/w/cpp/named_req/Destructible): JSON values can be destructed.
|
|
|
|
### Layout
|
|
|
|
- [StandardLayoutType](https://en.cppreference.com/w/cpp/named_req/StandardLayoutType): JSON values have
|
|
[standard layout](https://en.cppreference.com/w/cpp/language/data_members#Standard_layout): All non-static data
|
|
members are private and standard layout types, the class has no virtual functions or (virtual) base classes.
|
|
|
|
### Library-wide
|
|
|
|
- [EqualityComparable](https://en.cppreference.com/w/cpp/named_req/EqualityComparable): JSON values can be compared with
|
|
`==`, see [`operator==`](operator_eq.md).
|
|
- [LessThanComparable](https://en.cppreference.com/w/cpp/named_req/LessThanComparable): JSON values can be compared with
|
|
`<`, see [`operator<`](operator_le.md).
|
|
- [Swappable](https://en.cppreference.com/w/cpp/named_req/Swappable): Any JSON lvalue or rvalue of can be swapped with
|
|
any lvalue or rvalue of other compatible types, using unqualified function `swap`.
|
|
- [NullablePointer](https://en.cppreference.com/w/cpp/named_req/NullablePointer): JSON values can be compared against
|
|
`std::nullptr_t` objects which are used to model the `null` value.
|
|
|
|
### Container
|
|
|
|
- [Container](https://en.cppreference.com/w/cpp/named_req/Container): JSON values can be used like STL containers and
|
|
provide iterator access.
|
|
- [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer): JSON values can be used like
|
|
STL containers and provide reverse iterator access.
|
|
|
|
## Member types
|
|
|
|
- [**adl_serializer**](../adl_serializer/index.md) - the default serializer
|
|
- [**value_t**](value_t.md) - the JSON type enumeration
|
|
- [**json_pointer**](../json_pointer/index.md) - JSON Pointer implementation
|
|
- [**json_serializer**](json_serializer.md) - type of the serializer to for conversions from/to JSON
|
|
- [**error_handler_t**](error_handler_t.md) - type to choose behavior on decoding errors
|
|
- [**cbor_tag_handler_t**](cbor_tag_handler_t.md) - type to choose how to handle CBOR tags
|
|
- **initializer_list_t** - type for initializer lists of `basic_json` values
|
|
- [**input_format_t**](input_format_t.md) - type to choose the format to parse
|
|
- [**json_sax_t**](../json_sax/index.md) - type for SAX events
|
|
|
|
### Exceptions
|
|
|
|
- [**exception**](exception.md) - general exception of the `basic_json` class
|
|
- [**parse_error**](parse_error.md) - exception indicating a parse error
|
|
- [**invalid_iterator**](invalid_iterator.md) - exception indicating errors with iterators
|
|
- [**type_error**](type_error.md) - exception indicating executing a member function with a wrong type
|
|
- [**out_of_range**](out_of_range.md) - exception indicating access out of the defined range
|
|
- [**other_error**](other_error.md) - exception indicating other library errors
|
|
|
|
### Container types
|
|
|
|
| Type | Definition |
|
|
|--------------------------|-----------------------------------------------------------------------------------------------------------|
|
|
| `value_type` | `#!cpp basic_json` |
|
|
| `reference` | `#!cpp value_type&` |
|
|
| `const_reference` | `#!cpp const value_type&` |
|
|
| `difference_type` | `#!cpp std::ptrdiff_t` |
|
|
| `size_type` | `#!cpp std::size_t` |
|
|
| `allocator_type` | `#!cpp AllocatorType<basic_json>` |
|
|
| `pointer` | `#!cpp std::allocator_traits<allocator_type>::pointer` |
|
|
| `const_pointer` | `#!cpp std::allocator_traits<allocator_type>::const_pointer` |
|
|
| `iterator` | [LegacyBidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator) |
|
|
| `const_iterator` | constant [LegacyBidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator) |
|
|
| `reverse_iterator` | reverse iterator, derived from `iterator` |
|
|
| `const_reverse_iterator` | reverse iterator, derived from `const_iterator` |
|
|
| `iteration_proxy` | helper type for [`items`](items.md) function |
|
|
|
|
### JSON value data types
|
|
|
|
- [**array_t**](array_t.md) - type for arrays
|
|
- [**binary_t**](binary_t.md) - type for binary arrays
|
|
- [**boolean_t**](boolean_t.md) - type for booleans
|
|
- [**default_object_comparator_t**](default_object_comparator_t.md) - default comparator for objects
|
|
- [**number_float_t**](number_float_t.md) - type for numbers (floating-point)
|
|
- [**number_integer_t**](number_integer_t.md) - type for numbers (integer)
|
|
- [**number_unsigned_t**](number_unsigned_t.md) - type for numbers (unsigned)
|
|
- [**object_comparator_t**](object_comparator_t.md) - comparator for objects
|
|
- [**object_t**](object_t.md) - type for objects
|
|
- [**string_t**](string_t.md) - type for strings
|
|
|
|
### Parser callback
|
|
|
|
- [**parse_event_t**](parse_event_t.md) - parser event types
|
|
- [**parser_callback_t**](parser_callback_t.md) - per-element parser callback type
|
|
|
|
## Member functions
|
|
|
|
- [(constructor)](basic_json.md)
|
|
- [(destructor)](~basic_json.md)
|
|
- [**operator=**](operator=.md) - copy assignment
|
|
- [**array**](array.md) (_static_) - explicitly create an array
|
|
- [**binary**](binary.md) (_static_) - explicitly create a binary array
|
|
- [**object**](object.md) (_static_) - explicitly create an object
|
|
|
|
### Object inspection
|
|
|
|
Functions to inspect the type of a JSON value.
|
|
|
|
- [**type**](type.md) - return the type of the JSON value
|
|
- [**operator value_t**](operator_value_t.md) - return the type of the JSON value
|
|
- [**type_name**](type_name.md) - return the type as string
|
|
- [**is_primitive**](is_primitive.md) - return whether the type is primitive
|
|
- [**is_structured**](is_structured.md) - return whether the type is structured
|
|
- [**is_null**](is_null.md) - return whether the value is null
|
|
- [**is_boolean**](is_boolean.md) - return whether the value is a boolean
|
|
- [**is_number**](is_number.md) - return whether the value is a number
|
|
- [**is_number_integer**](is_number_integer.md) - return whether the value is an integer number
|
|
- [**is_number_unsigned**](is_number_unsigned.md) - return whether the value is an unsigned integer number
|
|
- [**is_number_float**](is_number_float.md) - return whether the value is a floating-point number
|
|
- [**is_object**](is_object.md) - return whether the value is an object
|
|
- [**is_array**](is_array.md) - return whether the value is an array
|
|
- [**is_string**](is_string.md) - return whether the value is a string
|
|
- [**is_binary**](is_binary.md) - return whether the value is a binary array
|
|
- [**is_discarded**](is_discarded.md) - return whether the value is discarded
|
|
|
|
Optional functions to access the [diagnostic positions](../macros/json_diagnostic_positions.md).
|
|
|
|
- [**start_pos**](start_pos.md) - return the start position of the value
|
|
- [**end_pos**](end_pos.md) - return the one past the end position of the value
|
|
|
|
### Value access
|
|
|
|
Direct access to the stored value of a JSON value.
|
|
|
|
- [**get**](get.md) - get a value
|
|
- [**get_to**](get_to.md) - get a value and write it to a destination
|
|
- [**get_ptr**](get_ptr.md) - get a pointer value
|
|
- [**get_ref**](get_ref.md) - get a reference value
|
|
- [**operator ValueType**](operator_ValueType.md) - get a value
|
|
- [**get_binary**](get_binary.md) - get a binary value
|
|
|
|
### Element access
|
|
|
|
Access to the JSON value
|
|
|
|
- [**at**](at.md) - access specified element with bounds checking
|
|
- [**operator[]**](operator[].md) - access specified element
|
|
- [**value**](value.md) - access specified object element with default value
|
|
- [**front**](front.md) - access the first element
|
|
- [**back**](back.md) - access the last element
|
|
|
|
### Lookup
|
|
|
|
- [**find**](find.md) - find an element in a JSON object
|
|
- [**count**](count.md) - returns the number of occurrences of a key in a JSON object
|
|
- [**contains**](contains.md) - check the existence of an element in a JSON object
|
|
|
|
### Iterators
|
|
|
|
- [**begin**](begin.md) - returns an iterator to the first element
|
|
- [**cbegin**](cbegin.md) - returns a const iterator to the first element
|
|
- [**end**](end.md) - returns an iterator to one past the last element
|
|
- [**cend**](cend.md) - returns a const iterator to one past the last element
|
|
- [**rbegin**](rbegin.md) - returns an iterator to the reverse-beginning
|
|
- [**rend**](rend.md) - returns an iterator to the reverse-end
|
|
- [**crbegin**](crbegin.md) - returns a const iterator to the reverse-beginning
|
|
- [**crend**](crend.md) - returns a const iterator to the reverse-end
|
|
- [**items**](items.md) - wrapper to access iterator member functions in range-based for
|
|
|
|
### Capacity
|
|
|
|
- [**empty**](empty.md) - checks whether the container is empty
|
|
- [**size**](size.md) - returns the number of elements
|
|
- [**max_size**](max_size.md) - returns the maximum possible number of elements
|
|
|
|
### Modifiers
|
|
|
|
- [**clear**](clear.md) - clears the contents
|
|
- [**push_back**](push_back.md) - add a value to an array/object
|
|
- [**operator+=**](operator+=.md) - add a value to an array/object
|
|
- [**emplace_back**](emplace_back.md) - add a value to an array
|
|
- [**emplace**](emplace.md) - add a value to an object if a key does not exist
|
|
- [**erase**](erase.md) - remove elements
|
|
- [**insert**](insert.md) - inserts elements
|
|
- [**update**](update.md) - updates a JSON object from another object, overwriting existing keys
|
|
- [**swap**](swap.md) - exchanges the values
|
|
|
|
### Lexicographical comparison operators
|
|
|
|
- [**operator==**](operator_eq.md) - comparison: equal
|
|
- [**operator!=**](operator_ne.md) - comparison: not equal
|
|
- [**operator<**](operator_lt.md) - comparison: less than
|
|
- [**operator>**](operator_gt.md) - comparison: greater than
|
|
- [**operator<=**](operator_le.md) - comparison: less than or equal
|
|
- [**operator>=**](operator_ge.md) - comparison: greater than or equal
|
|
- [**operator<=>**](operator_spaceship.md) - comparison: 3-way
|
|
|
|
### Serialization / Dumping
|
|
|
|
- [**dump**](dump.md) - serialization
|
|
|
|
### Deserialization / Parsing
|
|
|
|
- [**parse**](parse.md) (_static_) - deserialize from a compatible input
|
|
- [**accept**](accept.md) (_static_) - check if the input is valid JSON
|
|
- [**sax_parse**](sax_parse.md) (_static_) - generate SAX events
|
|
|
|
### JSON Pointer functions
|
|
|
|
- [**flatten**](flatten.md) - return flattened JSON value
|
|
- [**unflatten**](unflatten.md) - unflatten a previously flattened JSON value
|
|
|
|
### JSON Patch functions
|
|
|
|
- [**patch**](patch.md) - applies a JSON patch
|
|
- [**patch_inplace**](patch_inplace.md) - applies a JSON patch in place
|
|
- [**diff**](diff.md) (_static_) - creates a diff as a JSON patch
|
|
|
|
### JSON Merge Patch functions
|
|
|
|
- [**merge_patch**](merge_patch.md) - applies a JSON Merge Patch
|
|
|
|
## Static functions
|
|
|
|
- [**meta**](meta.md) - returns version information on the library
|
|
- [**get_allocator**](get_allocator.md) - returns the allocator associated with the container
|
|
|
|
### Binary formats
|
|
|
|
- [**from_bjdata**](from_bjdata.md) (_static_) - create a JSON value from an input in BJData format
|
|
- [**from_bon8**](from_bon8.md) (_static_) - create a JSON value from an input in BON8 format
|
|
- [**from_bson**](from_bson.md) (_static_) - create a JSON value from an input in BSON format
|
|
- [**from_cbor**](from_cbor.md) (_static_) - create a JSON value from an input in CBOR format
|
|
- [**from_msgpack**](from_msgpack.md) (_static_) - create a JSON value from an input in MessagePack format
|
|
- [**from_ubjson**](from_ubjson.md) (_static_) - create a JSON value from an input in UBJSON format
|
|
- [**to_bjdata**](to_bjdata.md) (_static_) - create a BJData serialization of a given JSON value
|
|
- [**to_bon8**](to_bon8.md) (_static_) - create a BON8 serialization of a given JSON value
|
|
- [**to_bson**](to_bson.md) (_static_) - create a BSON serialization of a given JSON value
|
|
- [**to_cbor**](to_cbor.md) (_static_) - create a CBOR serialization of a given JSON value
|
|
- [**to_msgpack**](to_msgpack.md) (_static_) - create a MessagePack serialization of a given JSON value
|
|
- [**to_ubjson**](to_ubjson.md) (_static_) - create a UBJSON serialization of a given JSON value
|
|
|
|
## Non-member functions
|
|
|
|
- [**operator<<(std::ostream&)**](../operator_ltlt.md) - serialize to stream
|
|
- [**operator>>(std::istream&)**](../operator_gtgt.md) - deserialize from stream
|
|
- [**to_string**](to_string.md) - user-defined `to_string` function for JSON values
|
|
- [**format_as**](format_as.md) - user-defined `format_as` function for JSON values (fmt support)
|
|
|
|
## Literals
|
|
|
|
- [**operator""_json**](../operator_literal_json.md) - user-defined string literal for JSON values
|
|
|
|
## Helper classes
|
|
|
|
- [**std::formatter<basic_json>**](std_formatter.md) - make JSON values formattable with `std::format`
|
|
- [**std::hash<basic_json>**](std_hash.md) - return a hash value for a JSON object
|
|
- [**std::swap<basic_json>**](std_swap.md) - exchanges the values of two JSON objects
|
|
|
|
## Examples
|
|
|
|
??? example
|
|
|
|
The example shows how the library is used.
|
|
|
|
```cpp
|
|
--8<-- "examples/README.cpp"
|
|
```
|
|
|
|
Output:
|
|
|
|
```json
|
|
--8<-- "examples/README.output"
|
|
```
|
|
|
|
## See also
|
|
|
|
- [RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format](https://tools.ietf.org/html/rfc8259)
|
|
|
|
## Version history
|
|
|
|
- Added in version 1.0.0.
|