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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
### jsoncons::encode_json, jsoncons::encode_json_pretty, jsoncons::try_encode_json, jsoncons::try_encode_json_pretty
Encode a C++ data structure to a JSON formatted string or stream. `encode_json` will work for all types that
have json reflection traites defined.
```cpp
#include <jsoncons/encode_json.hpp>
template <typename T,typename CharT> (1)
void encode_json(const T& val, basic_json_visitor<CharT>& encoder);
template <typename T,typename CharContainer>
void encode_json(const T& val, CharContainer& cont,
const basic_json_encode_options<CharContainer::value_type>& options (2)
= basic_json_encode_options<CharContainer::value_type>();
template <typename T,typename CharT>
void encode_json(const T& val, std::basic_ostream<CharT>& os,
const basic_json_encode_options<CharT>& options (3)
= basic_json_encode_options<CharT>();
template <typename T,typename CharContainer,typename Alloc,typename TempAlloc>
void encode_json(const allocator_set<Alloc,TempAlloc>& aset,
const T& val, CharContainer& cont,
const basic_json_encode_options<CharContainer::value_type>& options (4) (since 0.171.0)
= basic_json_encode_options<CharContainer::value_type>();
template <typename T,typename CharT,typename Alloc,typename TempAlloc>
void encode_json(const allocator_set<Alloc,TempAlloc>& aset,
const T& val, std::basic_ostream<CharT>& os,
const basic_json_encode_options<CharT>& options (5) (since 0.171.0)
= basic_json_encode_options<CharT>());
template <typename T,typename CharContainer>
void encode_json_pretty(const T& val, CharContainer& cont,
const basic_json_encode_options<CharContainer::value_type>& options (6) (since 0.155.0)
= basic_json_encode_options<CharContainer::value_type>());
template <typename T,typename CharT>
void encode_json_pretty(const T& val, std::basic_ostream<CharT>& os,
const basic_json_encode_options<CharT>& options (7) (since 0.155.0)
= basic_json_encode_options<CharT>());
template <typename T,typename CharContainer>
void encode_json_pretty(const allocator_set<Alloc,TempAlloc>& aset,
const T& val, CharContainer& cont,
const basic_json_encode_options<CharContainer::value_type>& options (8) (since 1.4.0)
= basic_json_encode_options<CharContainer::value_type>());
template <typename T,typename CharT>
void encode_json_pretty(const allocator_set<Alloc,TempAlloc>& aset,
const T& val, std::basic_ostream<CharT>& os,
const basic_json_encode_options<CharT>& options (9) (since 1.4.0)
template <typename T,typename CharT>
write_result try_encode_json(const T& val, basic_json_visitor<CharT>& encoder); (10) (since 1.4.0)
= basic_json_encode_options<CharT>());
template <typename T,typename CharContainer>
write_result try_encode_json(const T& val, CharContainer& cont,
const basic_json_encode_options<CharContainer::value_type>& options (11) (since 1.4.0)
= basic_json_encode_options<CharContainer::value_type>();
template <typename T,typename CharT>
write_result try_encode_json(const T& val, std::basic_ostream<CharT>& os,
const basic_json_encode_options<CharT>& options (12) (since 1.4.0)
= basic_json_encode_options<CharT>();
template <typename T,typename CharContainer,typename Alloc,typename TempAlloc>
write_result try_encode_json(const allocator_set<Alloc,TempAlloc>& aset,
const T& val, CharContainer& cont,
const basic_json_encode_options<CharContainer::value_type>& options (13) (since 1.4.0)
= basic_json_encode_options<CharContainer::value_type>();
template <typename T,typename CharT,typename Alloc,typename TempAlloc>
write_result try_encode_json(const allocator_set<Alloc,TempAlloc>& aset,
const T& val, std::basic_ostream<CharT>& os,
const basic_json_encode_options<CharT>& options (14) (since 1.4.0)
= basic_json_encode_options<CharT>());
template <typename T,typename CharContainer>
write_result try_encode_json_pretty(const T& val, CharContainer& cont,
const basic_json_encode_options<CharContainer::value_type>& options (15) (since 1.4.0)
= basic_json_encode_options<CharContainer::value_type>());
template <typename T,typename CharT>
write_result try_encode_json_pretty(const T& val, std::basic_ostream<CharT>& os,
const basic_json_encode_options<CharT>& options (16) (since 1.4.0)
= basic_json_encode_options<CharT>());
template <typename T,typename CharContainer>
write_result try_encode_json_pretty(const allocator_set<Alloc,TempAlloc>& aset,
const T& val, CharContainer& cont,
const basic_json_encode_options<CharContainer::value_type>& options (17) (since 1.4.0)
= basic_json_encode_options<CharContainer::value_type>());
template <typename T,typename CharT>
write_result try_encode_json_pretty(const allocator_set<Alloc,TempAlloc>& aset,
const T& val, std::basic_ostream<CharT>& os,
const basic_json_encode_options<CharT>& options (18) (since 1.4.0)
= basic_json_encode_options<CharT>());
```
(1) Write val to an encoder
(2) Encode `val` to a character container with "minified" output (single line) using the specified (or defaulted) [options](basic_json_options.md).
(3) Encode `val` to an output stream in "minified" output using the specified (or defaulted) [options](basic_json_options.md).
Functions (4)-(5) are identical to (2)-(3) except an [allocator_set](allocator_set.md) is passed as an additional argument.
(6) Encode `val` to a character container with "prettified" output (line indentation) using the specified (or defaulted) [options](basic_json_options.md).
(7) Encode `val` to an output stream with "prettified" output using the specified (or defaulted) [options](basic_json_options.md).
Functions (8)-(9) are identical to (6)-(7) except an [allocator_set](allocator_set.md) is passed as an additional argument.
(10)-(18) Non-throwing versions of (1)-(9)
#### Parameters
<table>
<tr>
<td>aset</td>
<td>[allocator_set](allocator_set.md)</td>
</tr>
<tr>
<td>val</td>
<td>C++ data structure</td>
</tr>
<tr>
<td>visitor</td>
<td>JSON output visitor</td>
</tr>
<tr>
<td>options</td>
<td>Serialization options</td>
</tr>
<tr>
<td>os</td>
<td>Output stream</td>
</tr>
</table>
#### Return value
(1)-(9) None
(10)-(18) [write_result](write_result.md)
#### Exceptions
(1)-(9) Throw [ser_error](ser_error.md) if encode fails.
Any overload may throw `std::bad_alloc` if memory allocation fails.
### Examples
#### Map with string-tuple pairs
```cpp
#include <iostream>
#include <map>
#include <tuple>
#include <jsoncons/json.hpp>
using namespace jsoncons;
int main()
{
using employee_collection = std::map<std::string,std::tuple<std::string,std::string,double>>;
employee_collection employees =
{
{"John Smith",{"Hourly","Software Engineer",10000}},
{"Jane Doe",{"Commission","Sales",20000}}
};
std::cout << "(1)\n" << '\n';
encode_json(employees,std::cout);
std::cout << "\n\n";
std::cout << "(2) Again, with pretty print\n" << '\n';
encode_json_pretty(employees, std::cout);
}
```
Output:
```
(1)
{"Jane Doe":["Commission","Sales",20000.0],"John Smith":["Hourly","Software Engineer",10000.0]}
(2) Again, with pretty print
{
"Jane Doe": ["Commission","Sales",20000.0],
"John Smith": ["Hourly","Software Engineer",10000.0]
}
```
#### Contain JSON output in an object (prettified output)
```cpp
#include <iostream>
#include <map>
#include <tuple>
#include <jsoncons/json.hpp>
using namespace jsoncons;
int main()
{
std::map<std::string, std::tuple<std::string, std::string, double>> employees =
{
{"John Smith",{"Hourly","Software Engineer",10000}},
{"Jane Doe",{"Commission","Sales",20000}}
};
json_stream_encoder encoder(std::cout);
encoder.begin_object();
encoder.key("Employees");
encode_json(employees, encoder);
encoder.end_object();
encoder.flush();
}
```
Output:
```json
{
"Employees": {
"Jane Doe": ["Commission","Sales",20000.0],
"John Smith": ["Hourly","Software Engineer",10000.0]
}
}
```
#### Contain JSON output in an object (compressed output)
```cpp
#include <iostream>
#include <map>
#include <tuple>
#include <jsoncons/json.hpp>
using namespace jsoncons;
int main()
{
std::map<std::string, std::tuple<std::string, std::string, double>> employees =
{
{"John Smith",{"Hourly","Software Engineer",10000}},
{"Jane Doe",{"Commission","Sales",20000}}
};
compact_json_stream_encoder encoder(std::cout);
encoder.begin_object();
encoder.key("Employees");
encode_json(employees, encoder);
encoder.end_object();
encoder.flush();
}
```
Output:
```json
{"Employees":{"Jane Doe":["Commission","Sales",20000.0],"John Smith":["Hourly","Software Engineer",10000.0]}}
```
### See also
[basic_json_visitor](basic_json_visitor.md)
[basic_json_options](basic_json_options.md)
[basic_json_encoder](basic_json_encoder.md)
[decode_json](decode_json.md)
|