xrpld
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 <deque>
9#include <istream>
10#include <stack>
11#include <string>
12
13namespace json {
14
19class Reader
20{
21public:
22 using Char = char;
23 using Location = Char const*;
24
29 Reader() = default;
30
39 bool
40 parse(std::string const& document, Value& root);
41
50 bool
51 parse(char const* beginDoc, char const* endDoc, Value& root);
52
57 bool
58 parse(std::istream& is, Value& root);
59
67 template <class BufferSequence>
68 bool
69 parse(Value& root, BufferSequence const& bs);
70
77 [[nodiscard]] std::string
79
80 static constexpr unsigned kNestLimit{25};
81
82private:
100
101 class Token
102 {
103 public:
104 explicit Token() = default;
105
109 };
110
112 {
113 public:
114 explicit ErrorInfo() = default;
115
119 };
120
122
123 bool
124 expectToken(TokenType type, Token& token, char const* message);
125 bool
126 readToken(Token& token);
127 void
128 skipSpaces();
129 bool
130 match(Location pattern, int patternLength);
131 bool
132 readComment();
133 bool
135 bool
137 bool
138 readString();
140 readNumber();
141 bool
142 readValue(unsigned depth);
143 bool
144 readObject(Token& token, unsigned depth);
145 bool
146 readArray(Token& token, unsigned depth);
147 bool
148 decodeNumber(Token& token);
149 bool
150 decodeString(Token& token);
151 bool
152 decodeString(Token& token, std::string& decoded);
153 bool
154 decodeDouble(Token& token);
155 bool
156 decodeUnicodeCodePoint(Token& token, Location& current, Location end, unsigned int& unicode);
157 bool
159 Token& token,
160 Location& current,
161 Location end,
162 unsigned int& unicode);
163 bool
164 addError(std::string const& message, Token& token, Location extra = nullptr);
165 bool
166 recoverFromError(TokenType skipUntilToken);
167 bool
168 addErrorAndRecover(std::string const& message, Token& token, 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
232
233} // namespace json
T append(T... args)
Location current_
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
bool recoverFromError(TokenType skipUntilToken)
Reader()=default
Constructs a Reader allowing all features for parsing.
bool decodeUnicodeCodePoint(Token &token, Location &current, Location end, unsigned int &unicode)
bool readObject(Token &token, unsigned depth)
std::string getFormattedErrorMessages() const
Returns a user friendly string that list errors in the parsed document.
void skipCommentTokens(Token &token)
bool addError(std::string const &message, Token &token, Location extra=nullptr)
bool decodeDouble(Token &token)
Location end_
static constexpr unsigned kNestLimit
Definition json_reader.h:80
bool readArray(Token &token, unsigned depth)
Value * lastValue_
Reader::TokenType readNumber()
void getLocationLineAndColumn(Location location, int &line, int &column) const
std::stack< Value * > Nodes
Char const * Location
Definition json_reader.h:23
bool decodeNumber(Token &token)
Value & currentValue()
Location begin_
std::string document_
void skipUntilSpace()
bool readValue(unsigned depth)
bool readCStyleComment()
bool readCppStyleComment()
bool expectToken(TokenType type, Token &token, char const *message)
std::deque< ErrorInfo > Errors
Location lastValueEnd_
bool decodeUnicodeEscapeSequence(Token &token, Location &current, Location end, unsigned int &unicode)
bool addErrorAndRecover(std::string const &message, Token &token, TokenType skipUntilToken)
bool match(Location pattern, int patternLength)
bool readToken(Token &token)
bool decodeString(Token &token)
Represents a JSON value.
Definition json_value.h:117
JSON (JavaScript Object Notation).
Definition json_errors.h:5
std::istream & operator>>(std::istream &, Value &)
Read from 'sin' into 'root'.
T reserve(T... args)