JSON for Modern C++  3.5.0
index.md
1 # JSON for Modern C++
2 
3 These pages contain the API documentation of JSON for Modern C++, a C++11 header-only JSON class.
4 
5 # Contents
6 
7 - @link nlohmann::basic_json `basic_json` class @endlink
8 - [Functions](functions_func.html)
9  - object inspection
10  - @link nlohmann::basic_json::dump dump @endlink -- value serialization
11  - @link nlohmann::basic_json::type type @endlink -- type of the value
12  - @link nlohmann::basic_json::is_primitive is_primitive @endlink,
13  @link nlohmann::basic_json::is_structured is_structured @endlink,
14  @link nlohmann::basic_json::is_null is_null @endlink,
15  @link nlohmann::basic_json::is_boolean is_boolean @endlink,
16  @link nlohmann::basic_json::is_number is_number @endlink,
17  @link nlohmann::basic_json::is_number_integer is_number_integer @endlink,
18  @link nlohmann::basic_json::is_number_unsigned is_number_unsigned @endlink,
19  @link nlohmann::basic_json::is_number_float is_number_float @endlink,
20  @link nlohmann::basic_json::is_object is_object @endlink,
21  @link nlohmann::basic_json::is_array is_array @endlink,
22  @link nlohmann::basic_json::is_string is_string @endlink,
23  @link nlohmann::basic_json::is_discarded is_discarded @endlink -- check for value type
24  - @link nlohmann::basic_json::operator value_t() const operator value_t @endlink -- type of the value (implicit conversion)
25  - value access
26  - @link nlohmann::basic_json::get get @endlink -- get a value
27  - @link nlohmann::basic_json::get_ptr get_ptr @endlink -- get a value pointer
28  - @link nlohmann::basic_json::get_ref get_ref @endlink -- get a value reference
29  - @link nlohmann::basic_json::operator ValueType() const operator ValueType @endlink -- get a value (implicit conversion)
30  - @link nlohmann::basic_json::value value @endlink -- get a value from an object and return default value if key is not present
31  - exceptions
32  - @link nlohmann::basic_json::parse_error parse_error @endlink for exceptions indicating a parse error
33  - @link nlohmann::basic_json::invalid_iterator invalid_iterator @endlink for exceptions indicating errors with iterators
34  - @link nlohmann::basic_json::type_error type_error @endlink for exceptions indicating executing a member function with a wrong type
35  - @link nlohmann::basic_json::out_of_range out_of_range @endlink for exceptions indicating access out of the defined range
36  - @link nlohmann::basic_json::other_error other_error @endlink for exceptions indicating other library errors
37  - lexicographical comparison operators
38  - serialization
39  - @link nlohmann::basic_json::dump dump @endlink serialize to string
40  - @link nlohmann::basic_json::operator<<(std::ostream&, const basic_json &) operator<< @endlink serialize to stream
41  - deserialization / parsing
42  - @link nlohmann::basic_json::parse parse @endlink parse from input (string, file, etc.) and return JSON value
43  - @link nlohmann::basic_json::sax_parse sax_parse @endlink parse from input (string, file, etc.) and generate SAX events
44  - @link nlohmann::basic_json::operator>>(std::istream&, basic_json&) operator>> @endlink parse from stream
45  - @link nlohmann::basic_json::accept accept @endlink check for syntax errors without parsing
46  - @link nlohmann::json_sax SAX interface @endlink define a user-defined SAX event consumer
47  - [binary formats](binary_formats.md):
48  - CBOR: @link nlohmann::basic_json::from_cbor from_cbor @endlink / @link nlohmann::basic_json::to_cbor to_cbor @endlink
49  - MessagePack: @link nlohmann::basic_json::from_msgpack from_msgpack @endlink / @link nlohmann::basic_json::to_msgpack to_msgpack @endlink
50  - UBJSON: @link nlohmann::basic_json::from_ubjson from_ubjson @endlink / @link nlohmann::basic_json::to_ubjson to_ubjson @endlink
51 - Types
52  - @link nlohmann::basic_json::array_t arrays @endlink
53  - @link nlohmann::basic_json::object_t objects @endlink
54  - @link nlohmann::basic_json::string_t strings @endlink
55  - @link nlohmann::basic_json::boolean_t booleans @endlink
56  - numbers
57  - @link nlohmann::basic_json::number_integer_t signed integers @endlink
58  - @link nlohmann::basic_json::number_unsigned_t unsigned integers @endlink
59  - @link nlohmann::basic_json::number_float_t floating-point @endlink
60 
61 # Container function overview
62 
63 The container functions known from STL have been extended to support the different value types from JSON. However, not all functions can be applied to all value types. Note that the signature of some functions differ between the types; for instance, `at` may be called with either a string to address a key in an object or with an integer to address a value in an array.
64 
65 Note that this table only lists those exceptions thrown due to the type. For instance, the @link nlohmann::basic_json::at(const typename object_t::key_type & key) `at` @endlink function will always throw a @link nlohmann::basic_json::type_error `json::type_error` @endlink exception when called for a string type. When called for an array, it *may* throw an @link nlohmann::basic_json::out_of_range `json::out_of_range` @endlink exception if the passed index is invalid.
66 
67 <table>
68  <tr>
69  <th rowspan="2">group</td>
70  <th rowspan="2">function</td>
71  <th colspan="6">JSON value type</th>
72  </tr>
73  <tr>
74  <th>object</th>
75  <th>array</th>
76  <th>string</th>
77  <th>number</th>
78  <th>boolean</th>
79  <th>null</th>
80  </tr>
81  <tr>
82  <td rowspan="8">iterators</td>
83  <td>`begin`</td>
84  <td class="ok_green">@link nlohmann::basic_json::begin `begin` @endlink</td>
85  <td class="ok_green">@link nlohmann::basic_json::begin `begin` @endlink</td>
86  <td class="ok_green">@link nlohmann::basic_json::begin `begin` @endlink</td>
87  <td class="ok_green">@link nlohmann::basic_json::begin `begin` @endlink</td>
88  <td class="ok_green">@link nlohmann::basic_json::begin `begin` @endlink</td>
89  <td class="ok_green">@link nlohmann::basic_json::begin `begin` @endlink (returns `end()`)</td>
90  </tr>
91  <tr>
92  <td>`cbegin`</td>
93  <td class="ok_green">@link nlohmann::basic_json::cbegin `cbegin` @endlink</td>
94  <td class="ok_green">@link nlohmann::basic_json::cbegin `cbegin` @endlink</td>
95  <td class="ok_green">@link nlohmann::basic_json::cbegin `cbegin` @endlink</td>
96  <td class="ok_green">@link nlohmann::basic_json::cbegin `cbegin` @endlink</td>
97  <td class="ok_green">@link nlohmann::basic_json::cbegin `cbegin` @endlink</td>
98  <td class="ok_green">@link nlohmann::basic_json::cbegin `cbegin` @endlink (returns `cend()`)</td>
99  </tr>
100  <tr>
101  <td>`end`</td>
102  <td class="ok_green">@link nlohmann::basic_json::end `end` @endlink</td>
103  <td class="ok_green">@link nlohmann::basic_json::end `end` @endlink</td>
104  <td class="ok_green">@link nlohmann::basic_json::end `end` @endlink</td>
105  <td class="ok_green">@link nlohmann::basic_json::end `end` @endlink</td>
106  <td class="ok_green">@link nlohmann::basic_json::end `end` @endlink</td>
107  <td class="ok_green">@link nlohmann::basic_json::end `end` @endlink</td>
108  </tr>
109  <tr>
110  <td>`cend`</td>
111  <td class="ok_green">@link nlohmann::basic_json::cend `cend` @endlink</td>
112  <td class="ok_green">@link nlohmann::basic_json::cend `cend` @endlink</td>
113  <td class="ok_green">@link nlohmann::basic_json::cend `cend` @endlink</td>
114  <td class="ok_green">@link nlohmann::basic_json::cend `cend` @endlink</td>
115  <td class="ok_green">@link nlohmann::basic_json::cend `cend` @endlink</td>
116  <td class="ok_green">@link nlohmann::basic_json::cend `cend` @endlink</td>
117  </tr>
118  <tr>
119  <td>`rbegin`</td>
120  <td class="ok_green">@link nlohmann::basic_json::rbegin `rbegin` @endlink</td>
121  <td class="ok_green">@link nlohmann::basic_json::rbegin `rbegin` @endlink</td>
122  <td class="ok_green">@link nlohmann::basic_json::rbegin `rbegin` @endlink</td>
123  <td class="ok_green">@link nlohmann::basic_json::rbegin `rbegin` @endlink</td>
124  <td class="ok_green">@link nlohmann::basic_json::rbegin `rbegin` @endlink</td>
125  <td class="ok_green">@link nlohmann::basic_json::rbegin `rbegin` @endlink</td>
126  </tr>
127  <tr>
128  <td>`crbegin`</td>
129  <td class="ok_green">@link nlohmann::basic_json::crbegin `crbegin` @endlink</td>
130  <td class="ok_green">@link nlohmann::basic_json::crbegin `crbegin` @endlink</td>
131  <td class="ok_green">@link nlohmann::basic_json::crbegin `crbegin` @endlink</td>
132  <td class="ok_green">@link nlohmann::basic_json::crbegin `crbegin` @endlink</td>
133  <td class="ok_green">@link nlohmann::basic_json::crbegin `crbegin` @endlink</td>
134  <td class="ok_green">@link nlohmann::basic_json::crbegin `crbegin` @endlink</td>
135  </tr>
136  <tr>
137  <td>`rend`</td>
138  <td class="ok_green">@link nlohmann::basic_json::rend `rend` @endlink</td>
139  <td class="ok_green">@link nlohmann::basic_json::rend `rend` @endlink</td>
140  <td class="ok_green">@link nlohmann::basic_json::rend `rend` @endlink</td>
141  <td class="ok_green">@link nlohmann::basic_json::rend `rend` @endlink</td>
142  <td class="ok_green">@link nlohmann::basic_json::rend `rend` @endlink</td>
143  <td class="ok_green">@link nlohmann::basic_json::rend `rend` @endlink</td>
144  </tr>
145  <tr>
146  <td>`crend`</td>
147  <td class="ok_green">@link nlohmann::basic_json::crend `crend` @endlink</td>
148  <td class="ok_green">@link nlohmann::basic_json::crend `crend` @endlink</td>
149  <td class="ok_green">@link nlohmann::basic_json::crend `crend` @endlink</td>
150  <td class="ok_green">@link nlohmann::basic_json::crend `crend` @endlink</td>
151  <td class="ok_green">@link nlohmann::basic_json::crend `crend` @endlink</td>
152  <td class="ok_green">@link nlohmann::basic_json::crend `crend` @endlink</td>
153  </tr>
154  <tr>
155  <td rowspan="4">element<br>access</td>
156  <td>`at`</td>
157  <td class="ok_green">@link nlohmann::basic_json::at(const typename object_t::key_type & key) `at` @endlink</td>
158  <td class="ok_green">@link nlohmann::basic_json::at(size_type) `at` @endlink</td>
159  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (304)</td>
160  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (304)</td>
161  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (304)</td>
162  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (304)</td>
163  </tr>
164  <tr>
165  <td>`operator[]`</td>
166  <td class="ok_green">@link nlohmann::basic_json::operator[](const typename object_t::key_type &key) `operator[]` @endlink</td>
167  <td class="ok_green">@link nlohmann::basic_json::operator[](size_type) `operator[]` @endlink</td>
168  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (305)</td>
169  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (305)</td>
170  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (305)</td>
171  <td class="ok_green">@link nlohmann::basic_json::operator[](const typename object_t::key_type & key) `operator[]` @endlink (creates object)<br>@link nlohmann::basic_json::operator[](size_type) `operator[]` @endlink (creates array)</td>
172  </tr>
173  <tr>
174  <td>`front`</td>
175  <td class="ok_green">@link nlohmann::basic_json::front `front` @endlink</td>
176  <td class="ok_green">@link nlohmann::basic_json::front `front` @endlink</td>
177  <td class="ok_green">@link nlohmann::basic_json::front `front` @endlink</td>
178  <td class="ok_green">@link nlohmann::basic_json::front `front` @endlink</td>
179  <td class="ok_green">@link nlohmann::basic_json::front `front` @endlink</td>
180  <td class="nok_throws">throws @link nlohmann::basic_json::invalid_iterator `json::invalid_iterator` @endlink (214)</td>
181  </tr>
182  <tr>
183  <td>`back`</td>
184  <td class="ok_green">@link nlohmann::basic_json::back `back` @endlink</td>
185  <td class="ok_green">@link nlohmann::basic_json::back `back` @endlink</td>
186  <td class="ok_green">@link nlohmann::basic_json::back `back` @endlink</td>
187  <td class="ok_green">@link nlohmann::basic_json::back `back` @endlink</td>
188  <td class="ok_green">@link nlohmann::basic_json::back `back` @endlink</td>
189  <td class="nok_throws">throws @link nlohmann::basic_json::invalid_iterator `json::invalid_iterator` @endlink (214)</td>
190  </tr>
191  <tr>
192  <td rowspan="3">capacity</td>
193  <td>`empty`</td>
194  <td class="ok_green">@link nlohmann::basic_json::empty `empty` @endlink</td>
195  <td class="ok_green">@link nlohmann::basic_json::empty `empty` @endlink</td>
196  <td class="ok_green">@link nlohmann::basic_json::empty `empty` @endlink (returns `false`)</td>
197  <td class="ok_green">@link nlohmann::basic_json::empty `empty` @endlink (returns `false`)</td>
198  <td class="ok_green">@link nlohmann::basic_json::empty `empty` @endlink (returns `false`)</td>
199  <td class="ok_green">@link nlohmann::basic_json::empty `empty` @endlink (returns `true`)</td>
200  </tr>
201  <tr>
202  <td>`size`</td>
203  <td class="ok_green">@link nlohmann::basic_json::size `size` @endlink</td>
204  <td class="ok_green">@link nlohmann::basic_json::size `size` @endlink</td>
205  <td class="ok_green">@link nlohmann::basic_json::size `size` @endlink (returns `1`)</td>
206  <td class="ok_green">@link nlohmann::basic_json::size `size` @endlink (returns `1`)</td>
207  <td class="ok_green">@link nlohmann::basic_json::size `size` @endlink (returns `1`)</td>
208  <td class="ok_green">@link nlohmann::basic_json::size `size` @endlink (returns `0`)</td>
209  </tr>
210  <tr>
211  <td>`max_size_`</td>
212  <td class="ok_green">@link nlohmann::basic_json::max_size `max_size` @endlink</td>
213  <td class="ok_green">@link nlohmann::basic_json::max_size `max_size` @endlink</td>
214  <td class="ok_green">@link nlohmann::basic_json::max_size `max_size` @endlink (returns `1`)</td>
215  <td class="ok_green">@link nlohmann::basic_json::max_size `max_size` @endlink (returns `1`)</td>
216  <td class="ok_green">@link nlohmann::basic_json::max_size `max_size` @endlink (returns `1`)</td>
217  <td class="ok_green">@link nlohmann::basic_json::max_size `max_size` @endlink (returns `0`)</td>
218  </tr>
219  <tr>
220  <td rowspan="7">modifiers</td>
221  <td>`clear`</td>
222  <td class="ok_green">@link nlohmann::basic_json::clear `clear` @endlink</td>
223  <td class="ok_green">@link nlohmann::basic_json::clear `clear` @endlink</td>
224  <td class="ok_green">@link nlohmann::basic_json::clear `clear` @endlink</td>
225  <td class="ok_green">@link nlohmann::basic_json::clear `clear` @endlink</td>
226  <td class="ok_green">@link nlohmann::basic_json::clear `clear` @endlink</td>
227  <td class="ok_green">@link nlohmann::basic_json::clear `clear` @endlink</td>
228  </tr>
229  <tr>
230  <td>`insert`</td>
231  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (309)</td>
232  <td class="ok_green">@link nlohmann::basic_json::insert `insert` @endlink</td>
233  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (309)</td>
234  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (309)</td>
235  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (309)</td>
236  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (309)</td>
237  </tr>
238  <tr>
239  <td>`erase`</td>
240  <td class="ok_green">@link nlohmann::basic_json::erase `erase` @endlink</td>
241  <td class="ok_green">@link nlohmann::basic_json::erase `erase` @endlink</td>
242  <td class="ok_green">@link nlohmann::basic_json::erase `erase` @endlink (converts to null)</td>
243  <td class="ok_green">@link nlohmann::basic_json::erase `erase` @endlink (converts to null)</td>
244  <td class="ok_green">@link nlohmann::basic_json::erase `erase` @endlink (converts to null)</td>
245  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (307)</td>
246  </tr>
247  <tr>
248  <td>`push_back`</td>
249  <td class="ok_green">@link nlohmann::basic_json::push_back(const typename object_t::value_type & val) `push_back` @endlink</td>
250  <td class="ok_green">@link nlohmann::basic_json::push_back(const nlohmann::basic_json &) `push_back` @endlink</td>
251  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (308)</td>
252  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (308)</td>
253  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (308)</td>
254  <td class="ok_green">@link nlohmann::basic_json::push_back(const typename object_t::value_type & val) `push_back` @endlink (creates object)<br>@link nlohmann::basic_json::push_back(const nlohmann::basic_json &) `push_back` @endlink (creates array)</td>
255  </tr>
256  <tr>
257  <td>`emplace` / `emplace_back`</td>
258  <td class="ok_green">@link nlohmann::basic_json::emplace() `emplace` @endlink</td>
259  <td class="ok_green">@link nlohmann::basic_json::emplace_back() `emplace_back` @endlink</td>
260  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (311)</td>
261  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (311)</td>
262  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (311)</td>
263  <td class="ok_green">@link nlohmann::basic_json::emplace() `emplace` @endlink (creates object)<br>@link nlohmann::basic_json::emplace_back() `emplace_back` @endlink (creates array)</td>
264  </tr>
265  <tr>
266  <td>`update`</td>
267  <td class="ok_green">@link nlohmann::basic_json::update() `update` @endlink</td>
268  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (312)</td>
269  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (312)</td>
270  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (312)</td>
271  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (312)</td>
272  <td class="nok_throws">throws @link nlohmann::basic_json::type_error `json::type_error` @endlink (312)</td>
273  </tr>
274  <tr>
275  <td>`swap`</td>
276  <td class="ok_green">@link nlohmann::basic_json::swap `swap` @endlink</td>
277  <td class="ok_green">@link nlohmann::basic_json::swap `swap` @endlink</td>
278  <td class="ok_green">@link nlohmann::basic_json::swap `swap` @endlink</td>
279  <td class="ok_green">@link nlohmann::basic_json::swap `swap` @endlink</td>
280  <td class="ok_green">@link nlohmann::basic_json::swap `swap` @endlink</td>
281  <td class="ok_green">@link nlohmann::basic_json::swap `swap` @endlink</td>
282  </tr>
283  <tr>
284  <td rowspan="2">lookup</td>
285  <td>`find`</td>
286  <td class="ok_green">@link nlohmann::basic_json::find `find` @endlink</td>
287  <td class="ok_green">@link nlohmann::basic_json::find `find` @endlink (returns `end()`)</td>
288  <td class="ok_green">@link nlohmann::basic_json::find `find` @endlink (returns `end()`)</td>
289  <td class="ok_green">@link nlohmann::basic_json::find `find` @endlink (returns `end()`)</td>
290  <td class="ok_green">@link nlohmann::basic_json::find `find` @endlink (returns `end()`)</td>
291  <td class="ok_green">@link nlohmann::basic_json::find `find` @endlink (returns `end()`)</td>
292  </tr>
293  <tr>
294  <td>`count`</td>
295  <td class="ok_green">@link nlohmann::basic_json::count `count` @endlink</td>
296  <td class="ok_green">@link nlohmann::basic_json::count `count` @endlink (returns `0`)</td>
297  <td class="ok_green">@link nlohmann::basic_json::count `count` @endlink (returns `0`)</td>
298  <td class="ok_green">@link nlohmann::basic_json::count `count` @endlink (returns `0`)</td>
299  <td class="ok_green">@link nlohmann::basic_json::count `count` @endlink (returns `0`)</td>
300  <td class="ok_green">@link nlohmann::basic_json::count `count` @endlink (returns `0`)</td>
301  </tr>
302 </table>
303 
304 @copyright Copyright &copy; 2013-2018 Niels Lohmann. The code is licensed under the [MIT License](http://opensource.org/licenses/MIT).
305 
306 @author [Niels Lohmann](http://nlohmann.me)
307 @see https://github.com/nlohmann/json to download the source code
308 
309 @version 3.5.0