rippled
Loading...
Searching...
No Matches
json_reader.h
1#ifndef XRPL_JSON_JSON_READER_H_INCLUDED
2#define XRPL_JSON_JSON_READER_H_INCLUDED
3
4#define CPPTL_JSON_READER_H_INCLUDED
5
6#include <xrpl/json/json_forwards.h>
7#include <xrpl/json/json_value.h>
8
9#include <boost/asio/buffer.hpp>
10
11#include <stack>
12
13namespace Json {
14
19class Reader
20{
21public:
22 using Char = char;
23 using Location = Char const*;
24
28 Reader() = default;
29
37 bool
38 parse(std::string const& document, Value& root);
39
47 bool
48 parse(char const* beginDoc, char const* endDoc, Value& root);
49
52 bool
53 parse(std::istream& is, Value& root);
54
61 template <class BufferSequence>
62 bool
63 parse(Value& root, BufferSequence const& bs);
64
72
73 static constexpr unsigned nest_limit{25};
74
75private:
93
94 class Token
95 {
96 public:
97 explicit Token() = default;
98
102 };
103
105 {
106 public:
107 explicit ErrorInfo() = default;
108
112 };
113
115
116 bool
117 expectToken(TokenType type, Token& token, char const* message);
118 bool
119 readToken(Token& token);
120 void
121 skipSpaces();
122 bool
123 match(Location pattern, int patternLength);
124 bool
125 readComment();
126 bool
128 bool
130 bool
131 readString();
133 readNumber();
134 bool
135 readValue(unsigned depth);
136 bool
137 readObject(Token& token, unsigned depth);
138 bool
139 readArray(Token& token, unsigned depth);
140 bool
141 decodeNumber(Token& token);
142 bool
143 decodeString(Token& token);
144 bool
145 decodeString(Token& token, std::string& decoded);
146 bool
147 decodeDouble(Token& token);
148 bool
150 Token& token,
151 Location& current,
152 Location end,
153 unsigned int& unicode);
154 bool
156 Token& token,
157 Location& current,
158 Location end,
159 unsigned int& unicode);
160 bool
161 addError(std::string const& message, Token& token, Location extra = 0);
162 bool
163 recoverFromError(TokenType skipUntilToken);
164 bool
166 std::string const& message,
167 Token& token,
168 TokenType skipUntilToken);
169 void
171 Value&
172 currentValue();
173 Char
174 getNextChar();
175 void
176 getLocationLineAndColumn(Location location, int& line, int& column) const;
178 getLocationLineAndColumn(Location location) const;
179 void
180 skipCommentTokens(Token& token);
181
191};
192
193template <class BufferSequence>
194bool
195Reader::parse(Value& root, BufferSequence const& bs)
196{
197 using namespace boost::asio;
198 std::string s;
199 s.reserve(buffer_size(bs));
200 for (auto const& b : bs)
201 s.append(static_cast<char const*>(b.data()), buffer_size(b));
202 return parse(s, root);
203}
204
231
232} // namespace Json
233
234#endif // CPPTL_JSON_READER_H_INCLUDED
T append(T... args)
Unserialize a JSON document into a Value.
Definition json_reader.h:20
Reader()=default
Constructs a Reader allowing all features for parsing.
void skipCommentTokens(Token &token)
bool addErrorAndRecover(std::string const &message, Token &token, TokenType skipUntilToken)
bool decodeDouble(Token &token)
std::string getFormatedErrorMessages() const
Returns a user friendly string that list errors in the parsed document.
Location current_
Location begin_
bool match(Location pattern, int patternLength)
bool expectToken(TokenType type, Token &token, char const *message)
bool decodeNumber(Token &token)
Char const * Location
Definition json_reader.h:23
bool decodeUnicodeEscapeSequence(Token &token, Location &current, Location end, unsigned int &unicode)
Location lastValueEnd_
bool readCppStyleComment()
Location end_
static constexpr unsigned nest_limit
Definition json_reader.h:73
bool readToken(Token &token)
bool readValue(unsigned depth)
Value & currentValue()
Value * lastValue_
void getLocationLineAndColumn(Location location, int &line, int &column) const
bool recoverFromError(TokenType skipUntilToken)
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
bool decodeUnicodeCodePoint(Token &token, Location &current, Location end, unsigned int &unicode)
bool decodeString(Token &token)
bool readArray(Token &token, unsigned depth)
bool addError(std::string const &message, Token &token, Location extra=0)
void skipUntilSpace()
bool readCStyleComment()
Reader::TokenType readNumber()
bool readObject(Token &token, unsigned depth)
std::string document_
Represents a JSON value.
Definition json_value.h:130
JSON (JavaScript Object Notation).
Definition json_errors.h:6
std::istream & operator>>(std::istream &, Value &)
Read from 'sin' into 'root'.
T reserve(T... args)