Though JSON is a ubiquitous data format, it is not a very compact format suitable for data exchange, for instance over a network. Hence, the library supports
The library implements several [binary formats](binary_formats/index) that encode JSON in an efficient way. Most of these formats support binary values; that is, values that have semantics define outside the library and only define a sequence of bytes to be stored.
The library implements several [binary formats](binary_formats/index.md) that encode JSON in an efficient way. Most of these formats support binary values; that is, values that have semantics define outside the library and only define a sequence of bytes to be stored.
JSON itself does not have a binary value. As such, binary values are an extension that this library implements to store values received by a binary format. Binary values are never created by the JSON parser, and are only part of a serialized JSON text if they have been created manually or via a binary format.
## API for binary values
```plantuml
class json::binary_t {
-- setters --
+void set_subtype(std::uint8_t subtype)
+void clear_subtype()
-- getters --
+std::uint8_t subtype() const
+bool has_subtype() const
}
"std::vector<uint8_t>" <|-- 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`:
```cpp
...
...
@@ -105,7 +118,7 @@ JSON does not have a binary type, and this library does not introduce a new type
### BSON
[BSON](binary_formats/bson) supports binary values and subtypes. If a subtype is given, it is used and added as unsigned 8-bit integer. If no subtype is given, the generic binary subtype 0x00 is used.
[BSON](binary_formats/bson.md) supports binary values and subtypes. If a subtype is given, it is used and added as unsigned 8-bit integer. If no subtype is given, the generic binary subtype 0x00 is used.
!!! example
...
...
@@ -145,7 +158,7 @@ JSON does not have a binary type, and this library does not introduce a new type
### CBOR
[CBOR](binary_formats/cbor) supports binary values, but no subtypes. Any binary value will be serialized as byte strings. The library will choose the smallest representation using the length of the byte array.
[CBOR](binary_formats/cbor.md) supports binary values, but no subtypes. Any binary value will be serialized as byte strings. The library will choose the smallest representation using the length of the byte array.
!!! example
...
...
@@ -183,7 +196,7 @@ JSON does not have a binary type, and this library does not introduce a new type
### MessagePack
[MessagePack](binary_formats/messagepack) supports binary values and subtypes. If a subtype is given, the ext family is used. The library will choose the smallest representation among fixext1, fixext2, fixext4, fixext8, ext8, ext16, and ext32. The subtype is then added as singed 8-bit integer.
[MessagePack](binary_formats/messagepack.md) supports binary values and subtypes. If a subtype is given, the ext family is used. The library will choose the smallest representation among fixext1, fixext2, fixext4, fixext8, ext8, ext16, and ext32. The subtype is then added as singed 8-bit integer.
If no subtype is given, the bin family (bin8, bin16, bin32) is used.
...
...
@@ -224,7 +237,7 @@ If no subtype is given, the bin family (bin8, bin16, bin32) is used.
### UBJSON
[UBJSON](binary_formats/ubjson) neither supports binary values nor subtypes, and proposes to serialize binary values as array of uint8 values. This translation is implemented by the library.
[UBJSON](binary_formats/ubjson.md) neither supports binary values nor subtypes, and proposes to serialize binary values as array of uint8 values. This translation is implemented by the library.