mirror of
https://github.com/nlohmann/json.git
synced 2025-07-29 23:01:16 +03:00
📝 add mkdocs
This commit is contained in:
223
doc/mkdocs/docs/features/arbitrary_types.md
Normal file
223
doc/mkdocs/docs/features/arbitrary_types.md
Normal file
@ -0,0 +1,223 @@
|
||||
# Arbitrary Types Conversions
|
||||
|
||||
Every type can be serialized in JSON, not just STL containers and scalar types. Usually, you would do something along those lines:
|
||||
|
||||
```cpp
|
||||
namespace ns {
|
||||
// a simple struct to model a person
|
||||
struct person {
|
||||
std::string name;
|
||||
std::string address;
|
||||
int age;
|
||||
};
|
||||
}
|
||||
|
||||
ns::person p = {"Ned Flanders", "744 Evergreen Terrace", 60};
|
||||
|
||||
// convert to JSON: copy each value into the JSON object
|
||||
json j;
|
||||
j["name"] = p.name;
|
||||
j["address"] = p.address;
|
||||
j["age"] = p.age;
|
||||
|
||||
// ...
|
||||
|
||||
// convert from JSON: copy each value from the JSON object
|
||||
ns::person p {
|
||||
j["name"].get<std::string>(),
|
||||
j["address"].get<std::string>(),
|
||||
j["age"].get<int>()
|
||||
};
|
||||
```
|
||||
|
||||
It works, but that's quite a lot of boilerplate... Fortunately, there's a better way:
|
||||
|
||||
```cpp
|
||||
// create a person
|
||||
ns::person p {"Ned Flanders", "744 Evergreen Terrace", 60};
|
||||
|
||||
// conversion: person -> json
|
||||
json j = p;
|
||||
|
||||
std::cout << j << std::endl;
|
||||
// {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"}
|
||||
|
||||
// conversion: json -> person
|
||||
auto p2 = j.get<ns::person>();
|
||||
|
||||
// that's it
|
||||
assert(p == p2);
|
||||
```
|
||||
|
||||
## Basic usage
|
||||
|
||||
To make this work with one of your types, you only need to provide two functions:
|
||||
|
||||
```cpp
|
||||
using nlohmann::json;
|
||||
|
||||
namespace ns {
|
||||
void to_json(json& j, const person& p) {
|
||||
j = json{ {"name", p.name}, {"address", p.address}, {"age", p.age} };
|
||||
}
|
||||
|
||||
void from_json(const json& j, person& p) {
|
||||
j.at("name").get_to(p.name);
|
||||
j.at("address").get_to(p.address);
|
||||
j.at("age").get_to(p.age);
|
||||
}
|
||||
} // namespace ns
|
||||
```
|
||||
|
||||
That's all! When calling the `json` constructor with your type, your custom `to_json` method will be automatically called.
|
||||
Likewise, when calling `get<your_type>()` or `get_to(your_type&)`, the `from_json` method will be called.
|
||||
|
||||
Some important things:
|
||||
|
||||
* Those methods **MUST** be in your type's namespace (which can be the global namespace), or the library will not be able to locate them (in this example, they are in namespace `ns`, where `person` is defined).
|
||||
* Those methods **MUST** be available (e.g., proper headers must be included) everywhere you use these conversions. Look at [issue 1108](https://github.com/nlohmann/json/issues/1108) for errors that may occur otherwise.
|
||||
* When using `get<your_type>()`, `your_type` **MUST** be [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible). (There is a way to bypass this requirement described later.)
|
||||
* In function `from_json`, use function [`at()`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a93403e803947b86f4da2d1fb3345cf2c.html#a93403e803947b86f4da2d1fb3345cf2c) to access the object values rather than `operator[]`. In case a key does not exist, `at` throws an exception that you can handle, whereas `operator[]` exhibits undefined behavior.
|
||||
* You do not need to add serializers or deserializers for STL types like `std::vector`: the library already implements these.
|
||||
|
||||
|
||||
## How do I convert third-party types?
|
||||
|
||||
This requires a bit more advanced technique. But first, let's see how this conversion mechanism works:
|
||||
|
||||
The library uses **JSON Serializers** to convert types to json.
|
||||
The default serializer for `nlohmann::json` is `nlohmann::adl_serializer` (ADL means [Argument-Dependent Lookup](https://en.cppreference.com/w/cpp/language/adl)).
|
||||
|
||||
It is implemented like this (simplified):
|
||||
|
||||
```cpp
|
||||
template <typename T>
|
||||
struct adl_serializer {
|
||||
static void to_json(json& j, const T& value) {
|
||||
// calls the "to_json" method in T's namespace
|
||||
}
|
||||
|
||||
static void from_json(const json& j, T& value) {
|
||||
// same thing, but with the "from_json" method
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
This serializer works fine when you have control over the type's namespace. However, what about `boost::optional` or `std::filesystem::path` (C++17)? Hijacking the `boost` namespace is pretty bad, and it's illegal to add something other than template specializations to `std`...
|
||||
|
||||
To solve this, you need to add a specialization of `adl_serializer` to the `nlohmann` namespace, here's an example:
|
||||
|
||||
```cpp
|
||||
// partial specialization (full specialization works too)
|
||||
namespace nlohmann {
|
||||
template <typename T>
|
||||
struct adl_serializer<boost::optional<T>> {
|
||||
static void to_json(json& j, const boost::optional<T>& opt) {
|
||||
if (opt == boost::none) {
|
||||
j = nullptr;
|
||||
} else {
|
||||
j = *opt; // this will call adl_serializer<T>::to_json which will
|
||||
// find the free function to_json in T's namespace!
|
||||
}
|
||||
}
|
||||
|
||||
static void from_json(const json& j, boost::optional<T>& opt) {
|
||||
if (j.is_null()) {
|
||||
opt = boost::none;
|
||||
} else {
|
||||
opt = j.get<T>(); // same as above, but with
|
||||
// adl_serializer<T>::from_json
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
## How can I use `get()` for non-default constructible/non-copyable types?
|
||||
|
||||
There is a way, if your type is [MoveConstructible](https://en.cppreference.com/w/cpp/named_req/MoveConstructible). You will need to specialize the `adl_serializer` as well, but with a special `from_json` overload:
|
||||
|
||||
```cpp
|
||||
struct move_only_type {
|
||||
move_only_type() = delete;
|
||||
move_only_type(int ii): i(ii) {}
|
||||
move_only_type(const move_only_type&) = delete;
|
||||
move_only_type(move_only_type&&) = default;
|
||||
|
||||
int i;
|
||||
};
|
||||
|
||||
namespace nlohmann {
|
||||
template <>
|
||||
struct adl_serializer<move_only_type> {
|
||||
// note: the return type is no longer 'void', and the method only takes
|
||||
// one argument
|
||||
static move_only_type from_json(const json& j) {
|
||||
return {j.get<int>()};
|
||||
}
|
||||
|
||||
// Here's the catch! You must provide a to_json method! Otherwise you
|
||||
// will not be able to convert move_only_type to json, since you fully
|
||||
// specialized adl_serializer on that type
|
||||
static void to_json(json& j, move_only_type t) {
|
||||
j = t.i;
|
||||
}
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
## Can I write my own serializer? (Advanced use)
|
||||
|
||||
Yes. You might want to take a look at [`unit-udt.cpp`](https://github.com/nlohmann/json/blob/develop/test/src/unit-udt.cpp) in the test suite, to see a few examples.
|
||||
|
||||
If you write your own serializer, you'll need to do a few things:
|
||||
|
||||
- use a different `basic_json` alias than `nlohmann::json` (the last template parameter of `basic_json` is the `JSONSerializer`)
|
||||
- use your `basic_json` alias (or a template parameter) in all your `to_json`/`from_json` methods
|
||||
- use `nlohmann::to_json` and `nlohmann::from_json` when you need ADL
|
||||
|
||||
Here is an example, without simplifications, that only accepts types with a size <= 32, and uses ADL.
|
||||
|
||||
```cpp
|
||||
// You should use void as a second template argument
|
||||
// if you don't need compile-time checks on T
|
||||
template<typename T, typename SFINAE = typename std::enable_if<sizeof(T) <= 32>::type>
|
||||
struct less_than_32_serializer {
|
||||
template <typename BasicJsonType>
|
||||
static void to_json(BasicJsonType& j, T value) {
|
||||
// we want to use ADL, and call the correct to_json overload
|
||||
using nlohmann::to_json; // this method is called by adl_serializer,
|
||||
// this is where the magic happens
|
||||
to_json(j, value);
|
||||
}
|
||||
|
||||
template <typename BasicJsonType>
|
||||
static void from_json(const BasicJsonType& j, T& value) {
|
||||
// same thing here
|
||||
using nlohmann::from_json;
|
||||
from_json(j, value);
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
Be **very** careful when reimplementing your serializer, you can stack overflow if you don't pay attention:
|
||||
|
||||
```cpp
|
||||
template <typename T, void>
|
||||
struct bad_serializer
|
||||
{
|
||||
template <typename BasicJsonType>
|
||||
static void to_json(BasicJsonType& j, const T& value) {
|
||||
// this calls BasicJsonType::json_serializer<T>::to_json(j, value);
|
||||
// if BasicJsonType::json_serializer == bad_serializer ... oops!
|
||||
j = value;
|
||||
}
|
||||
|
||||
template <typename BasicJsonType>
|
||||
static void to_json(const BasicJsonType& j, T& value) {
|
||||
// this calls BasicJsonType::json_serializer<T>::from_json(j, value);
|
||||
// if BasicJsonType::json_serializer == bad_serializer ... oops!
|
||||
value = j.template get<T>(); // oops!
|
||||
}
|
||||
};
|
||||
```
|
94
doc/mkdocs/docs/features/binary_formats/bson.md
Normal file
94
doc/mkdocs/docs/features/binary_formats/bson.md
Normal file
@ -0,0 +1,94 @@
|
||||
# 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"
|
||||
```
|
172
doc/mkdocs/docs/features/binary_formats/cbor.md
Normal file
172
doc/mkdocs/docs/features/binary_formats/cbor.md
Normal file
@ -0,0 +1,172 @@
|
||||
# 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
|
||||
|
||||
|
||||
!!! 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)
|
||||
- tagged items (0xC6..0xD4, 0xD8..0xDB)
|
||||
- expected conversions (0xD5..0xD7)
|
||||
- simple values (0xE0..0xF3, 0xF8)
|
||||
- undefined (0xF7)
|
||||
- half-precision floats (0xF9)
|
||||
- break (0xFF)
|
||||
|
||||
!!! 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)
|
||||
- tagged items (0xC6..0xD4, 0xD8..0xDB)
|
||||
- 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.
|
||||
|
||||
!!! example
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/from_cbor.cpp"
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
--8<-- "examples/from_cbor.output"
|
||||
```
|
41
doc/mkdocs/docs/features/binary_formats/index.md
Normal file
41
doc/mkdocs/docs/features/binary_formats/index.md
Normal file
@ -0,0 +1,41 @@
|
||||
# Overview
|
||||
|
||||
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) (Binary JSON),
|
||||
- [CBOR](cbor) (Concise Binary Object Representation),
|
||||
- [MessagePack](messagepack), and
|
||||
- [UBJSON](ubjson) (Universal Binary JSON Specification)
|
||||
|
||||
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 | not supported |
|
||||
| MessagePack | supported | supported |
|
||||
| UBJSON | not supported | not supported |
|
||||
|
||||
### 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 % |
|
142
doc/mkdocs/docs/features/binary_formats/messagepack.md
Normal file
142
doc/mkdocs/docs/features/binary_formats/messagepack.md
Normal file
@ -0,0 +1,142 @@
|
||||
# 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* | 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 "Unused MessagePack types"
|
||||
|
||||
The following MessagePack types are not used in the conversion: float 32 (0xCA)
|
||||
|
||||
!!! 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"
|
||||
```
|
133
doc/mkdocs/docs/features/binary_formats/ubjson.md
Normal file
133
doc/mkdocs/docs/features/binary_formats/ubjson.md
Normal file
@ -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_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)
|
||||
- unsigned integer numbers above 9223372036854775807
|
||||
|
||||
!!! 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"
|
||||
```
|
281
doc/mkdocs/docs/features/binary_values.md
Normal file
281
doc/mkdocs/docs/features/binary_values.md
Normal file
@ -0,0 +1,281 @@
|
||||
# Binary Values
|
||||
|
||||
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.
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
auto binary = json::binary_t({0xCA, 0xFE, 0xBA, 0xBE});
|
||||
auto binary_with_subtype = json::binary_t({0xCA, 0xFE, 0xBA, 0xBE}, 42);
|
||||
```
|
||||
|
||||
There are several convenience functions to check and set the subtype:
|
||||
|
||||
```cpp
|
||||
binary.has_subtype(); // returns false
|
||||
binary_with_subtype.has_subtype(); // returns true
|
||||
|
||||
binary_with_subtype.clear_subtype();
|
||||
binary_with_subtype.has_subtype(); // returns true
|
||||
|
||||
binary_with_subtype.set_subtype(42);
|
||||
binary.set_subtype(23);
|
||||
|
||||
binary.subtype(); // returns 23
|
||||
```
|
||||
|
||||
As `json::binary_t` is subclassing `std::vector<std::uint8_t>`, all member functions are available:
|
||||
|
||||
```cpp
|
||||
binary.size(); // returns 4
|
||||
binary[1]; // returns 0xFE
|
||||
```
|
||||
|
||||
JSON values can be constructed from `json::binary_t`:
|
||||
|
||||
```cpp
|
||||
json j = binary;
|
||||
```
|
||||
|
||||
Binary values are primitive values just like numbers or strings:
|
||||
|
||||
```cpp
|
||||
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()`:
|
||||
|
||||
```cpp
|
||||
j.get_binary().has_subtype(); // returns true
|
||||
j.get_binary().size(); // returns 4
|
||||
```
|
||||
|
||||
For convencience, binary JSON values can be constructed via `json::binary`:
|
||||
|
||||
```cpp
|
||||
auto j2 = json::binary({0xCA, 0xFE, 0xBA, 0xBE}, 23);
|
||||
auto j3 = json::binary({0xCA, 0xFE, 0xBA, 0xBE});
|
||||
|
||||
j2 == j; // returns true
|
||||
j3.get_binary().has_subtype(); // returns false
|
||||
```
|
||||
|
||||
|
||||
|
||||
## Serialization
|
||||
|
||||
Binary values are serialized differently according to the formats.
|
||||
|
||||
### JSON
|
||||
|
||||
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
|
||||
|
||||
Code:
|
||||
|
||||
```cpp
|
||||
// create a binary value of subtype 42
|
||||
json j;
|
||||
j["binary"] = json::binary({0xCA, 0xFE, 0xBA, 0xBE}, 42);
|
||||
|
||||
// serialize to standard output
|
||||
std::cout << j.dump(2) << std::endl;
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
{
|
||||
"binary": {
|
||||
"bytes": [202, 254, 186, 190],
|
||||
"subtype": 42
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
!!! warning "No roundtrip for binary values"
|
||||
|
||||
The JSON parser will not parse the objects generated by binary values back to binary values. This is by design to remain standards compliant. Serializing binary values to JSON is only implemented for debugging purposes.
|
||||
|
||||
### 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.
|
||||
|
||||
!!! example
|
||||
|
||||
Code:
|
||||
|
||||
```cpp
|
||||
// create a binary value of subtype 42
|
||||
json j;
|
||||
j["binary"] = json::binary({0xCA, 0xFE, 0xBA, 0xBE}, 42);
|
||||
|
||||
// convert to BSON
|
||||
auto v = json::to_bson(j);
|
||||
```
|
||||
|
||||
`v` is a `std::vector<std::uint8t>` with the following 22 elements:
|
||||
|
||||
```c
|
||||
0x16 0x00 0x00 0x00 // number of bytes in the document
|
||||
0x05 // binary value
|
||||
0x62 0x69 0x6E 0x61 0x72 0x79 0x00 // key "binary" + null byte
|
||||
0x04 0x00 0x00 0x00 // number of bytes
|
||||
0x2a // subtype
|
||||
0xCA 0xFE 0xBA 0xBE // content
|
||||
0x00 // end of the document
|
||||
```
|
||||
|
||||
Note that the serialization preserves the subtype, and deserializing `v` would yield the following value:
|
||||
|
||||
```json
|
||||
{
|
||||
"binary": {
|
||||
"bytes": [202, 254, 186, 190],
|
||||
"subtype": 42
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 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.
|
||||
|
||||
!!! example
|
||||
|
||||
Code:
|
||||
|
||||
```cpp
|
||||
// create a binary value of subtype 42 (will be ignored by CBOR)
|
||||
json j;
|
||||
j["binary"] = json::binary({0xCA, 0xFE, 0xBA, 0xBE}, 42);
|
||||
|
||||
// convert to CBOR
|
||||
auto v = json::to_cbor(j);
|
||||
```
|
||||
|
||||
`v` is a `std::vector<std::uint8t>` with the following 13 elements:
|
||||
|
||||
```c
|
||||
0xA1 // map(1)
|
||||
0x66 // text(6)
|
||||
0x62 0x69 0x6E 0x61 0x72 0x79 // "binary"
|
||||
0x44 // bytes(4)
|
||||
0xCA 0xFE 0xBA 0xBE // content
|
||||
```
|
||||
|
||||
Note the subtype (42) is **not** serialized, and deserializing `v` would yield the following value:
|
||||
|
||||
```json
|
||||
{
|
||||
"binary": {
|
||||
"bytes": [202, 254, 186, 190],
|
||||
"subtype": null
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 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.
|
||||
|
||||
If no subtype is given, the bin family (bin8, bin16, bin32) is used.
|
||||
|
||||
!!! example
|
||||
|
||||
Code:
|
||||
|
||||
```cpp
|
||||
// create a binary value of subtype 42
|
||||
json j;
|
||||
j["binary"] = json::binary({0xCA, 0xFE, 0xBA, 0xBE}, 42);
|
||||
|
||||
// convert to MessagePack
|
||||
auto v = json::to_msgpack(j);
|
||||
```
|
||||
|
||||
`v` is a `std::vector<std::uint8t>` with the following 14 elements:
|
||||
|
||||
```c
|
||||
0x81 // fixmap1
|
||||
0xA6 // fixstr6
|
||||
0x62 0x69 0x6E 0x61 0x72 0x79 // "binary"
|
||||
0xD6 // fixext4
|
||||
0x2A // subtype
|
||||
0xCA 0xFE 0xBA 0xBE // content
|
||||
```
|
||||
|
||||
Note that the serialization preserves the subtype, and deserializing `v` would yield the following value:
|
||||
|
||||
```json
|
||||
{
|
||||
"binary": {
|
||||
"bytes": [202, 254, 186, 190],
|
||||
"subtype": 42
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 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.
|
||||
|
||||
!!! example
|
||||
|
||||
Code:
|
||||
|
||||
```cpp
|
||||
// create a binary value of subtype 42 (will be ignored in UBJSON)
|
||||
json j;
|
||||
j["binary"] = json::binary({0xCA, 0xFE, 0xBA, 0xBE}, 42);
|
||||
|
||||
// convert to UBJSON
|
||||
auto v = json::to_msgpack(j);
|
||||
```
|
||||
|
||||
`v` is a `std::vector<std::uint8t>` with the following 20 elements:
|
||||
|
||||
```c
|
||||
0x7B // '{'
|
||||
0x69 0x06 // i 6 (length of the key)
|
||||
0x62 0x69 0x6E 0x61 0x72 0x79 // "binary"
|
||||
0x5B // '['
|
||||
0x55 0xCA 0x55 0xFE 0x55 0xBA 0x55 0xBE // content (each byte prefixed with 'U')
|
||||
0x5D // ']'
|
||||
0x7D // '}'
|
||||
```
|
||||
|
||||
The following code uses the type and size optimization for UBJSON:
|
||||
|
||||
```cpp
|
||||
// convert to UBJSON using the size and type optimization
|
||||
auto v = json::to_ubjson(j, true, true);
|
||||
```
|
||||
|
||||
The resulting vector has 23 elements; the optimization is not effective for examples with few values:
|
||||
|
||||
```c
|
||||
0x7B // '{'
|
||||
0x24 // '$' type of the object elements
|
||||
0x5B // '[' array
|
||||
0x23 0x69 0x01 // '#' i 1 number of object elements
|
||||
0x69 0x06 // i 6 (length of the key)
|
||||
0x62 0x69 0x6E 0x61 0x72 0x79 // "binary"
|
||||
0x24 0x55 // '$' 'U' type of the array elements: unsinged integers
|
||||
0x23 0x69 0x04 // '#' i 4 number of array elements
|
||||
0xCA 0xFE 0xBA 0xBE // content
|
||||
```
|
||||
|
||||
Note that subtype (42) is **not** serialized and that UBJSON has **no binary type**, and deserializing `v` would yield the following value:
|
||||
|
||||
```json
|
||||
{
|
||||
"binary": [202, 254, 186, 190]
|
||||
}
|
||||
```
|
53
doc/mkdocs/docs/features/enum_conversion.md
Normal file
53
doc/mkdocs/docs/features/enum_conversion.md
Normal file
@ -0,0 +1,53 @@
|
||||
# Specializing enum conversion
|
||||
|
||||
By default, enum values are serialized to JSON as integers. In some cases this could result in undesired behavior. If an enum is modified or re-ordered after data has been serialized to JSON, the later de-serialized JSON data may be undefined or a different enum value than was originally intended.
|
||||
|
||||
It is possible to more precisely specify how a given enum is mapped to and from JSON as shown below:
|
||||
|
||||
```cpp
|
||||
// example enum type declaration
|
||||
enum TaskState {
|
||||
TS_STOPPED,
|
||||
TS_RUNNING,
|
||||
TS_COMPLETED,
|
||||
TS_INVALID=-1,
|
||||
};
|
||||
|
||||
// map TaskState values to JSON as strings
|
||||
NLOHMANN_JSON_SERIALIZE_ENUM( TaskState, {
|
||||
{TS_INVALID, nullptr},
|
||||
{TS_STOPPED, "stopped"},
|
||||
{TS_RUNNING, "running"},
|
||||
{TS_COMPLETED, "completed"},
|
||||
})
|
||||
```
|
||||
|
||||
The `NLOHMANN_JSON_SERIALIZE_ENUM()` macro declares a set of `to_json()` / `from_json()` functions for type `TaskState` while avoiding repetition and boilerplate serialization code.
|
||||
|
||||
## Usage
|
||||
|
||||
```cpp
|
||||
// enum to JSON as string
|
||||
json j = TS_STOPPED;
|
||||
assert(j == "stopped");
|
||||
|
||||
// json string to enum
|
||||
json j3 = "running";
|
||||
assert(j3.get<TaskState>() == TS_RUNNING);
|
||||
|
||||
// undefined json value to enum (where the first map entry above is the default)
|
||||
json jPi = 3.14;
|
||||
assert(jPi.get<TaskState>() == TS_INVALID );
|
||||
```
|
||||
|
||||
## Notes
|
||||
|
||||
Just as in [Arbitrary Type Conversions](#arbitrary-types-conversions) above,
|
||||
|
||||
- `NLOHMANN_JSON_SERIALIZE_ENUM()` MUST be declared in your enum type's namespace (which can be the global namespace), or the library will not be able to locate it and it will default to integer serialization.
|
||||
- It MUST be available (e.g., proper headers must be included) everywhere you use the conversions.
|
||||
|
||||
Other Important points:
|
||||
|
||||
- When using `get<ENUM_TYPE>()`, undefined JSON values will default to the first pair specified in your map. Select this default pair carefully.
|
||||
- If an enum or JSON value is specified more than once in your map, the first matching occurrence from the top of the map will be returned when converting to or from JSON.
|
28
doc/mkdocs/docs/features/json_patch.md
Normal file
28
doc/mkdocs/docs/features/json_patch.md
Normal file
@ -0,0 +1,28 @@
|
||||
# JSON Patch
|
||||
|
||||
On top of this, **JSON Patch** ([RFC 6902](https://tools.ietf.org/html/rfc6902)) allows to describe differences between two JSON values - effectively allowing patch and diff operations known from Unix.
|
||||
|
||||
```cpp
|
||||
|
||||
// a JSON patch (RFC 6902)
|
||||
json j_patch = R"([
|
||||
{ "op": "replace", "path": "/baz", "value": "boo" },
|
||||
{ "op": "add", "path": "/hello", "value": ["world"] },
|
||||
{ "op": "remove", "path": "/foo"}
|
||||
])"_json;
|
||||
|
||||
// apply the patch
|
||||
json j_result = j_original.patch(j_patch);
|
||||
// {
|
||||
// "baz": "boo",
|
||||
// "hello": ["world"]
|
||||
// }
|
||||
|
||||
// calculate a JSON patch from two JSON values
|
||||
json::diff(j_result, j_original);
|
||||
// [
|
||||
// { "op":" replace", "path": "/baz", "value": ["one", "two", "three"] },
|
||||
// { "op": "remove","path": "/hello" },
|
||||
// { "op": "add", "path": "/foo", "value": "bar" }
|
||||
// ]
|
||||
```
|
15
doc/mkdocs/docs/features/json_pointer.md
Normal file
15
doc/mkdocs/docs/features/json_pointer.md
Normal file
@ -0,0 +1,15 @@
|
||||
# JSON Pointer
|
||||
|
||||
The library supports **JSON Pointer** ([RFC 6901](https://tools.ietf.org/html/rfc6901)) as alternative means to address structured values.
|
||||
|
||||
```cpp
|
||||
// a JSON value
|
||||
json j_original = R"({
|
||||
"baz": ["one", "two", "three"],
|
||||
"foo": "bar"
|
||||
})"_json;
|
||||
|
||||
// access members with a JSON pointer (RFC 6901)
|
||||
j_original["/baz/1"_json_pointer];
|
||||
// "two"
|
||||
```
|
31
doc/mkdocs/docs/features/merge_patch.md
Normal file
31
doc/mkdocs/docs/features/merge_patch.md
Normal file
@ -0,0 +1,31 @@
|
||||
# JSON Merge Patch
|
||||
|
||||
The library supports **JSON Merge Patch** ([RFC 7386](https://tools.ietf.org/html/rfc7386)) as a patch format. Instead of using JSON Pointer (see above) to specify values to be manipulated, it describes the changes using a syntax that closely mimics the document being modified.
|
||||
|
||||
```cpp
|
||||
// a JSON value
|
||||
json j_document = R"({
|
||||
"a": "b",
|
||||
"c": {
|
||||
"d": "e",
|
||||
"f": "g"
|
||||
}
|
||||
})"_json;
|
||||
|
||||
// a patch
|
||||
json j_patch = R"({
|
||||
"a":"z",
|
||||
"c": {
|
||||
"f": null
|
||||
}
|
||||
})"_json;
|
||||
|
||||
// apply the patch
|
||||
j_document.merge_patch(j_patch);
|
||||
// {
|
||||
// "a": "z",
|
||||
// "c": {
|
||||
// "d": "e"
|
||||
// }
|
||||
// }
|
||||
```
|
42
doc/mkdocs/docs/features/sax_interface.md
Normal file
42
doc/mkdocs/docs/features/sax_interface.md
Normal file
@ -0,0 +1,42 @@
|
||||
# SAX Interface
|
||||
|
||||
The library uses a SAX-like interface with the following functions:
|
||||
|
||||
```cpp
|
||||
// called when null is parsed
|
||||
bool null();
|
||||
|
||||
// called when a boolean is parsed; value is passed
|
||||
bool boolean(bool val);
|
||||
|
||||
// called when a signed or unsigned integer number is parsed; value is passed
|
||||
bool number_integer(number_integer_t val);
|
||||
bool number_unsigned(number_unsigned_t val);
|
||||
|
||||
// called when a floating-point number is parsed; value and original string is passed
|
||||
bool number_float(number_float_t val, const string_t& s);
|
||||
|
||||
// called when a string is parsed; value is passed and can be safely moved away
|
||||
bool string(string_t& val);
|
||||
|
||||
// called when an object or array begins or ends, resp. The number of elements is passed (or -1 if not known)
|
||||
bool start_object(std::size_t elements);
|
||||
bool end_object();
|
||||
bool start_array(std::size_t elements);
|
||||
bool end_array();
|
||||
// called when an object key is parsed; value is passed and can be safely moved away
|
||||
bool key(string_t& val);
|
||||
|
||||
// called when a parse error occurs; byte position, the last token, and an exception is passed
|
||||
bool parse_error(std::size_t position, const std::string& last_token, const detail::exception& ex);
|
||||
```
|
||||
|
||||
The return value of each function determines whether parsing should proceed.
|
||||
|
||||
To implement your own SAX handler, proceed as follows:
|
||||
|
||||
1. Implement the SAX interface in a class. You can use class `nlohmann::json_sax<json>` as base class, but you can also use any class where the functions described above are implemented and public.
|
||||
2. Create an object of your SAX interface class, e.g. `my_sax`.
|
||||
3. Call `#!cpp bool json::sax_parse(input, &my_sax);` where the first parameter can be any input like a string or an input stream and the second parameter is a pointer to your SAX interface.
|
||||
|
||||
Note the `sax_parse` function only returns a `#!cpp bool` indicating the result of the last executed SAX event. It does not return `json` value - it is up to you to decide what to do with the SAX events. Furthermore, no exceptions are thrown in case of a parse error - it is up to you what to do with the exception object passed to your `parse_error` implementation. Internally, the SAX interface is used for the DOM parser (class `json_sax_dom_parser`) as well as the acceptor (`json_sax_acceptor`), see file `json_sax.hpp`.
|
Reference in New Issue
Block a user