BT

JSON for Modern C++ Reaches Version 3.1

| by Sergio De Simone Follow 7 Followers on Feb 06, 2018. Estimated reading time: 1 minute |

A note to our readers: You asked so we have developed a set of features that allow you to reduce the noise: you can get email and web notifications for topics you are interested in. Learn more about our new features.

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.

Rate this Article

Adoption Stage
Style

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.

Tell us what you think

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

Email me replies to any of my messages in this thread
Community comments

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

Email me replies to any of my messages in this thread

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

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT