Argo  1.0
A C++ library for handling JSON.
Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
argo::parser Class Reference

A recursive decent parser for JSON messages. More...

#include <parser.hpp>

Public Member Functions

 parser (reader &r, bool read_all=true, size_t p_max_token_length=max_token_length, size_t p_max_nesting_depth=max_nesting_depth, bool p_convert_numbers=true, bool p_fallback_to_double=true, bool p_convert_strings=true)
 
std::unique_ptr< jsonparse ()
 

Static Public Member Functions

static std::unique_ptr< jsonparse (std::istream &i)
 
static std::unique_ptr< jsonparse (int fd)
 
static std::unique_ptr< jsonparse (FILE *f)
 
static std::unique_ptr< jsonparse (const char *s)
 
static std::unique_ptr< jsonparse (const std::string &s)
 
static std::unique_ptr< jsonload (const std::string &file_name)
 

Static Public Attributes

static const size_t max_message_length = 10*1024*1024
 The default maximum length of a message that can be parsed.
 
static const size_t max_token_length = 100*1024
 
static const size_t max_nesting_depth = 1000
 

Detailed Description

A recursive decent parser for JSON messages.

A class to parse JSON messages from an input source and store the results in a json object instance.

Definition at line 55 of file parser.hpp.

Constructor & Destructor Documentation

parser::parser ( reader r,
bool  read_all = true,
size_t  p_max_token_length = max_token_length,
size_t  p_max_nesting_depth = max_nesting_depth,
bool  p_convert_numbers = true,
bool  p_fallback_to_double = true,
bool  p_convert_strings = true 
)

Constructor. Create a parser that will read from the given reader. If you need to read from some other type of source, then create an appropriate derived class of reader and pass that in here.

Parameters
rWhere to read the message from.
read_allIf true, read the whole file to the end and throw an exception if there is anything other than white space following a parseable JSON message. If false, read until the end of the first parseable JSON message and leave it at that.
p_max_nesting_depthThe maximum total depth of nested objects and arrays allowed.
p_max_token_lengthThe maximum length of any individual token, You can change this if you have a strangely huge string in a message or you can make it smaller if you have very constrained memory availability.
p_convert_numbersIf true, convert numbers to ints or doubles as appropriate. If false, create the json instance using the json(type, raw_string) method but don't do the numeric conversion. This allows the caller to write custom code to handle numbers outside of the normal int & double ranges.
p_fallback_to_doubleIf true, convert integers that are too large to be stored as an int into doubles instead.
p_convert_stringsIf true, convert strings into UTF-8 encoded STL strings. If false, create the json instance using the json(type, raw_string) method but leave the caller to do any conversion. This is useful when dealing with messages with broken Unicode characters and the like in them.
Exceptions
json_parser_exceptionThrown when there is something syntactically wrong with the message.
json_io_exceptionThrown when something goes wrong with reading.
json_utf_exceptionThrown when an invalid string is found in the message.

Definition at line 47 of file parser.cpp.

Member Function Documentation

unique_ptr< json > parser::load ( const std::string &  file_name)
static

Convenience method. Open a file, parse the entire contents, then close it. The file_name parameter is the name of the file.

Parameters
file_nameFile name to read the message from.
Exceptions
json_io_exceptionThrown when something goes wrong with reading.
json_utf_exceptionThrown when an invalid string is found in the message.

Definition at line 365 of file parser.cpp.

unique_ptr< json > parser::parse ( std::istream &  i)
static

Convenience method. Parse a JSON message read from an istream. If you need more detailed control of the options, create a reader and parser object directly.

Parameters
iWhere to read the message from.
Exceptions
json_io_exceptionThrown when something goes wrong with reading.
json_utf_exceptionThrown when an invalid string is found in the message.

Definition at line 330 of file parser.cpp.

unique_ptr< json > parser::parse ( int  fd)
static

Convenience method. Parse a JSON message read from a POSIX file descriptor. If you need more detailed control of the options, create a reader and parser object directly.

Parameters
fdWhere to read the message from.
Exceptions
json_io_exceptionThrown when something goes wrong with reading.
json_utf_exceptionThrown when an invalid string is found in the message.

Definition at line 338 of file parser.cpp.

unique_ptr< json > parser::parse ( FILE *  f)
static

Convenience method. Parse a JSON message read from a stdio file. If you need more detailed control of the options, create a reader and parser object directly.

Parameters
fWhere to read the message from.
Exceptions
json_io_exceptionThrown when something goes wrong with reading.
json_utf_exceptionThrown when an invalid string is found in the message.

Definition at line 346 of file parser.cpp.

unique_ptr< json > parser::parse ( const char *  s)
static

Convenience method. Parse a JSON message read from a string. If you need more detailed control of the options, create a reader and parser object directly.

Parameters
sWhere to read the message from.
Exceptions
json_io_exceptionThrown when something goes wrong with reading.
json_utf_exceptionThrown when an invalid string is found in the message.

Definition at line 353 of file parser.cpp.

unique_ptr< json > parser::parse ( const std::string &  s)
static

Convenience method. Parse a JSON message read from a string. If you need more detailed control of the options, create a reader and parser object directly.

Parameters
sWhere to read the message from.
Exceptions
json_io_exceptionThrown when something goes wrong with reading.
json_utf_exceptionThrown when an invalid string is found in the message.

Definition at line 359 of file parser.cpp.

unique_ptr< json > parser::parse ( )

Parse a single json object from the stream. The object is dynamically allocated to prevent the need to make a deep copy at any point.

Definition at line 306 of file parser.cpp.

Member Data Documentation

const size_t argo::parser::max_nesting_depth = 1000
static

The default maximum nesting depth (objects and arrays) of a message that can be parsed.

Definition at line 72 of file parser.hpp.

const size_t argo::parser::max_token_length = 100*1024
static

The default maximum length of any individual lexical token (string and number being the relevant ones in this context).

Definition at line 66 of file parser.hpp.


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