|
◆ dump()
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>
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] | indent | If 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_char | The character to use for indentation if indent is greater than 0 . The default is (space). |
[in] | ensure_ascii | If ensure_ascii is true, all non-ASCII characters in the output are escaped with \uXXXX sequences, and the result consists of ASCII characters only. |
[in] | error_handler | how to react on decoding errors; there are three possible values: strict (throws and exception in case a decoding error occurs; default), replace (replace invalid UTF-8 sequences with U+FFFD), and ignore (ignore invalid UTF-8 sequences during serialization). |
- Returns
- string containing the serialization of the JSON value
- Exceptions
-
type_error.316 | if 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. ^^
2 #include <nlohmann/json.hpp> 9 json j_object = {{ "one", 1}, { "two", 2}}; 10 json j_array = {1, 2, 4, 8, 16}; 11 json j_string = "Hellö 😀!"; 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"; 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"; 28 std::cout << "strings:" << '\n' 29 << j_string.dump() << '\n' 30 << j_string.dump(-1, ' ', true) << '\n'; 33 json j_invalid = "ä\xA9ü"; 36 std::cout << j_invalid.dump() << std::endl; 40 std::cout << e.what() << std::endl; 43 std::cout << "string with replaced invalid characters: " 44 << j_invalid.dump(-1, ' ', false, json::error_handler_t::replace) 45 << "\nstring with ignored invalid characters: " 46 << j_invalid.dump(-1, ' ', false, json::error_handler_t::ignore) basic_json<> json default JSON class
detail::type_error type_error exception indicating executing a member function with a wrong type
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 2: 0xA9
string with replaced invalid characters: "ä�ü"
string with ignored invalid characters: "äü"
^^ The example code above can be translated withg++ -std=c++11 -Isingle_include 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; error handlers added in version 3.4.0.
Definition at line 14347 of file json.hpp.
|