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#include <xrpl/json/json_forwards.h>
5#include <xrpl/json/json_value.h>
6
7#include <boost/asio/buffer.hpp>
8
9#include <stack>
10
11namespace Json {
12
17class Reader
18{
19public:
20 using Char = char;
21 using Location = Char const*;
22
26 Reader() = default;
27
35 bool
36 parse(std::string const& document, Value& root);
37
45 bool
46 parse(char const* beginDoc, char const* endDoc, Value& root);
47
50 bool
51 parse(std::istream& is, Value& root);
52
59 template <class BufferSequence>
60 bool
61 parse(Value& root, BufferSequence const& bs);
62
70
71 static constexpr unsigned nest_limit{25};
72
73private:
91
92 class Token
93 {
94 public:
95 explicit Token() = default;
96
100 };
101
103 {
104 public:
105 explicit ErrorInfo() = default;
106
110 };
111
113
114 bool
115 expectToken(TokenType type, Token& token, char const* message);
116 bool
117 readToken(Token& token);
118 void
119 skipSpaces();
120 bool
121 match(Location pattern, int patternLength);
122 bool
123 readComment();
124 bool
126 bool
128 bool
129 readString();
131 readNumber();
132 bool
133 readValue(unsigned depth);
134 bool
135 readObject(Token& token, unsigned depth);
136 bool
137 readArray(Token& token, unsigned depth);
138 bool
139 decodeNumber(Token& token);
140 bool
141 decodeString(Token& token);
142 bool
143 decodeString(Token& token, std::string& decoded);
144 bool
145 decodeDouble(Token& token);
146 bool
148 Token& token,
149 Location& current,
150 Location end,
151 unsigned int& unicode);
152 bool
154 Token& token,
155 Location& current,
156 Location end,
157 unsigned int& unicode);
158 bool
159 addError(std::string const& message, Token& token, Location extra = 0);
160 bool
161 recoverFromError(TokenType skipUntilToken);
162 bool
164 std::string const& message,
165 Token& token,
166 TokenType skipUntilToken);
167 void
169 Value&
170 currentValue();
171 Char
172 getNextChar();
173 void
174 getLocationLineAndColumn(Location location, int& line, int& column) const;
176 getLocationLineAndColumn(Location location) const;
177 void
178 skipCommentTokens(Token& token);
179
189};
190
191template <class BufferSequence>
192bool
193Reader::parse(Value& root, BufferSequence const& bs)
194{
195 using namespace boost::asio;
196 std::string s;
197 s.reserve(buffer_size(bs));
198 for (auto const& b : bs)
199 s.append(static_cast<char const*>(b.data()), buffer_size(b));
200 return parse(s, root);
201}
202
229
230} // namespace Json
231
232#endif // XRPL_JSON_JSON_READER_H_INCLUDED
T append(T... args)
Unserialize a JSON document into a Value.
Definition json_reader.h:18
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)
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:21
bool decodeUnicodeEscapeSequence(Token &token, Location &current, Location end, unsigned int &unicode)
Location lastValueEnd_
std::string getFormattedErrorMessages() const
Returns a user friendly string that list errors in the parsed document.
bool readCppStyleComment()
Location end_
static constexpr unsigned nest_limit
Definition json_reader.h:71
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:131
JSON (JavaScript Object Notation).
Definition json_errors.h:6
std::istream & operator>>(std::istream &, Value &)
Read from 'sin' into 'root'.
T reserve(T... args)