JSON for Modern C++  3.0.0

◆ dump()

template<template< typename, typename, typename... > class ObjectType = std::map, template< typename, typename... > 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 > class AllocatorType = std::allocator, template< typename, typename=void > class JSONSerializer = adl_serializer>
string_t nlohmann::basic_json::dump ( const int  indent = -1,
const char  indent_char = ' ',
const bool  ensure_ascii = false 
) const
inline

Serialization function for JSON values. The function tries to mimic Python's json.dumps() function, and currently supports its indent and ensure_ascii parameters.

Parameters
[in]indentIf indent is nonnegative, then array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. -1 (the default) selects the most compact representation.
[in]indent_charThe character to use for indentation if indent is greater than 0. The default is (space).
[in]ensure_asciiIf ensure_ascii is true, all non-ASCII characters in the output are escaped with \uXXXX sequences, and the result consists of ASCII characters only.
Returns
string containing the serialization of the JSON value
Exceptions
type_error.316if a string stored inside the JSON value is not UTF-8 encoded
Complexity
Linear.
Exception safety
Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
Example
The following example shows the effect of different indent, indent_char, and ensure_ascii parameters to the result of the serialization.
1 #include <iostream>
2 #include "json.hpp"
3 
4 using json = nlohmann::json;
5 
6 int main()
7 {
8  // create JSON values
9  json j_object = {{"one", 1}, {"two", 2}};
10  json j_array = {1, 2, 4, 8, 16};
11  json j_string = "Hellö 😀!";
12 
13  // call dump()
14  std::cout << "objects:" << '\n'
15  << j_object.dump() << "\n\n"
16  << j_object.dump(-1) << "\n\n"
17  << j_object.dump(0) << "\n\n"
18  << j_object.dump(4) << "\n\n"
19  << j_object.dump(1, '\t') << "\n\n";
20 
21  std::cout << "arrays:" << '\n'
22  << j_array.dump() << "\n\n"
23  << j_array.dump(-1) << "\n\n"
24  << j_array.dump(0) << "\n\n"
25  << j_array.dump(4) << "\n\n"
26  << j_array.dump(1, '\t') << "\n\n";
27 
28  std::cout << "strings:" << '\n'
29  << j_string.dump() << '\n'
30  << j_string.dump(-1, ' ', true) << '\n';
31 
32  // create JSON value with invalid UTF-8 byte sequence
33  json j_invalid = "\xF0\xA4\xAD\xC0";
34  try
35  {
36  std::cout << j_invalid.dump() << std::endl;
37  }
38  catch (json::type_error& e)
39  {
40  std::cout << e.what() << std::endl;
41  }
42 }
basic_json<> json
default JSON class
Definition: json.hpp:14353
detail::type_error type_error
exception indicating executing a member function with a wrong type
Definition: json.hpp:7412
Output (play with this example online):
objects:
{"one":1,"two":2}

{"one":1,"two":2}

{
"one": 1,
"two": 2
}

{
    "one": 1,
    "two": 2
}

{
	"one": 1,
	"two": 2
}

arrays:
[1,2,4,8,16]

[1,2,4,8,16]

[
1,
2,
4,
8,
16
]

[
    1,
    2,
    4,
    8,
    16
]

[
	1,
	2,
	4,
	8,
	16
]

strings:
"Hellö 😀!"
"Hell\u00f6 \ud83d\ude00!"
[json.exception.type_error.316] invalid UTF-8 byte at index 3: 0xC0
The example code above can be translated with
g++ -std=c++11 -Isrc doc/examples/dump.cpp -o dump 
See also
https://docs.python.org/2/library/json.html#json.dump
Since
version 1.0.0; indentation character indent_char, option ensure_ascii and exceptions added in version 3.0.0

Definition at line 9061 of file json.hpp.