Files
json/docs/mkdocs/docs/api/basic_json_document/accept.md
T
Niels Lohmann f64464daf1 Split the json_view feature page into sub-pages
The single features/json_view.md page grew to ~600 lines covering eight
topics. Split it into features/json_view/ with an overview page and
pages for ownership and lifetime, differences from basic_json, reading
values, generic code, editing, and images.

The new differences page collects what was spread over several sections
("What is the same", "What is different", "Duplicate keys", and notes in
the conversion and serialization sections) under one summary table, and
adds the parts of the basic_json interface a view does not have. The
other pages now link to it instead of restating the rules.

Internal links across the documentation point to the new pages.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-11-02 20:42:55 +01:00

3.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.

The result is the same as that of BasicJsonType::accept() by design; the difference is how the check is made. This function runs the document's builder, which checks the text as parse() does, while BasicJsonType::accept() runs the SAX parser with a handler that ignores every event. Use this one if you go on to parse the same input with parse() anyway, so that both use the same code; either is fine for a plain yes/no answer.

When both ignore_comments and ignore_trailing_commas are #!cpp false (the default), the builder writes no index. With either option set, it builds the index as parse() does and discards it: the result is the same, only the time differs. (Neither option needs the index; the variant that skips it exists only for the default options to keep the code size down.)

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.