• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# <small>nlohmann::basic_json::</small>parse
2
3```cpp
4// (1)
5template<typename InputType>
6static basic_json parse(InputType&& i,
7                        const parser_callback_t cb = nullptr,
8                        const bool allow_exceptions = true,
9                        const bool ignore_comments = false);
10
11// (2)
12template<typename IteratorType>
13static basic_json parse(IteratorType first, IteratorType last,
14                        const parser_callback_t cb = nullptr,
15                        const bool allow_exceptions = true,
16                        const bool ignore_comments = false);
17```
18
191. Deserialize from a compatible input.
202. Deserialize from a pair of character iterators
21
22    The `value_type` of the iterator must be an integral type with size of 1, 2 or 4 bytes, which will be interpreted
23    respectively as UTF-8, UTF-16 and UTF-32.
24
25## Template parameters
26
27`InputType`
28:   A compatible input, for instance:
29
30    - an `std::istream` object
31    - a `FILE` pointer (must not be null)
32    - a C-style array of characters
33    - a pointer to a null-terminated string of single byte characters
34    - a `std::string`
35    - an object `obj` for which `begin(obj)` and `end(obj)` produces a valid pair of iterators.
36
37`IteratorType`
38:   a compatible iterator type, for instance.
39
40    - a pair of `std::string::iterator` or `std::vector<std::uint8_t>::iterator`
41    - a pair of pointers such as `ptr` and `ptr + len`
42
43## Parameters
44
45`i` (in)
46:   Input to parse from.
47
48`cb` (in)
49:   a parser callback function of type [`parser_callback_t`](parser_callback_t.md) which is used to control the
50    deserialization by filtering unwanted values (optional)
51
52`allow_exceptions` (in)
53:    whether to throw exceptions in case of a parse error (optional, `#!cpp true` by default)
54
55`ignore_comments` (in)
56:   whether comments should be ignored and treated like whitespace (`#!cpp true`) or yield a parse error
57    (`#!cpp false`); (optional, `#!cpp false` by default)
58
59`first` (in)
60:   iterator to start of character range
61
62`last` (in)
63:   iterator to end of character range
64
65## Return value
66
67Deserialized JSON value; in case of a parse error and `allow_exceptions` set to `#!cpp false`, the return value will be
68`value_t::discarded`. The latter can be checked with [`is_discarded`](is_discarded.md).
69
70## Exception safety
71
72Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
73
74## Exceptions
75
76- Throws [`parse_error.101`](../../home/exceptions.md#jsonexceptionparse_error101) in case of an unexpected token.
77- Throws [`parse_error.102`](../../home/exceptions.md#jsonexceptionparse_error102) if to_unicode fails or surrogate
78  error.
79- Throws [`parse_error.103`](../../home/exceptions.md#jsonexceptionparse_error103) if to_unicode fails.
80
81## Complexity
82
83Linear in the length of the input. The parser is a predictive LL(1) parser. The complexity can be higher if the parser
84callback function `cb` or reading from (1) the input `i` or (2) the iterator range [`first`, `last`] has a
85super-linear complexity.
86
87## Notes
88
89(1) A UTF-8 byte order mark is silently ignored.
90
91!!! danger "Runtime assertion"
92
93    The precondition that a passed `#!cpp FILE` pointer must not be null is enforced with a
94    [runtime assertion](../../features/assertions.md).
95
96## Examples
97
98??? example "Parsing from a character array"
99
100    The example below demonstrates the `parse()` function reading from an array.
101
102    ```cpp
103    --8<-- "examples/parse__array__parser_callback_t.cpp"
104    ```
105
106    Output:
107
108    ```json
109    --8<-- "examples/parse__array__parser_callback_t.output"
110    ```
111
112??? example "Parsing from a string"
113
114    The example below demonstrates the `parse()` function with and without callback function.
115
116    ```cpp
117    --8<-- "examples/parse__string__parser_callback_t.cpp"
118    ```
119
120    Output:
121
122    ```json
123    --8<-- "examples/parse__string__parser_callback_t.output"
124    ```
125
126??? example "Parsing from an input stream"
127
128    The example below demonstrates the `parse()` function with and without callback function.
129
130    ```cpp
131    --8<-- "examples/parse__istream__parser_callback_t.cpp"
132    ```
133
134    Output:
135
136    ```json
137    --8<-- "examples/parse__istream__parser_callback_t.output"
138    ```
139
140??? example "Parsing from a contiguous container"
141
142    The example below demonstrates the `parse()` function reading from a contiguous container.
143
144    ```cpp
145    --8<-- "examples/parse__contiguouscontainer__parser_callback_t.cpp"
146    ```
147
148    Output:
149
150    ```json
151    --8<-- "examples/parse__contiguouscontainer__parser_callback_t.output"
152    ```
153
154??? example "Parsing from a non null-terminated string"
155
156    The example below demonstrates the `parse()` function reading from a string that is not null-terminated.
157
158    ```cpp
159    --8<-- "examples/parse__pointers.cpp"
160    ```
161
162    Output:
163
164    ```json
165    --8<-- "examples/parse__pointers.output"
166    ```
167
168??? example "Parsing from an iterator pair"
169
170    The example below demonstrates the `parse()` function reading from an iterator pair.
171
172    ```cpp
173    --8<-- "examples/parse__iterator_pair.cpp"
174    ```
175
176    Output:
177
178    ```json
179    --8<-- "examples/parse__iterator_pair.output"
180    ```
181
182??? example "Effect of `allow_exceptions` parameter"
183
184    The example below demonstrates the effect of the `allow_exceptions` parameter in the ´parse()` function.
185
186    ```cpp
187    --8<-- "examples/parse__allow_exceptions.cpp"
188    ```
189
190    Output:
191
192    ```json
193    --8<-- "examples/parse__allow_exceptions.output"
194    ```
195
196## See also
197
198- [accept](accept.md) - check if the input is valid JSON
199- [operator>>](../operator_gtgt.md) - deserialize from stream
200
201## Version history
202
203- Added in version 1.0.0.
204- Overload for contiguous containers (1) added in version 2.0.3.
205- Ignoring comments via `ignore_comments` added in version 3.9.0.
206
207!!! warning "Deprecation"
208
209    Overload (2) replaces calls to `parse` with a pair of iterators as their first parameter which has been
210    deprecated in version 3.8.0. This overload will be removed in version 4.0.0. Please replace all calls like
211    `#!cpp parse({ptr, ptr+len}, ...);` with `#!cpp parse(ptr, ptr+len, ...);`.
212
213    You should be warned by your compiler with a `-Wdeprecated-declarations` warning if you are using a deprecated
214    function.
215