InfoQ Homepage News JSON for Modern C++ Reaches Version 3.1

JSON for Modern C++ Reaches Version 3.1


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"

// a JSON patch (RFC 6902)
json j_patch_1 = R"([
  { "op": "replace", "path": "/a", "value": "z" },
  { "op": "remove", "path": "/f"}

// a JSON Merge patch (RFC 7386)
json j_patch_2 = R"({
  "c": {
    "f": null

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

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.

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p


Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.