• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# <small>nlohmann::basic_json::</small>accept
2
3```cpp
4// (1)
5template<typename InputType>
6static bool accept(InputType&& i,
7                   const bool ignore_comments = false);
8
9// (2)
10template<typename IteratorType>
11static bool accept(IteratorType first, IteratorType last,
12                   const bool ignore_comments = false);
13```
14
15Checks whether the input is valid JSON.
16
171. Reads from a compatible input.
182. Reads from a pair of character iterators
19
20    The value_type of the iterator must be an integral type with size of 1, 2 or 4 bytes, which will be interpreted
21    respectively as UTF-8, UTF-16 and UTF-32.
22
23Unlike the [`parse`](parse.md) function, this function neither throws an exception in case of invalid JSON input
24(i.e., a parse error) nor creates diagnostic information.
25
26## Template parameters
27
28`InputType`
29:   A compatible input, for instance:
30
31    - an `std::istream` object
32    - a `FILE` pointer (must not be null)
33    - a C-style array of characters
34    - a pointer to a null-terminated string of single byte characters
35    - a `std::string`
36    - an object `obj` for which `begin(obj)` and `end(obj)` produces a valid pair of iterators.
37
38`IteratorType`
39:   a compatible iterator type, for instance.
40
41    - a pair of `std::string::iterator` or `std::vector<std::uint8_t>::iterator`
42    - a pair of pointers such as `ptr` and `ptr + len`
43
44## Parameters
45
46`i` (in)
47:   Input to parse from.
48
49`ignore_comments` (in)
50:   whether comments should be ignored and treated like whitespace (`#!cpp true`) or yield a parse error
51    (`#!cpp false`); (optional, `#!cpp false` by default)
52
53`first` (in)
54:   iterator to start of character range
55
56`last` (in)
57:   iterator to end of character range
58
59## Return value
60
61Whether the input is valid JSON.
62
63## Exception safety
64
65Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
66
67## Complexity
68
69Linear in the length of the input. The parser is a predictive LL(1) parser.
70
71## Notes
72
73(1) A UTF-8 byte order mark is silently ignored.
74
75!!! danger "Runtime assertion"
76
77    The precondition that a passed `#!cpp FILE` pointer must not be null is enforced with a
78    [runtime assertion](../../features/assertions.md).
79
80## Examples
81
82??? example
83
84    The example below demonstrates the `accept()` function reading from a string.
85
86    ```cpp
87    --8<-- "examples/accept__string.cpp"
88    ```
89
90    Output:
91
92    ```json
93    --8<-- "examples/accept__string.output"
94    ```
95
96## See also
97
98- [parse](parse.md) - deserialize from a compatible input
99- [operator>>](../operator_gtgt.md) - deserialize from stream
100
101## Version history
102
103- Added in version 3.0.0.
104- Ignoring comments via `ignore_comments` added in version 3.9.0.
105
106!!! warning "Deprecation"
107
108    Overload (2) replaces calls to `accept` with a pair of iterators as their first parameter which has been
109    deprecated in version 3.8.0. This overload will be removed in version 4.0.0. Please replace all calls like
110    `#!cpp accept({ptr, ptr+len}, ...);` with `#!cpp accept(ptr, ptr+len, ...);`.
111
112    You should be warned by your compiler with a `-Wdeprecated-declarations` warning if you are using a deprecated
113    function.
114