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.
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.
@@ -88,7 +88,7 @@ Binary values are serialized differently according to the formats.
...
@@ -88,7 +88,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`.
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
Code:
Code:
...
@@ -120,7 +120,7 @@ JSON does not have a binary type, and this library does not introduce a new type
...
@@ -120,7 +120,7 @@ JSON does not have a binary type, and this library does not introduce a new type
[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.
[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
??? example
Code:
Code:
...
@@ -160,7 +160,7 @@ JSON does not have a binary type, and this library does not introduce a new type
...
@@ -160,7 +160,7 @@ JSON does not have a binary type, and this library does not introduce a new type
[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.
[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
??? example
Code:
Code:
...
@@ -200,7 +200,7 @@ JSON does not have a binary type, and this library does not introduce a new type
...
@@ -200,7 +200,7 @@ JSON does not have a binary type, and this library does not introduce a new type
If no subtype is given, the bin family (bin8, bin16, bin32) is used.
If no subtype is given, the bin family (bin8, bin16, bin32) is used.
!!! example
??? example
Code:
Code:
...
@@ -239,7 +239,7 @@ If no subtype is given, the bin family (bin8, bin16, bin32) is used.
...
@@ -239,7 +239,7 @@ If no subtype is given, the bin family (bin8, bin16, bin32) is used.
[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.
[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.
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.
## Patches
```cpp
JSON Patch ([RFC 6902](https://tools.ietf.org/html/rfc6902)) defines a JSON document structure for expressing a sequence of operations to apply to a JSON) document. With the `patch` function, a JSON Patch is applied to the current JSON value by executing all operations from the 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.
The library supports JSON Merge Patch ([RFC 7386](https://tools.ietf.org/html/rfc7386)) as a patch format.
The merge patch format is primarily intended for use with the HTTP PATCH method as a means of describing a set of modifications to a target resource's content. This function applies a merge patch to the current JSON value.
```cpp
Instead of using [JSON Pointer](json_pointer.md) to specify values to be manipulated, it describes the changes using a syntax that closely mimics the document being modified.
// a JSON value
jsonj_document=R"({
"a": "b",
"c": {
"d": "e",
"f": "g"
}
})"_json;
// a patch
??? example
jsonj_patch=R"({
"a":"z",
"c": {
"f": null
}
})"_json;
// apply the patch
The following code shows how a JSON Merge Patch is applied to a JSON document.