- Design goals
- Sponsors
- Support (documentation, FAQ, discussions, API, bug issues)
- Examples
- Read JSON from a file
- Creating
json
objects from JSON literals - JSON as first-class data type
- Serialization / Deserialization
- STL-like access
- Conversion from STL containers
- JSON Pointer and JSON Patch
- JSON Merge Patch
- Implicit conversions
- Conversions to/from arbitrary types
- Specializing enum conversion
- Binary formats (BSON, CBOR, MessagePack, UBJSON, and BJData)
- Supported compilers
- Integration
- License
- Contact
- Thanks
- Used third-party tools
- Projects using JSON for Modern C++
- Notes
- Execute unit tests
Design goals
There are myriads of JSON libraries out there, and each may even have its reason to exist. Our class had these design goals:
-
Intuitive syntax. In languages such as Python, JSON feels like a first class data type. We used all the operator magic of modern C++ to achieve the same feeling in your code. Check out the examples below and you'll know what I mean.
-
Trivial integration. Our whole code consists of a single header file
json.hpp
. That's it. No library, no subproject, no dependencies, no complex build system. The class is written in vanilla C++11. All in all, everything should require no adjustment of your compiler flags or project settings. -
Serious testing. Our code is heavily unit-tested and covers 100% of the code, including all exceptional behavior. Furthermore, we checked with Valgrind and the Clang Sanitizers that there are no memory leaks. Google OSS-Fuzz additionally runs fuzz tests against all parsers 24/7, effectively executing billions of tests so far. To maintain high quality, the project is following the Core Infrastructure Initiative (CII) best practices.
Other aspects were not so important to us:
-
Memory efficiency. Each JSON object has an overhead of one pointer (the maximal size of a union) and one enumeration element (1 byte). The default generalization uses the following C++ data types:
std::string
for strings,int64_t
,uint64_t
ordouble
for numbers,std::map
for objects,std::vector
for arrays, andbool
for Booleans. However, you can template the generalized classbasic_json
to your needs. -
Speed. There are certainly faster JSON libraries out there. However, if your goal is to speed up your development by adding JSON support with a single header, then this library is the way to go. If you know how to use a
std::vector
orstd::map
, you are already set.
See the contribution guidelines for more information.
Sponsors
You can sponsor this library at GitHub Sponsors.
:raising_hand: Priority Sponsor
:label: Named Sponsors
Thanks everyone!
Support
:question: If you have a question, please check if it is already answered in the FAQ or the Q&A section. If not, please ask a new question there.
:books: If you want to learn more about how to use the library, check out the rest of the README, have a look at code examples, or browse through the help pages.
:construction: If you want to understand the API better, check out the API Reference.
:bug: If you found a bug, please check the FAQ if it is a known issue or the result of a design decision. Please also have a look at the issue list before you create a new issue. Please provide as much information as possible to help us understand and reproduce your issue.
There is also a docset for the documentation browsers Dash, Velocity, and Zeal that contains the full documentation as offline resource.
Examples
Here are some examples to give you an idea how to use the class.
Beside the examples below, you may want to:
→ Check the documentation
→ Browse the standalone example files
Every API function (documented in the API Documentation) has a corresponding standalone example file. For example, the emplace()
function has a matching emplace.cpp example file.
Read JSON from a file
The json
class provides an API for manipulating a JSON value. To create a json
object by reading a JSON file:
#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
// ...
std::ifstream f("example.json");
json data = json::parse(f);
Creating json
objects from JSON literals
Assume you want to create hard-code this literal JSON value in a file, as a json
object:
{
"pi": 3.141,
"happy": true
}
There are various options:
// Using (raw) string literals and json::parse
json ex1 = json::parse(R"(
{
"pi": 3.141,
"happy": true
}
)");
// Using user-defined (raw) string literals
using namespace nlohmann::literals;
json ex2 = R"(
{
"pi": 3.141,
"happy": true
}
)"_json;
// Using initializer lists
json ex3 = {
{"happy", true},
{"pi", 3.141},
};
JSON as first-class data type
Here are some examples to give you an idea how to use the class.
Assume you want to create the JSON object
{
"pi": 3.141,
"happy": true,
"name": "Niels",
"nothing": null,
"answer": {
"everything": 42
},
"list": [1, 0, 2],
"object": {
"currency": "USD",
"value": 42.99
}
}
With this library, you could write:
// create an empty structure (null)
json j;
// add a number that is stored as double (note the implicit conversion of j to an object)
j["pi"] = 3.141;
// add a Boolean that is stored as bool
j["happy"] = true;
// add a string that is stored as std::string
j["name"] = "Niels";
// add another null object by passing nullptr
j["nothing"] = nullptr;
// add an object inside the object
j["answer"]["everything"] = 42;
// add an array that is stored as std::vector (using an initializer list)
j["list"] = { 1, 0, 2 };
// add another object (using an initializer list of pairs)
j["object"] = { {"currency", "USD"}, {"value", 42.99} };
// instead, you could also write (which looks very similar to the JSON above)
json j2 = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {
{"everything", 42}
}},
{"list", {1, 0, 2}},
{"object", {
{"currency", "USD"},
{"value", 42.99}
}}
};
Note that in all these cases, you never need to "tell" the compiler which JSON value type you want to use. If you want to be explicit or express some edge cases, the functions json::array()
and json::object()
will help:
// a way to express the empty array []
json empty_array_explicit = json::array();
// ways to express the empty object {}
json empty_object_implicit = json({});
json empty_object_explicit = json::object();
// a way to express an _array_ of key/value pairs [["currency", "USD"], ["value", 42.99]]
json array_not_object = json::array({ {"currency", "USD"}, {"value", 42.99} });
Serialization / Deserialization
To/from strings
You can create a JSON value (deserialization) by appending _json
to a string literal:
// create object from string literal
json j = "{ \"happy\": true, \"pi\": 3.141 }"_json;
// or even nicer with a raw string literal
auto j2 = R"(
{
"happy": true,
"pi": 3.141
}
)"_json;
Note that without appending the _json
suffix, the passed string literal is not parsed, but just used as JSON string
value. That is, json j = "{ \"happy\": true, \"pi\": 3.141 }"
would just store the string
"{ "happy": true, "pi": 3.141 }"
rather than parsing the actual object.
The string literal should be brought into scope with using namespace nlohmann::literals;
(see json::parse()
).
The above example can also be expressed explicitly using json::parse()
:
// parse explicitly
auto j3 = json::parse(R"({"happy": true, "pi": 3.141})");
You can also get a string representation of a JSON value (serialize):
// explicit conversion to string
std::string s = j.dump(); // {"happy":true,"pi":3.141}
// serialization with pretty printing
// pass in the amount of spaces to indent
std::cout << j.dump(4) << std::endl;
// {
// "happy": true,
// "pi": 3.141
// }
Note the difference between serialization and assignment:
// store a string in a JSON value
json j_string = "this is a string";
// retrieve the string value
auto cpp_string = j_string.template get<std::string>();
// retrieve the string value (alternative when a variable already exists)
std::string cpp_string2;
j_string.get_to(cpp_string2);
// retrieve the serialized value (explicit JSON serialization)
std::string serialized_string = j_string.dump();
// output of original string
std::cout << cpp_string << " == " << cpp_string2 << " == " << j_string.template get<std::string>() <<