JSON for Modern C++ 3.1 adds support for Universal Binary JSON (UBJSON) specification and JSON Merge Patch.
UBJSON is one of several formats that JSON for Modern C++ supports to reduce the size of the encoded value and to speed up its decoding. Besides UBJSON, CBOR and MessagePack are also supported. Each provides different strengths, so depending on the kind of data you have to represent, your best option may vary. Of the three, UBJSON is the only complete binary format, meaning that all JSON values can be converted to UBJSON and all UBJSON values can be converted to JSON.
The JSON Merge Patch format aims to declaratively describe differences between two JSON documents. This format is meant to be used in conjunction with the HTTP PATCH verb, which allows to partially update a resource, whereas HTTP PUT is used to fully replace it. The JSON Merge Patch allows you to define just a portion of the JSON and have it merged on the server-side. This can be more convenient than using the basic JSON Patch format, which relies on the specification of a set of operation to patch the original JSON document. For example:
// a JSON value
json j_document = R"({
"a": "b",
"c": {
"d": "e",
"f": "g"
}
})"_json;
// a JSON patch (RFC 6902)
json j_patch_1 = R"([
{ "op": "replace", "path": "/a", "value": "z" },
{ "op": "remove", "path": "/f"}
])"_json;
// a JSON Merge patch (RFC 7386)
json j_patch_2 = R"({
"a":"z",
"c": {
"f": null
}
})"_json;
The JSON for Modern C++ library is meant to provide an intuitive syntax to handle JSON data as if it were a first-class data type. For example, you could instantiate an object by writing the following:
json j2 = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {
{"everything", 42}
}},
{"list", {1, 0, 2}},
{"object", {
{"currency", "USD"},
{"value", 42.99}
}}
};
Similarly, this is how you decode a JSON string by appending __json
to a string literal:
auto j2 = R"(
{
"happy": true,
"pi": 3.141
}
)"_json;
Other advantages that JSON for Modern C++ provides are trivial integration, thanks to it being encapsulated in a single header file json.hpp
not requiring any external libraries nor dependencies; additionally, it claims to have 100% code coverage unit testing and to be free of memory leaks.