JSON Voorhees
Killer JSON for C++
jsonv::parse_options Class Reference

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_optionsfailure_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_optionsmax_failures (std::size_t limit)
 
encoding string_encoding () const
 The output encoding for multi-byte characters in strings. More...
 
parse_optionsstring_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_optionsnumber_encoding (numbers)
 
commas comma_policy () const
 How should extra commas be treated? By default, this is commas::allow_trailing. More...
 
parse_optionscomma_policy (commas)
 
size_type max_structure_depth () const
 The maximum allowed nesting depth of any structure in the JSON document. More...
 
parse_optionsmax_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_optionsrequire_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_optionscomplete_parse (bool)
 
bool comments () const
 Are JSON comments allowed? More...
 
parse_optionscomments (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...
 

Detailed Description

Configuration for various parsing options.

All parse functions should take in a parse_options as a paramter and should respect your settings.

Examples:
parse(tokenizer&, const parse_options&).

Definition at line 114 of file parse.hpp.

Member Enumeration Documentation

When dealing with comma separators, how should extra commas be treated?

Enumerator
strict 

Do not allow any extra commas anywhere – require valid JSON.

allow_trailing 

Allow a single trailing comma at the end of an array or object (similar to C++ enum definitions).

Definition at line 155 of file parse.hpp.

The encoding format for strings.

Enumerator
utf8 

Use UTF-8 like a sane library should.

See also
http://www.unicode.org/versions/Unicode6.2.0/ch03.pdf#G7404
utf8_strict 

Like utf8, but check that there are no unprintable characters in the input stream (see std::isprint).

To contrast this with utf8, this mode will reject things such as the tab and newline characters, while this will reject them.

cesu8 

Use the CESU-8 Compatibility Encoding Scheme for UTF-16? It is generally not recommended unless your processing environment requires binary collation with UTF-16.

If you do not know you need this, you probably do not.

See also
http://www.unicode.org/reports/tr26/

Definition at line 133 of file parse.hpp.

How should numbers be dealt with?

Enumerator
decimal 

Parse all forms of decimal input that we can.

To contrast this from strict, the decimal does not allow leading zeros on numbers.

strict 

Strictly comply with the JSON specification for numbers – no leading zeros!

Definition at line 164 of file parse.hpp.

When a parse error is encountered, what should the parser do?

Enumerator
fail_immediately 

Immediately throw a parse_error – do not attempt to construct a partial result.

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.

Definition at line 120 of file parse.hpp.

Constructor & Destructor Documentation

jsonv::parse_options::parse_options ( )

Create an instance with the default options.

Member Function Documentation

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?

Warning
There is no "official" syntax for JSON comments, but this system allows
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/).

Warning
When using this option, it is best to construct a 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.
Examples:
parse(tokenizer&, const parse_options&).
static parse_options jsonv::parse_options::create_strict ( )
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.


The documentation for this class was generated from the following file: