A recursive decent parser for JSON messages.
More...
#include <parser.hpp>
|
static std::unique_ptr< json > | parse (std::istream &i) |
|
static std::unique_ptr< json > | parse (int fd) |
|
static std::unique_ptr< json > | parse (FILE *f) |
|
static std::unique_ptr< json > | parse (const char *s) |
|
static std::unique_ptr< json > | parse (const std::string &s) |
|
static std::unique_ptr< json > | load (const std::string &file_name) |
|
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.
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
-
r | Where to read the message from. |
read_all | If 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_depth | The maximum total depth of nested objects and arrays allowed. |
p_max_token_length | The 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_numbers | If 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_double | If true, convert integers that are too large to be stored as an int into doubles instead. |
p_convert_strings | If 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_exception | Thrown when there is something syntactically wrong with the message. |
json_io_exception | Thrown when something goes wrong with reading. |
json_utf_exception | Thrown when an invalid string is found in the message. |
Definition at line 47 of file parser.cpp.
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_name | File name to read the message from. |
- Exceptions
-
json_io_exception | Thrown when something goes wrong with reading. |
json_utf_exception | Thrown 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
-
i | Where to read the message from. |
- Exceptions
-
json_io_exception | Thrown when something goes wrong with reading. |
json_utf_exception | Thrown 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
-
fd | Where to read the message from. |
- Exceptions
-
json_io_exception | Thrown when something goes wrong with reading. |
json_utf_exception | Thrown 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
-
f | Where to read the message from. |
- Exceptions
-
json_io_exception | Thrown when something goes wrong with reading. |
json_utf_exception | Thrown 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
-
s | Where to read the message from. |
- Exceptions
-
json_io_exception | Thrown when something goes wrong with reading. |
json_utf_exception | Thrown 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
-
s | Where to read the message from. |
- Exceptions
-
json_io_exception | Thrown when something goes wrong with reading. |
json_utf_exception | Thrown 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.
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: