Defined in header <nlohmann/json.hpp>
template<
template<typename U, typename V, typename... Args> class ObjectType = std::map,
template<typename U, typename... Args> class ArrayType = std::vector,
class StringType = std::string,
class BooleanType = bool,
class NumberIntegerType = std::int64_t,
class NumberUnsignedType = std::uint64_t,
class NumberFloatType = double,
template<typename U> class AllocatorType = std::allocator,
template<typename T, typename SFINAE = void> class JSONSerializer = adl_serializer,
class BinaryType = std::vector<std::uint8_t>,
class CustomBaseClass = void
>
class basic_json;
| Template parameter | Description | Derived type |
|---|---|---|
ObjectType | type for JSON objects | object_t |
ArrayType | type for JSON arrays | array_t |
StringType | type for JSON strings and object keys | string_t |
BooleanType | type for JSON booleans | boolean_t |
NumberIntegerType | type for JSON integer numbers | number_integer_t |
NumberUnsignedType | type for JSON unsigned integer numbers | number_unsigned_t |
NumberFloatType | type for JSON floating-point numbers | number_float_t |
AllocatorType | type of the allocator to use | |
JSONSerializer | the serializer to resolve internal calls to to_json() and from_json() | json_serializer |
BinaryType | type for binary arrays | binary_t |
CustomBaseClass | extension point for user code | json_base_class_t |
All operations that add values to an array (push_back , operator+=, emplace_back, insert, and operator[] for a non-existing index) can yield a reallocation, in which case all iterators (including the end() iterator) and all references to the elements are invalidated.
For ordered_json, also all operations that add a value to an object (push_back, operator+=, emplace, insert, update, and operator[] for a non-existing key) can yield a reallocation, in which case all iterators (including the end() iterator) and all references to the elements are invalidated.
The class satisfies the following concept requirements:
==, see operator==.<, see operator<.swap.std::nullptr_t objects which are used to model the null value.basic_json valuesbasic_json class| Type | Definition |
|---|---|
value_type | basic_json |
reference | value_type& |
const_reference | const value_type& |
difference_type | std::ptrdiff_t |
size_type | std::size_t |
allocator_type | AllocatorType<basic_json> |
pointer | std::allocator_traits<allocator_type>::pointer |
const_pointer | std::allocator_traits<allocator_type>::const_pointer |
iterator | LegacyBidirectionalIterator |
const_iterator | constant LegacyBidirectionalIterator |
reverse_iterator | reverse iterator, derived from iterator |
const_reverse_iterator | reverse iterator, derived from const_iterator |
iteration_proxy | helper type for items function |
Functions to inspect the type of a JSON value.
Optional functions to access the diagnostic positions.
Direct access to the stored value of a JSON value.
Access to the JSON value
to_string function for JSON valuesformat_as function for JSON values (fmt support)std::formatExample
The example shows how the library is used.
#include <iostream>
#include <iomanip>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
// create a JSON object
json j =
{
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{
"answer", {
{"everything", 42}
}
},
{"list", {1, 0, 2}},
{
"object", {
{"currency", "USD"},
{"value", 42.99}
}
}
};
// add new values
j["new"]["key"]["value"] = {"another", "list"};
// count elements
auto s = j.size();
j["size"] = s;
// pretty print with indent of 4 spaces
std::cout << std::setw(4) << j << '\n';
}
Output:
{
"answer": {
"everything": 42
},
"happy": true,
"list": [
1,
0,
2
],
"name": "Niels",
"new": {
"key": {
"value": [
"another",
"list"
]
}
},
"nothing": null,
"object": {
"currency": "USD",
"value": 42.99
},
"pi": 3.141,
"size": 8
}