1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
### jsoncons::basic_json::dump, jsoncons::basic_json::dump_pretty
```cpp
void dump(basic_json_visitor<char_type>& visitor) const; (1)
template <CharContainer>
void dump(CharContainer& cont,
const basic_json_encode_options<char_type>& options (2)
= basic_json_encode_options<char_type>()) const;
void dump(std::basic_ostream<char_type>& os,
const basic_json_encode_options<char_type>& options (3)
= basic_json_encode_options<char_type>();
template <CharContainer>
void dump_pretty(CharContainer& cont,
const basic_json_encode_options<char_type>& options (4)
= basic_json_encode_options<char_type>()) const;
void dump_pretty(std::basic_ostream<char_type>& os,
const basic_json_encode_options<char_type>& options (5)
= basic_json_encode_options<char_type>()) const;
template <CharContainer>
void dump(CharContainer& cont,
const basic_json_encode_options<char_type>& options, (6) (deprecated since 1.4.0)
indenting) const;
template <CharContainer>
void dump(CharContainer& cont, indenting indent) const; (7) (deprecated since 1.4.0)
void dump(std::basic_ostream<char_type>& os, (8) (deprecated since 1.4.0)
indenting indent) const;
```
(1) Dumps a json value to the specified [visitor](../basic_json_visitor.md).
(2) Dumps a json value to a character container with "minified" output (single line.)
(3) Dumps a json value to an output stream with "minified" output.
Functions (4)-(5) are identical to (2)-(3) except with "prettified" output (line indentation.)
Functions (6)-(8) that take an `indenting` argument are for backward
compatibility, prefer `dump_pretty`.
#### Exceptions
Throws [ser_error](ser_error.md) if there is a serialization error.
### Examples
#### Dump json value to csv file
```cpp
#include <jsoncons/json.hpp>
#include <jsoncons_ext/csv/csv.hpp>
using namespace jsoncons;
int main()
{
const json books = json::parse(R"(
[
{
"title" : "Kafka on the Shore",
"author" : "Haruki Murakami",
"price" : 25.17
},
{
"title" : "Women: A Novel",
"author" : "Charles Bukowski",
"price" : 12.00
},
{
"title" : "Cutter's Way",
"author" : "Ivan Passer"
}
]
)");
auto options = csv_options{}
.column_names("author,title,price");
csv_stream_encoder encoder(std::cout, options);
books.dump(encoder);
}
```
Output:
```csv
author,title,price
Haruki Murakami,Kafka on the Shore,25.17
Charles Bukowski,Women: A Novel,12.0
Ivan Passer,Cutter's Way,
```
#### Dump json content into a larger document
```cpp
#include <jsoncons/json.hpp>
using namespace jsoncons;
int main()
{
const json some_books = json::parse(R"(
[
{
"title" : "Kafka on the Shore",
"author" : "Haruki Murakami",
"price" : 25.17
},
{
"title" : "Women: A Novel",
"author" : "Charles Bukowski",
"price" : 12.00
}
]
)");
const json more_books = json::parse(R"(
[
{
"title" : "A Wild Sheep Chase: A Novel",
"author" : "Haruki Murakami",
"price" : 9.01
},
{
"title" : "Cutter's Way",
"author" : "Ivan Passer",
"price" : 8.00
}
]
)");
json_stream_encoder encoder(std::cout); // pretty print
encoder.begin_array();
for (const auto& book : some_books.array_range())
{
book.dump(encoder);
}
for (const auto& book : more_books.array_range())
{
book.dump(encoder);
}
encoder.end_array();
encoder.flush();
}
```
Output:
```json
[
{
"author": "Haruki Murakami",
"price": 25.17,
"title": "Kafka on the Shore"
},
{
"author": "Charles Bukowski",
"price": 12.0,
"title": "Women: A Novel"
},
{
"author": "Haruki Murakami",
"price": 9.01,
"title": "A Wild Sheep Chase: A Novel"
},
{
"author": "Ivan Passer",
"price": 8.0,
"title": "Cutter's Way"
}
]
```
|