mirror of
https://github.com/nlohmann/json.git
synced 2026-07-12 21:45:10 +00:00
Reorganize directories (#3462)
* 🦃 move files * 🦃 rename doc folder to docs * 🦃 rename test folder to tests
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
# BSON
|
||||
|
||||
BSON, short for Binary JSON, is a binary-encoded serialization of JSON-like documents. Like JSON, BSON supports the
|
||||
embedding of documents and arrays within other documents and arrays. BSON also contains extensions that allow
|
||||
representation of data types that are not part of the JSON spec. For example, BSON has a Date type and a BinData type.
|
||||
|
||||
!!! abstract "References"
|
||||
|
||||
- [BSON Website](http://bsonspec.org) - the main source on BSON
|
||||
- [BSON Specification](http://bsonspec.org/spec.html) - the specification
|
||||
|
||||
|
||||
## Serialization
|
||||
|
||||
The library uses the following mapping from JSON values types to BSON types:
|
||||
|
||||
| JSON value type | value/range | BSON type | marker |
|
||||
|-----------------|-------------------------------------------|-----------|--------|
|
||||
| null | `null` | null | 0x0A |
|
||||
| boolean | `true`, `false` | boolean | 0x08 |
|
||||
| number_integer | -9223372036854775808..-2147483649 | int64 | 0x12 |
|
||||
| number_integer | -2147483648..2147483647 | int32 | 0x10 |
|
||||
| number_integer | 2147483648..9223372036854775807 | int64 | 0x12 |
|
||||
| number_unsigned | 0..2147483647 | int32 | 0x10 |
|
||||
| number_unsigned | 2147483648..9223372036854775807 | int64 | 0x12 |
|
||||
| number_unsigned | 9223372036854775808..18446744073709551615 | -- | -- |
|
||||
| number_float | *any value* | double | 0x01 |
|
||||
| string | *any value* | string | 0x02 |
|
||||
| array | *any value* | document | 0x04 |
|
||||
| object | *any value* | document | 0x03 |
|
||||
| binary | *any value* | binary | 0x05 |
|
||||
|
||||
!!! warning "Incomplete mapping"
|
||||
|
||||
The mapping is **incomplete**, since only JSON-objects (and things
|
||||
contained therein) can be serialized to BSON.
|
||||
Also, integers larger than 9223372036854775807 cannot be serialized to BSON,
|
||||
and the keys may not contain U+0000, since they are serialized a
|
||||
zero-terminated c-strings.
|
||||
|
||||
??? example
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/to_bson.cpp"
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```c
|
||||
--8<-- "examples/to_bson.output"
|
||||
```
|
||||
|
||||
|
||||
## Deserialization
|
||||
|
||||
The library maps BSON record types to JSON value types as follows:
|
||||
|
||||
| BSON type | BSON marker byte | JSON value type |
|
||||
|-----------------------|------------------|-----------------|
|
||||
| double | 0x01 | number_float |
|
||||
| string | 0x02 | string |
|
||||
| document | 0x03 | object |
|
||||
| array | 0x04 | array |
|
||||
| binary | 0x05 | binary |
|
||||
| undefined | 0x06 | *unsupported* |
|
||||
| ObjectId | 0x07 | *unsupported* |
|
||||
| boolean | 0x08 | boolean |
|
||||
| UTC Date-Time | 0x09 | *unsupported* |
|
||||
| null | 0x0A | null |
|
||||
| Regular Expr. | 0x0B | *unsupported* |
|
||||
| DB Pointer | 0x0C | *unsupported* |
|
||||
| JavaScript Code | 0x0D | *unsupported* |
|
||||
| Symbol | 0x0E | *unsupported* |
|
||||
| JavaScript Code | 0x0F | *unsupported* |
|
||||
| int32 | 0x10 | number_integer |
|
||||
| Timestamp | 0x11 | *unsupported* |
|
||||
| 128-bit decimal float | 0x13 | *unsupported* |
|
||||
| Max Key | 0x7F | *unsupported* |
|
||||
| Min Key | 0xFF | *unsupported* |
|
||||
|
||||
!!! warning "Incomplete mapping"
|
||||
|
||||
The mapping is **incomplete**. The unsupported mappings are indicated in the table above.
|
||||
|
||||
|
||||
??? example
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/from_bson.cpp"
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
--8<-- "examples/from_bson.output"
|
||||
```
|
||||
@@ -0,0 +1,180 @@
|
||||
# CBOR
|
||||
|
||||
The Concise Binary Object Representation (CBOR) is a data format whose design goals include the possibility of extremely
|
||||
small code size, fairly small message size, and extensibility without the need for version negotiation.
|
||||
|
||||
!!! abstract "References"
|
||||
|
||||
- [CBOR Website](http://cbor.io) - the main source on CBOR
|
||||
- [CBOR Playground](http://cbor.me) - an interactive webpage to translate between JSON and CBOR
|
||||
- [RFC 7049](https://tools.ietf.org/html/rfc7049) - the CBOR specification
|
||||
|
||||
## Serialization
|
||||
|
||||
The library uses the following mapping from JSON values types to CBOR types according to the CBOR specification (RFC 7049):
|
||||
|
||||
| JSON value type | value/range | CBOR type | first byte |
|
||||
|-----------------|--------------------------------------------|-----------------------------------|------------|
|
||||
| null | `null` | Null | 0xF6 |
|
||||
| boolean | `true` | True | 0xF5 |
|
||||
| boolean | `false` | False | 0xF4 |
|
||||
| number_integer | -9223372036854775808..-2147483649 | Negative integer (8 bytes follow) | 0x3B |
|
||||
| number_integer | -2147483648..-32769 | Negative integer (4 bytes follow) | 0x3A |
|
||||
| number_integer | -32768..-129 | Negative integer (2 bytes follow) | 0x39 |
|
||||
| number_integer | -128..-25 | Negative integer (1 byte follow) | 0x38 |
|
||||
| number_integer | -24..-1 | Negative integer | 0x20..0x37 |
|
||||
| number_integer | 0..23 | Integer | 0x00..0x17 |
|
||||
| number_integer | 24..255 | Unsigned integer (1 byte follow) | 0x18 |
|
||||
| number_integer | 256..65535 | Unsigned integer (2 bytes follow) | 0x19 |
|
||||
| number_integer | 65536..4294967295 | Unsigned integer (4 bytes follow) | 0x1A |
|
||||
| number_integer | 4294967296..18446744073709551615 | Unsigned integer (8 bytes follow) | 0x1B |
|
||||
| number_unsigned | 0..23 | Integer | 0x00..0x17 |
|
||||
| number_unsigned | 24..255 | Unsigned integer (1 byte follow) | 0x18 |
|
||||
| number_unsigned | 256..65535 | Unsigned integer (2 bytes follow) | 0x19 |
|
||||
| number_unsigned | 65536..4294967295 | Unsigned integer (4 bytes follow) | 0x1A |
|
||||
| number_unsigned | 4294967296..18446744073709551615 | Unsigned integer (8 bytes follow) | 0x1B |
|
||||
| number_float | *any value representable by a float* | Single-Precision Float | 0xFA |
|
||||
| number_float | *any value NOT representable by a float* | Double-Precision Float | 0xFB |
|
||||
| string | *length*: 0..23 | UTF-8 string | 0x60..0x77 |
|
||||
| string | *length*: 23..255 | UTF-8 string (1 byte follow) | 0x78 |
|
||||
| string | *length*: 256..65535 | UTF-8 string (2 bytes follow) | 0x79 |
|
||||
| string | *length*: 65536..4294967295 | UTF-8 string (4 bytes follow) | 0x7A |
|
||||
| string | *length*: 4294967296..18446744073709551615 | UTF-8 string (8 bytes follow) | 0x7B |
|
||||
| array | *size*: 0..23 | array | 0x80..0x97 |
|
||||
| array | *size*: 23..255 | array (1 byte follow) | 0x98 |
|
||||
| array | *size*: 256..65535 | array (2 bytes follow) | 0x99 |
|
||||
| array | *size*: 65536..4294967295 | array (4 bytes follow) | 0x9A |
|
||||
| array | *size*: 4294967296..18446744073709551615 | array (8 bytes follow) | 0x9B |
|
||||
| object | *size*: 0..23 | map | 0xA0..0xB7 |
|
||||
| object | *size*: 23..255 | map (1 byte follow) | 0xB8 |
|
||||
| object | *size*: 256..65535 | map (2 bytes follow) | 0xB9 |
|
||||
| object | *size*: 65536..4294967295 | map (4 bytes follow) | 0xBA |
|
||||
| object | *size*: 4294967296..18446744073709551615 | map (8 bytes follow) | 0xBB |
|
||||
| binary | *size*: 0..23 | byte string | 0x40..0x57 |
|
||||
| binary | *size*: 23..255 | byte string (1 byte follow) | 0x58 |
|
||||
| binary | *size*: 256..65535 | byte string (2 bytes follow) | 0x59 |
|
||||
| binary | *size*: 65536..4294967295 | byte string (4 bytes follow) | 0x5A |
|
||||
| binary | *size*: 4294967296..18446744073709551615 | byte string (8 bytes follow) | 0x5B |
|
||||
|
||||
Binary values with subtype are mapped to tagged values (0xD8..0xDB) depending on the subtype, followed by a byte string,
|
||||
see "binary" cells in the table above.
|
||||
|
||||
!!! success "Complete mapping"
|
||||
|
||||
The mapping is **complete** in the sense that any JSON value type can be converted to a CBOR value.
|
||||
|
||||
!!! info "NaN/infinity handling"
|
||||
|
||||
If NaN or Infinity are stored inside a JSON number, they are serialized properly. This behavior differs from the normal JSON serialization which serializes NaN or Infinity to `null`.
|
||||
|
||||
!!! info "Unused CBOR types"
|
||||
|
||||
The following CBOR types are not used in the conversion:
|
||||
|
||||
- UTF-8 strings terminated by "break" (0x7F)
|
||||
- arrays terminated by "break" (0x9F)
|
||||
- maps terminated by "break" (0xBF)
|
||||
- byte strings terminated by "break" (0x5F)
|
||||
- date/time (0xC0..0xC1)
|
||||
- bignum (0xC2..0xC3)
|
||||
- decimal fraction (0xC4)
|
||||
- bigfloat (0xC5)
|
||||
- expected conversions (0xD5..0xD7)
|
||||
- simple values (0xE0..0xF3, 0xF8)
|
||||
- undefined (0xF7)
|
||||
- half-precision floats (0xF9)
|
||||
- break (0xFF)
|
||||
|
||||
!!! info "Tagged items"
|
||||
|
||||
Binary subtypes will be serialized as tagged items. See [binary values](../binary_values.md#cbor) for an example.
|
||||
|
||||
??? example
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/to_cbor.cpp"
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```c
|
||||
--8<-- "examples/to_cbor.output"
|
||||
```
|
||||
|
||||
## Deserialization
|
||||
|
||||
The library maps CBOR types to JSON value types as follows:
|
||||
|
||||
| CBOR type | JSON value type | first byte |
|
||||
|------------------------|-----------------|------------|
|
||||
| Integer | number_unsigned | 0x00..0x17 |
|
||||
| Unsigned integer | number_unsigned | 0x18 |
|
||||
| Unsigned integer | number_unsigned | 0x19 |
|
||||
| Unsigned integer | number_unsigned | 0x1A |
|
||||
| Unsigned integer | number_unsigned | 0x1B |
|
||||
| Negative integer | number_integer | 0x20..0x37 |
|
||||
| Negative integer | number_integer | 0x38 |
|
||||
| Negative integer | number_integer | 0x39 |
|
||||
| Negative integer | number_integer | 0x3A |
|
||||
| Negative integer | number_integer | 0x3B |
|
||||
| Byte string | binary | 0x40..0x57 |
|
||||
| Byte string | binary | 0x58 |
|
||||
| Byte string | binary | 0x59 |
|
||||
| Byte string | binary | 0x5A |
|
||||
| Byte string | binary | 0x5B |
|
||||
| UTF-8 string | string | 0x60..0x77 |
|
||||
| UTF-8 string | string | 0x78 |
|
||||
| UTF-8 string | string | 0x79 |
|
||||
| UTF-8 string | string | 0x7A |
|
||||
| UTF-8 string | string | 0x7B |
|
||||
| UTF-8 string | string | 0x7F |
|
||||
| array | array | 0x80..0x97 |
|
||||
| array | array | 0x98 |
|
||||
| array | array | 0x99 |
|
||||
| array | array | 0x9A |
|
||||
| array | array | 0x9B |
|
||||
| array | array | 0x9F |
|
||||
| map | object | 0xA0..0xB7 |
|
||||
| map | object | 0xB8 |
|
||||
| map | object | 0xB9 |
|
||||
| map | object | 0xBA |
|
||||
| map | object | 0xBB |
|
||||
| map | object | 0xBF |
|
||||
| False | `false` | 0xF4 |
|
||||
| True | `true` | 0xF5 |
|
||||
| Null | `null` | 0xF6 |
|
||||
| Half-Precision Float | number_float | 0xF9 |
|
||||
| Single-Precision Float | number_float | 0xFA |
|
||||
| Double-Precision Float | number_float | 0xFB |
|
||||
|
||||
!!! warning "Incomplete mapping"
|
||||
|
||||
The mapping is **incomplete** in the sense that not all CBOR types can be converted to a JSON value. The following CBOR types are not supported and will yield parse errors:
|
||||
|
||||
- date/time (0xC0..0xC1)
|
||||
- bignum (0xC2..0xC3)
|
||||
- decimal fraction (0xC4)
|
||||
- bigfloat (0xC5)
|
||||
- expected conversions (0xD5..0xD7)
|
||||
- simple values (0xE0..0xF3, 0xF8)
|
||||
- undefined (0xF7)
|
||||
|
||||
!!! warning "Object keys"
|
||||
|
||||
CBOR allows map keys of any type, whereas JSON only allows strings as keys in object values. Therefore, CBOR maps with keys other than UTF-8 strings are rejected.
|
||||
|
||||
!!! warning "Tagged items"
|
||||
|
||||
Tagged items will throw a parse error by default. They can be ignored by passing `cbor_tag_handler_t::ignore` to function `from_cbor`. They can be stored by passing `cbor_tag_handler_t::store` to function `from_cbor`.
|
||||
|
||||
??? example
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/from_cbor.cpp"
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
--8<-- "examples/from_cbor.output"
|
||||
```
|
||||
@@ -0,0 +1,45 @@
|
||||
# Binary Formats
|
||||
|
||||
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
|
||||
|
||||
- [BSON](bson.md) (Binary JSON),
|
||||
- [CBOR](cbor.md) (Concise Binary Object Representation),
|
||||
- [MessagePack](messagepack.md), and
|
||||
- [UBJSON](ubjson.md) (Universal Binary JSON)
|
||||
|
||||
to efficiently encode JSON values to byte vectors and to decode such vectors.
|
||||
|
||||
## Comparison
|
||||
|
||||
### Completeness
|
||||
|
||||
| Format | Serialization | Deserialization |
|
||||
|-------------|-----------------------------------------------|----------------------------------------------|
|
||||
| BSON | incomplete: top-level value must be an object | incomplete, but all JSON types are supported |
|
||||
| CBOR | complete | incomplete, but all JSON types are supported |
|
||||
| MessagePack | complete | complete |
|
||||
| UBJSON | complete | complete |
|
||||
|
||||
### Binary values
|
||||
|
||||
| Format | Binary values | Binary subtypes |
|
||||
|-------------|---------------|-----------------|
|
||||
| BSON | supported | supported |
|
||||
| CBOR | supported | supported |
|
||||
| MessagePack | supported | supported |
|
||||
| UBJSON | not supported | not supported |
|
||||
|
||||
See [binary values](../binary_values.md) for more information.
|
||||
|
||||
### Sizes
|
||||
|
||||
| Format | canada.json | twitter.json | citm_catalog.json | jeopardy.json |
|
||||
|--------------------|-------------|--------------|-------------------|---------------|
|
||||
| BSON | 85,8 % | 95,2 % | 95,8 % | 106,7 % |
|
||||
| CBOR | 50,5 % | 86,3 % | 68,4 % | 88,0 % |
|
||||
| MessagePack | 50,6 % | 86,0 % | 68,5 % | 87,9 % |
|
||||
| UBJSON | 53,2 % | 91,3 % | 78,2 % | 96,6 % |
|
||||
| UBJSON (size) | 58,6 % | 92,3 % | 86,8 % | 97,4 % |
|
||||
| UBJSON (size+type) | 55,9 % | 92,3 % | 85,0 % | 95,0 % |
|
||||
|
||||
Sizes compared to minified JSON value.
|
||||
@@ -0,0 +1,139 @@
|
||||
# MessagePack
|
||||
|
||||
MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON. But it's faster and smaller. Small integers are encoded into a single byte, and typical short strings require only one extra byte in addition to the strings themselves.
|
||||
|
||||
!!! abstract "References"
|
||||
|
||||
- [MessagePack website](https://msgpack.org)
|
||||
- [MessagePack specification](https://github.com/msgpack/msgpack/blob/master/spec.md)
|
||||
|
||||
## Serialization
|
||||
|
||||
The library uses the following mapping from JSON values types to MessagePack types according to the MessagePack specification:
|
||||
|
||||
| JSON value type | value/range | MessagePack type | first byte |
|
||||
|-----------------|------------------------------------------|------------------|------------|
|
||||
| null | `null` | nil | 0xC0 |
|
||||
| boolean | `true` | true | 0xC3 |
|
||||
| boolean | `false` | false | 0xC2 |
|
||||
| number_integer | -9223372036854775808..-2147483649 | int64 | 0xD3 |
|
||||
| number_integer | -2147483648..-32769 | int32 | 0xD2 |
|
||||
| number_integer | -32768..-129 | int16 | 0xD1 |
|
||||
| number_integer | -128..-33 | int8 | 0xD0 |
|
||||
| number_integer | -32..-1 | negative fixint | 0xE0..0xFF |
|
||||
| number_integer | 0..127 | positive fixint | 0x00..0x7F |
|
||||
| number_integer | 128..255 | uint 8 | 0xCC |
|
||||
| number_integer | 256..65535 | uint 16 | 0xCD |
|
||||
| number_integer | 65536..4294967295 | uint 32 | 0xCE |
|
||||
| number_integer | 4294967296..18446744073709551615 | uint 64 | 0xCF |
|
||||
| number_unsigned | 0..127 | positive fixint | 0x00..0x7F |
|
||||
| number_unsigned | 128..255 | uint 8 | 0xCC |
|
||||
| number_unsigned | 256..65535 | uint 16 | 0xCD |
|
||||
| number_unsigned | 65536..4294967295 | uint 32 | 0xCE |
|
||||
| number_unsigned | 4294967296..18446744073709551615 | uint 64 | 0xCF |
|
||||
| number_float | *any value representable by a float* | float 32 | 0xCA |
|
||||
| number_float | *any value NOT representable by a float* | float 64 | 0xCB |
|
||||
| string | *length*: 0..31 | fixstr | 0xA0..0xBF |
|
||||
| string | *length*: 32..255 | str 8 | 0xD9 |
|
||||
| string | *length*: 256..65535 | str 16 | 0xDA |
|
||||
| string | *length*: 65536..4294967295 | str 32 | 0xDB |
|
||||
| array | *size*: 0..15 | fixarray | 0x90..0x9F |
|
||||
| array | *size*: 16..65535 | array 16 | 0xDC |
|
||||
| array | *size*: 65536..4294967295 | array 32 | 0xDD |
|
||||
| object | *size*: 0..15 | fix map | 0x80..0x8F |
|
||||
| object | *size*: 16..65535 | map 16 | 0xDE |
|
||||
| object | *size*: 65536..4294967295 | map 32 | 0xDF |
|
||||
| binary | *size*: 0..255 | bin 8 | 0xC4 |
|
||||
| binary | *size*: 256..65535 | bin 16 | 0xC5 |
|
||||
| binary | *size*: 65536..4294967295 | bin 32 | 0xC6 |
|
||||
|
||||
!!! success "Complete mapping"
|
||||
|
||||
The mapping is **complete** in the sense that any JSON value type can be converted to a MessagePack value.
|
||||
|
||||
Any MessagePack output created by `to_msgpack` can be successfully parsed by `from_msgpack`.
|
||||
|
||||
!!! warning "Size constraints"
|
||||
|
||||
The following values can **not** be converted to a MessagePack value:
|
||||
|
||||
- strings with more than 4294967295 bytes
|
||||
- byte strings with more than 4294967295 bytes
|
||||
- arrays with more than 4294967295 elements
|
||||
- objects with more than 4294967295 elements
|
||||
|
||||
!!! info "NaN/infinity handling"
|
||||
|
||||
If NaN or Infinity are stored inside a JSON number, they are serialized properly. function which serializes NaN or Infinity to `null`.
|
||||
|
||||
??? example
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/to_msgpack.cpp"
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```c
|
||||
--8<-- "examples/to_msgpack.output"
|
||||
```
|
||||
|
||||
## Deserialization
|
||||
|
||||
The library maps MessagePack types to JSON value types as follows:
|
||||
|
||||
| MessagePack type | JSON value type | first byte |
|
||||
|------------------|-----------------|------------|
|
||||
| positive fixint | number_unsigned | 0x00..0x7F |
|
||||
| fixmap | object | 0x80..0x8F |
|
||||
| fixarray | array | 0x90..0x9F |
|
||||
| fixstr | string | 0xA0..0xBF |
|
||||
| nil | `null` | 0xC0 |
|
||||
| false | `false` | 0xC2 |
|
||||
| true | `true` | 0xC3 |
|
||||
| float 32 | number_float | 0xCA |
|
||||
| float 64 | number_float | 0xCB |
|
||||
| uint 8 | number_unsigned | 0xCC |
|
||||
| uint 16 | number_unsigned | 0xCD |
|
||||
| uint 32 | number_unsigned | 0xCE |
|
||||
| uint 64 | number_unsigned | 0xCF |
|
||||
| int 8 | number_integer | 0xD0 |
|
||||
| int 16 | number_integer | 0xD1 |
|
||||
| int 32 | number_integer | 0xD2 |
|
||||
| int 64 | number_integer | 0xD3 |
|
||||
| str 8 | string | 0xD9 |
|
||||
| str 16 | string | 0xDA |
|
||||
| str 32 | string | 0xDB |
|
||||
| array 16 | array | 0xDC |
|
||||
| array 32 | array | 0xDD |
|
||||
| map 16 | object | 0xDE |
|
||||
| map 32 | object | 0xDF |
|
||||
| bin 8 | binary | 0xC4 |
|
||||
| bin 16 | binary | 0xC5 |
|
||||
| bin 32 | binary | 0xC6 |
|
||||
| ext 8 | binary | 0xC7 |
|
||||
| ext 16 | binary | 0xC8 |
|
||||
| ext 32 | binary | 0xC9 |
|
||||
| fixext 1 | binary | 0xD4 |
|
||||
| fixext 2 | binary | 0xD5 |
|
||||
| fixext 4 | binary | 0xD6 |
|
||||
| fixext 8 | binary | 0xD7 |
|
||||
| fixext 16 | binary | 0xD8 |
|
||||
| negative fixint | number_integer | 0xE0-0xFF |
|
||||
|
||||
!!! info
|
||||
|
||||
Any MessagePack output created by `to_msgpack` can be successfully parsed by `from_msgpack`.
|
||||
|
||||
|
||||
??? example
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/from_msgpack.cpp"
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
--8<-- "examples/from_msgpack.output"
|
||||
```
|
||||
@@ -0,0 +1,133 @@
|
||||
# UBJSON
|
||||
|
||||
Universal Binary JSON (UBJSON) is a binary form directly imitating JSON, but requiring fewer bytes of data. It aims to achieve the generality of JSON, combined with being much easier to process than JSON.
|
||||
|
||||
!!! abstract "References"
|
||||
|
||||
- [UBJSON Website](http://ubjson.org)
|
||||
|
||||
## Serialization
|
||||
|
||||
The library uses the following mapping from JSON values types to UBJSON types according to the UBJSON specification:
|
||||
|
||||
| JSON value type | value/range | UBJSON type | marker |
|
||||
|-----------------|-----------------------------------|----------------|--------|
|
||||
| null | `null` | null | `Z` |
|
||||
| boolean | `true` | true | `T` |
|
||||
| boolean | `false` | false | `F` |
|
||||
| number_integer | -9223372036854775808..-2147483649 | int64 | `L` |
|
||||
| number_integer | -2147483648..-32769 | int32 | `l` |
|
||||
| number_integer | -32768..-129 | int16 | `I` |
|
||||
| number_integer | -128..127 | int8 | `i` |
|
||||
| number_integer | 128..255 | uint8 | `U` |
|
||||
| number_integer | 256..32767 | int16 | `I` |
|
||||
| number_integer | 32768..2147483647 | int32 | `l` |
|
||||
| number_integer | 2147483648..9223372036854775807 | int64 | `L` |
|
||||
| number_unsigned | 0..127 | int8 | `i` |
|
||||
| number_unsigned | 128..255 | uint8 | `U` |
|
||||
| number_unsigned | 256..32767 | int16 | `I` |
|
||||
| number_unsigned | 32768..2147483647 | int32 | `l` |
|
||||
| number_unsigned | 2147483648..9223372036854775807 | int64 | `L` |
|
||||
| number_unsigned | 2147483649..18446744073709551615 | high-precision | `H` |
|
||||
| number_float | *any value* | float64 | `D` |
|
||||
| string | *with shortest length indicator* | string | `S` |
|
||||
| array | *see notes on optimized format* | array | `[` |
|
||||
| object | *see notes on optimized format* | map | `{` |
|
||||
|
||||
!!! success "Complete mapping"
|
||||
|
||||
The mapping is **complete** in the sense that any JSON value type can be converted to a UBJSON value.
|
||||
|
||||
Any UBJSON output created by `to_ubjson` can be successfully parsed by `from_ubjson`.
|
||||
|
||||
!!! warning "Size constraints"
|
||||
|
||||
The following values can **not** be converted to a UBJSON value:
|
||||
|
||||
- strings with more than 9223372036854775807 bytes (theoretical)
|
||||
|
||||
!!! info "Unused UBJSON markers"
|
||||
|
||||
The following markers are not used in the conversion:
|
||||
|
||||
- `Z`: no-op values are not created.
|
||||
- `C`: single-byte strings are serialized with `S` markers.
|
||||
|
||||
!!! info "NaN/infinity handling"
|
||||
|
||||
If NaN or Infinity are stored inside a JSON number, they are
|
||||
serialized properly. This behavior differs from the `dump()`
|
||||
function which serializes NaN or Infinity to `null`.
|
||||
|
||||
!!! info "Optimized formats"
|
||||
|
||||
The optimized formats for containers are supported: Parameter
|
||||
`use_size` adds size information to the beginning of a container and
|
||||
removes the closing marker. Parameter `use_type` further checks
|
||||
whether all elements of a container have the same type and adds the
|
||||
type marker to the beginning of the container. The `use_type`
|
||||
parameter must only be used together with `use_size = true`.
|
||||
|
||||
Note that `use_size = true` alone may result in larger representations -
|
||||
the benefit of this parameter is that the receiving side is
|
||||
immediately informed on the number of elements of the container.
|
||||
|
||||
!!! info "Binary values"
|
||||
|
||||
If the JSON data contains the binary type, the value stored is a list
|
||||
of integers, as suggested by the UBJSON documentation. In particular,
|
||||
this means that serialization and the deserialization of a JSON
|
||||
containing binary values into UBJSON and back will result in a
|
||||
different JSON object.
|
||||
|
||||
|
||||
??? example
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/to_ubjson.cpp"
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```c
|
||||
--8<-- "examples/to_ubjson.output"
|
||||
```
|
||||
|
||||
## Deserialization
|
||||
|
||||
The library maps UBJSON types to JSON value types as follows:
|
||||
|
||||
| UBJSON type | JSON value type | marker |
|
||||
|-------------|-----------------------------------------|--------|
|
||||
| no-op | *no value, next value is read* | `N` |
|
||||
| null | `null` | `Z` |
|
||||
| false | `false` | `F` |
|
||||
| true | `true` | `T` |
|
||||
| float32 | number_float | `d` |
|
||||
| float64 | number_float | `D` |
|
||||
| uint8 | number_unsigned | `U` |
|
||||
| int8 | number_integer | `i` |
|
||||
| int16 | number_integer | `I` |
|
||||
| int32 | number_integer | `l` |
|
||||
| int64 | number_integer | `L` |
|
||||
| string | string | `S` |
|
||||
| char | string | `C` |
|
||||
| array | array (optimized values are supported) | `[` |
|
||||
| object | object (optimized values are supported) | `{` |
|
||||
|
||||
!!! success "Complete mapping"
|
||||
|
||||
The mapping is **complete** in the sense that any UBJSON value can be converted to a JSON value.
|
||||
|
||||
|
||||
??? example
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/from_ubjson.cpp"
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
--8<-- "examples/from_ubjson.output"
|
||||
```
|
||||
Reference in New Issue
Block a user