Files
json/docs/mkdocs/docs/api/basic_json_document/accept.md
T
Niels Lohmann c006db93d4 Reject integer lengths in json_document::parse
parse(ptr, len) compiled: len converted to allow_exceptions, and ptr was
read as a C string, past the end of a buffer without a terminating NUL.
Delete the overloads of parse, parse_copy, accept, and read that take an
integer other than bool where the flags are expected.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-09 16:10:11 +02:00

2.0 KiB

nlohmann::basic_json_document::accept

template<typename InputType>
static bool accept(InputType&& input,
                   const bool ignore_comments = false,
                   const bool ignore_trailing_commas = false);

Checks whether the input is valid JSON, accepting and rejecting exactly what BasicJsonType::accept() does, with the same options. Unlike parse(), this function never throws an exception for invalid input, and the returned #!cpp bool is the only result -- no document is returned.

Template parameters

InputType
A compatible input; see parse.

Parameters

input (in)
Input to check.
ignore_comments (in)
whether comments should be ignored and treated like whitespace (#!cpp true) or yield a parse error (#!cpp false); (optional, #!cpp false by default)
ignore_trailing_commas (in)
whether trailing commas in arrays or objects should be ignored and treated like whitespace (#!cpp true) or yield a parse error (#!cpp false); (optional, #!cpp false by default)

Return value

Whether the input is valid JSON.

Exception safety

Strong guarantee: this function itself never throws for an invalid input; it can only throw what allocating the input's own copy (for inputs that are always read into a buffer) throws.

Complexity

Linear in the length of the input.

Notes

An integer argument that is not a #!cpp bool where the flags are expected, such as #!cpp accept(ptr, len), does not compile; see parse.

Examples

??? example

```cpp
--8<-- "examples/basic_json_document__accept.cpp"
```

Output:

```json
--8<-- "examples/basic_json_document__accept.output"
```

See also

Version history

  • Added in version 3.13.0.