rippled
Loading...
Searching...
No Matches
json_reader.h
1#pragma once
2
3#include <xrpl/json/json_forwards.h>
4#include <xrpl/json/json_value.h>
5
6#include <boost/asio/buffer.hpp>
7
8#include <stack>
9
10namespace Json {
11
16class Reader
17{
18public:
19 using Char = char;
20 using Location = Char const*;
21
25 Reader() = default;
26
34 bool
35 parse(std::string const& document, Value& root);
36
44 bool
45 parse(char const* beginDoc, char const* endDoc, Value& root);
46
49 bool
50 parse(std::istream& is, Value& root);
51
58 template <class BufferSequence>
59 bool
60 parse(Value& root, BufferSequence const& bs);
61
69
70 static constexpr unsigned nest_limit{25};
71
72private:
90
91 class Token
92 {
93 public:
94 explicit Token() = default;
95
99 };
100
102 {
103 public:
104 explicit ErrorInfo() = default;
105
109 };
110
112
113 bool
114 expectToken(TokenType type, Token& token, char const* message);
115 bool
116 readToken(Token& token);
117 void
118 skipSpaces();
119 bool
120 match(Location pattern, int patternLength);
121 bool
122 readComment();
123 bool
125 bool
127 bool
128 readString();
130 readNumber();
131 bool
132 readValue(unsigned depth);
133 bool
134 readObject(Token& token, unsigned depth);
135 bool
136 readArray(Token& token, unsigned depth);
137 bool
138 decodeNumber(Token& token);
139 bool
140 decodeString(Token& token);
141 bool
142 decodeString(Token& token, std::string& decoded);
143 bool
144 decodeDouble(Token& token);
145 bool
146 decodeUnicodeCodePoint(Token& token, Location& current, Location end, unsigned int& unicode);
147 bool
148 decodeUnicodeEscapeSequence(Token& token, Location& current, Location end, unsigned int& unicode);
149 bool
150 addError(std::string const& message, Token& token, Location extra = 0);
151 bool
152 recoverFromError(TokenType skipUntilToken);
153 bool
154 addErrorAndRecover(std::string const& message, Token& token, TokenType skipUntilToken);
155 void
157 Value&
158 currentValue();
159 Char
160 getNextChar();
161 void
162 getLocationLineAndColumn(Location location, int& line, int& column) const;
164 getLocationLineAndColumn(Location location) const;
165 void
166 skipCommentTokens(Token& token);
167
177};
178
179template <class BufferSequence>
180bool
181Reader::parse(Value& root, BufferSequence const& bs)
182{
183 using namespace boost::asio;
184 std::string s;
185 s.reserve(buffer_size(bs));
186 for (auto const& b : bs)
187 s.append(static_cast<char const*>(b.data()), buffer_size(b));
188 return parse(s, root);
189}
190
217
218} // namespace Json
T append(T... args)
Unserialize a JSON document into a Value.
Definition json_reader.h:17
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:20
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:70
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:5
std::istream & operator>>(std::istream &, Value &)
Read from 'sin' into 'root'.
T reserve(T... args)