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.
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:
...
...
@@ -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.
!!! example
??? example
Code:
...
...
@@ -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.
!!! example
??? example
Code:
...
...
@@ -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.
!!! example
??? example
Code:
...
...
@@ -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.
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.
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.
??? example
The following code shows how a JSON patch is applied to a value.
```cpp
--8<-- "examples/patch.cpp"
```
Output:
```json
--8<-- "examples/patch.output"
```
## Diff
The library can also calculate a JSON patch (i.e., a **diff**) given two JSON values.
!!! success "Invariant"
For two JSON values *source* and *target*, the following code yields always true:
```cüü
source.patch(diff(source, target)) == target;
```
??? example
The following code shows how a JSON patch is created as a diff for two JSON values.
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
// a JSON value
jsonj_document=R"({
"a": "b",
"c": {
"d": "e",
"f": "g"
}
})"_json;
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 patch
jsonj_patch=R"({
"a":"z",
"c": {
"f": null
}
})"_json;
??? example
// apply the patch
j_document.merge_patch(j_patch);
// {
// "a": "z",
// "c": {
// "d": "e"
// }
// }
```
The following code shows how a JSON Merge Patch is applied to a JSON document.