• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# basic_json::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 a 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
33    - a C-style array of characters
34    - a pointer to a null-terminated string of single byte characters
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
39
40## Parameters
41
42`i` (in)
43:   Input to parse from.
44
45`ignore_comments` (in)
46:   whether comments should be ignored and treated like whitespace (`#!cpp true`) or yield a parse error
47    (`#!cpp false`); (optional, `#!cpp false` by default)
48
49`first` (in)
50:   iterator to start of character range
51
52`last` (in)
53:   iterator to end of character range
54
55## Return value
56
57Whether the input is valid JSON.
58
59## Exception safety
60
61Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
62
63## Complexity
64
65Linear in the length of the input. The parser is a predictive LL(1) parser.
66
67## Notes
68
69(1) A UTF-8 byte order mark is silently ignored.
70
71## Examples
72
73??? example
74
75    The example below demonstrates the `accept()` function reading from a string.
76
77    ```cpp
78    --8<-- "examples/accept__string.cpp"
79    ```
80
81    Output:
82
83    ```json
84    --8<-- "examples/accept__string.output"
85    ```
86
87## Version history
88
89- Added in version 3.0.0.
90- Ignoring comments via `ignore_comments` added in version 3.9.0.
91