This commit is contained in:
nlohmann
2026-10-03 05:43:12 +00:00
parent 2aaa1d24ef
commit a410b4f40f
741 changed files with 8115 additions and 2252 deletions
File diff suppressed because one or more lines are too long
+12 -12
View File
@@ -21,7 +21,7 @@ class vector ["std::vector<uint8_t>"]
vector <|-- binary_t
```
By default, binary values are stored as `std::vector<std::uint8_t>`. This type can be changed by providing a template parameter to the `basic_json` type. To store binary subtypes, the storage type is extended and exposed as `json::binary_t`:
By default, binary values are stored as `std::vector<std::uint8_t>`. This type can be changed by providing a template parameter to the `basic_json` type. To store binary subtypes, the storage type is extended and exposed as [`json::binary_t`](https://json.nlohmann.me/api/basic_json/binary_t/index.md):
```
auto binary = json::binary_t({0xCA, 0xFE, 0xBA, 0xBE});
@@ -56,21 +56,21 @@ JSON values can be constructed from `json::binary_t`:
json j = binary;
```
Binary values are primitive values just like numbers or strings:
Binary values are primitive values just like numbers or strings, as reflected by [`is_binary()`](https://json.nlohmann.me/api/basic_json/is_binary/index.md) and [`is_primitive()`](https://json.nlohmann.me/api/basic_json/is_primitive/index.md):
```
j.is_binary(); // returns true
j.is_primitive(); // returns true
```
Given a binary JSON value, the `binary_t` can be accessed by reference as via `get_binary()`:
Given a binary JSON value, the `binary_t` can be accessed by reference via [`get_binary()`](https://json.nlohmann.me/api/basic_json/get_binary/index.md):
```
j.get_binary().has_subtype(); // returns true
j.get_binary().size(); // returns 4
```
For convenience, binary JSON values can be constructed via `json::binary`:
For convenience, binary JSON values can be constructed via [`json::binary`](https://json.nlohmann.me/api/basic_json/binary/index.md):
```
auto j2 = json::binary({0xCA, 0xFE, 0xBA, 0xBE}, 23);
@@ -89,7 +89,7 @@ Binary values are serialized differently according to the formats.
JSON does not have a binary type, and this library does not introduce a new type as this would break conformance. Instead, binary values are serialized as an object with two keys: `bytes` holds an array of integers, and `subtype` is an integer or `null`.
Example
Example: serialize a binary value to JSON
Code:
@@ -121,7 +121,7 @@ The JSON parser will not parse the objects generated by binary values back to bi
[BJData](https://json.nlohmann.me/features/binary_formats/bjdata/index.md) neither supports binary values nor subtypes and proposes to serialize binary values as an array of uint8 values. The library implements this translation.
Example
Example: serialize a binary value to BJData
Code:
@@ -178,7 +178,7 @@ Note that subtype (42) is **not** serialized and that BJData has **no binary typ
[BON8](https://json.nlohmann.me/features/binary_formats/bon8/index.md) neither supports binary values nor subtypes. The library serializes binary values as an array of integers.
Example
Example: serialize a binary value to BON8
Code:
@@ -212,7 +212,7 @@ Note that the subtype is lost, and deserializing `v` would yield the following v
[BSON](https://json.nlohmann.me/features/binary_formats/bson/index.md) supports binary values and subtypes. If a subtype is given, it is used and added as an unsigned 8-bit integer. If no subtype is given, the generic binary subtype 0x00 is used.
Example
Example: serialize a binary value to BSON
Code:
@@ -252,7 +252,7 @@ Note that the serialization preserves the subtype, and deserializing `v` would y
[CBOR](https://json.nlohmann.me/features/binary_formats/cbor/index.md) supports binary values, but no subtypes. Subtypes will be serialized as tags. Any binary value will be serialized as byte strings. The library will choose the smallest representation using the length of the byte array.
Example
Example: serialize a binary value to CBOR
Code:
@@ -276,7 +276,7 @@ auto v = json::to_cbor(j);
0xCA 0xFE 0xBA 0xBE // content
```
Note that the subtype is serialized as tag. However, parsing tagged values yield a parse error unless `json::cbor_tag_handler_t::ignore` or `json::cbor_tag_handler_t::store` is passed to `json::from_cbor`.
Note that the subtype is serialized as tag. However, parsing tagged values yield a parse error unless `json::cbor_tag_handler_t::ignore` or `json::cbor_tag_handler_t::store` is passed to [`json::from_cbor`](https://json.nlohmann.me/api/basic_json/from_cbor/index.md) (see [`cbor_tag_handler_t`](https://json.nlohmann.me/api/basic_json/cbor_tag_handler_t/index.md)).
```
{
@@ -293,7 +293,7 @@ Note that the subtype is serialized as tag. However, parsing tagged values yield
If no subtype is given, the bin family (bin8, bin16, bin32) is used.
Example
Example: serialize a binary value to MessagePack
Code:
@@ -332,7 +332,7 @@ Note that the serialization preserves the subtype, and deserializing `v` would y
[UBJSON](https://json.nlohmann.me/features/binary_formats/ubjson/index.md) neither supports binary values nor subtypes and proposes to serialize binary values as an array of uint8 values. The library implements this translation.
Example
Example: serialize a binary value to UBJSON
Code: