JSON Voorhees
Killer JSON for C++
|
Configuration for various parsing options. More...
#include <jsonv/parse.hpp>
Public Types | |
enum | on_error { on_error::fail_immediately, on_error::collect_all, on_error::ignore } |
When a parse error is encountered, what should the parser do? More... | |
enum | encoding { encoding::utf8, encoding::utf8_strict, encoding::cesu8 } |
The encoding format for strings. More... | |
enum | commas { commas::strict, commas::allow_trailing } |
When dealing with comma separators, how should extra commas be treated? More... | |
enum | numbers { numbers::decimal, numbers::strict } |
How should numbers be dealt with? More... | |
using | size_type = value::size_type |
Public Member Functions | |
parse_options () | |
Create an instance with the default options. More... | |
on_error | failure_mode () const |
See on_error . More... | |
parse_options & | failure_mode (on_error mode) |
std::size_t | max_failures () const |
The maximum allowed parsing failures the parser can encounter before throwing an error. More... | |
parse_options & | max_failures (std::size_t limit) |
encoding | string_encoding () const |
The output encoding for multi-byte characters in strings. More... | |
parse_options & | string_encoding (encoding) |
numbers | number_encoding () const |
How should a parser interpret numbers? By default, this is numbers::decimal , which allows any form of decimal input. | |
parse_options & | number_encoding (numbers) |
commas | comma_policy () const |
How should extra commas be treated? By default, this is commas::allow_trailing . More... | |
parse_options & | comma_policy (commas) |
size_type | max_structure_depth () const |
The maximum allowed nesting depth of any structure in the JSON document. More... | |
parse_options & | max_structure_depth (size_type depth) |
bool | require_document () const |
If set to true, the result of a parse is required to have kind of kind::object or kind::array . More... | |
parse_options & | require_document (bool) |
bool | complete_parse () const |
Should the input be completely parsed to consider the parsing a success? This is on by default. More... | |
parse_options & | complete_parse (bool) |
bool | comments () const |
Are JSON comments allowed? More... | |
parse_options & | comments (bool) |
Static Public Member Functions | |
static parse_options | create_default () |
Create a parser with the default options – this is the same result as the default constructor, but might be helpful if you like to be more explicit. | |
static parse_options | create_strict () |
Create a strict parser. More... | |
Configuration for various parsing options.
All parse functions should take in a parse_options
as a paramter and should respect your settings.
|
strong |
|
strong |
The encoding format for strings.
|
strong |
|
strong |
When a parse error is encountered, what should the parser do?
Enumerator | |
---|---|
fail_immediately |
Immediately throw a |
collect_all |
Attempt to continue parsing and constructing a result. |
ignore |
Ignore all errors and pretend to be successful. This is not recommended unless you are 100% certain the JSON you are attempting to parse is valid. Using this failure mode does not improve parser performance. |
jsonv::parse_options::parse_options | ( | ) |
Create an instance with the default options.
commas jsonv::parse_options::comma_policy | ( | ) | const |
How should extra commas be treated? By default, this is commas::allow_trailing
.
bool jsonv::parse_options::comments | ( | ) | const |
Are JSON comments allowed?
bool jsonv::parse_options::complete_parse | ( | ) | const |
Should the input be completely parsed to consider the parsing a success? This is on by default.
Disabling this option can be useful for situations where JSON input is coming from some stream and you wish to process distinct objects separately (this technique is used to great effect in jq: http://stedolan.github.io/jq/).
tokenizer
for your input stream and reuse that. The parse
functions all internally buffer your istream
and while they attempt to use putback
re-put characters back into the istream
, they are not necessarily successful at doing so.
|
static |
Create a strict parser.
In general, these options are meant to fail on anything that is not a 100% valid JSON document. More specifically:
on_error jsonv::parse_options::failure_mode | ( | ) | const |
See on_error
.
The default failure mode is fail_immediately
.
std::size_t jsonv::parse_options::max_failures | ( | ) | const |
The maximum allowed parsing failures the parser can encounter before throwing an error.
This is only applicable if the failure_mode
is not on_error::fail_immediately
. By default, this value is 10.
You should probably not set this value to an unreasonably high number, as each parse error encountered must be stored in memory for some period of time.
size_type jsonv::parse_options::max_structure_depth | ( | ) | const |
The maximum allowed nesting depth of any structure in the JSON document.
The JSON specification technically limits the depth to 20, but very few implementations actually conform to this, so it is fairly dangerous to set this value. By default, the value is 0, which means we should not do any depth checking.
bool jsonv::parse_options::require_document | ( | ) | const |
If set to true, the result of a parse is required to have kind
of kind::object
or kind::array
.
By default, this is turned off, which will allow parse
to return incomplete documents.
encoding jsonv::parse_options::string_encoding | ( | ) | const |
The output encoding for multi-byte characters in strings.
The default value is UTF-8 because UTF-8 is best. Keep in mind this changes the output encoding for all decoded strings. If you need mixed encodings, you must handle that in your application.