40 #ifndef _ARGO_WINDOWS_ 50 size_t p_max_token_length,
51 size_t p_max_nesting_depth,
52 bool p_convert_numbers,
53 bool p_fallback_to_double,
54 bool p_convert_strings) :
57 m_max_token_length(p_max_token_length),
58 m_max_nesting_depth(p_max_nesting_depth),
59 m_convert_numbers(p_convert_numbers),
60 m_fallback_to_double(p_fallback_to_double),
61 m_convert_strings(p_convert_strings)
65 unique_ptr<json> parser::parse_number_int(
const token &t)
73 return unique_ptr<json>(
new json(i));
78 if (m_fallback_to_double)
80 return parse_number_double(t);
92 unique_ptr<json> parser::parse_number_double(
const token &t)
97 if (is >> d && isfinite(d))
99 return unique_ptr<json>(
new json(d));
111 unique_ptr<json> parser::parse_value(
lexer &l,
size_t nesting_depth)
118 return parse_object(l, nesting_depth + 1);
120 return parse_array(l, nesting_depth + 1);
122 if (m_convert_numbers)
124 return parse_number_int(t);
131 if (m_convert_numbers)
133 return parse_number_double(t);
140 if (m_convert_strings)
157 return unique_ptr<json>(
new json(
false));
159 return unique_ptr<json>(
new json(
true));
161 return unique_ptr<json>(
new json);
170 unique_ptr<json> parser::parse_array(
lexer &l,
size_t nesting_depth)
172 if (nesting_depth > m_max_nesting_depth)
180 unique_ptr<json> array = unique_ptr<json>(
new json(json::array_e));
191 array->get_array().push_back(parse_value(l, nesting_depth));
200 array->get_array().push_back(parse_value(l, nesting_depth));
218 void parser::parse_name_value_pair(
lexer &l, unique_ptr<json> &
object,
size_t nesting_depth)
226 if (m_convert_strings)
253 object->get_object()[name] = parse_value(l, nesting_depth);
256 unique_ptr<json> parser::parse_object(
lexer &l,
size_t nesting_depth)
258 if (nesting_depth > m_max_nesting_depth)
266 unique_ptr<json>
object = unique_ptr<json>(
new json(json::object_e));
282 parse_name_value_pair(l,
object, nesting_depth);
310 lexer l(m_reader, m_max_token_length);
312 auto res = parse_value(l, 0);
319 while ((c = m_reader.
next()) != EOF)
321 if ((c != 0x20) && (c != 0x09) && (c != 0x0A) && (c != 0x0D))
337 #ifndef _ARGO_WINDOWS_ 367 ifstream is(file_name);
379 istream &NAMESPACE::operator>>(istream &stream,
json &j)
385 void NAMESPACE::operator>>(
string &s,
json &j)
A derived class of reader that reads from stdio FILEs.
The json_parser_exception class.
static const size_t max_message_length
The default maximum length of a message that can be parsed.
const std::string & get_raw_value() const
Get the raw untranslated JSON value.
Exception class for parser errors.
A class to read JSON messages from various types of input stream.
virtual int next()
Get the next character from the reader.
#define NAMESPACE
You can change the namespace of the whole library by changing this value.
Specific class of exceptions for IO errors of various types.
A recursive decent parser for JSON messages.
A number was too large or small to convert accrurately to an int or double.
A derived class of reader that reads from POSIX file descriptors.
void add_byte_index(size_t byte_index) noexcept
Add the byte index where the error occured to the exception.
Common defs needed everywhere and, as far as is possible, platform specific changes.
Attempt to open file failed.
The json_utf8_exception class.
static std::unique_ptr< json > load(const std::string &file_name)
Lexical tokens read from a JSON message.
std::unique_ptr< json > parse()
size_t get_byte_index() const
Get the current byte index in the input.
All json things are represented by instances of this class.
A lexical analyser for JSON messages.
static std::unique_ptr< std::string > json_string_to_utf8(const std::string &src)
A class to read JSON messages from C++ istreams.
Maximum nesting depth exceeded.
An invalid character was found during parsing (e.g. ! outside of a string).
Exception class for errors translating to and from UTF-8 strings.
A valid, but unexpected, lexical token was found (e.g. { {).
token_type get_type() const
Get the token type.
static std::unique_ptr< json > parse(std::istream &i)
The json_io_exception class.
void reset_byte_index()
Reset the byte index at the start of parsing a messege.