rippled
Loading...
Searching...
No Matches
json_reader.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_JSON_JSON_READER_H_INCLUDED
21#define RIPPLE_JSON_JSON_READER_H_INCLUDED
22
23#define CPPTL_JSON_READER_H_INCLUDED
24
25#include <xrpl/json/json_forwards.h>
26#include <xrpl/json/json_value.h>
27
28#include <boost/asio/buffer.hpp>
29
30#include <stack>
31
32namespace Json {
33
38class Reader
39{
40public:
41 using Char = char;
42 using Location = Char const*;
43
47 Reader() = default;
48
56 bool
57 parse(std::string const& document, Value& root);
58
66 bool
67 parse(char const* beginDoc, char const* endDoc, Value& root);
68
71 bool
72 parse(std::istream& is, Value& root);
73
80 template <class BufferSequence>
81 bool
82 parse(Value& root, BufferSequence const& bs);
83
91
92 static constexpr unsigned nest_limit{25};
93
94private:
95 enum TokenType {
111 };
112
113 class Token
114 {
115 public:
116 explicit Token() = default;
117
121 };
122
124 {
125 public:
126 explicit ErrorInfo() = default;
127
131 };
132
134
135 bool
136 expectToken(TokenType type, Token& token, char const* message);
137 bool
138 readToken(Token& token);
139 void
140 skipSpaces();
141 bool
142 match(Location pattern, int patternLength);
143 bool
144 readComment();
145 bool
147 bool
149 bool
150 readString();
152 readNumber();
153 bool
154 readValue(unsigned depth);
155 bool
156 readObject(Token& token, unsigned depth);
157 bool
158 readArray(Token& token, unsigned depth);
159 bool
160 decodeNumber(Token& token);
161 bool
162 decodeString(Token& token);
163 bool
164 decodeString(Token& token, std::string& decoded);
165 bool
166 decodeDouble(Token& token);
167 bool
169 Token& token,
170 Location& current,
171 Location end,
172 unsigned int& unicode);
173 bool
175 Token& token,
176 Location& current,
177 Location end,
178 unsigned int& unicode);
179 bool
180 addError(std::string const& message, Token& token, Location extra = 0);
181 bool
182 recoverFromError(TokenType skipUntilToken);
183 bool
185 std::string const& message,
186 Token& token,
187 TokenType skipUntilToken);
188 void
190 Value&
191 currentValue();
192 Char
193 getNextChar();
194 void
195 getLocationLineAndColumn(Location location, int& line, int& column) const;
197 getLocationLineAndColumn(Location location) const;
198 void
199 skipCommentTokens(Token& token);
200
210};
211
212template <class BufferSequence>
213bool
214Reader::parse(Value& root, BufferSequence const& bs)
215{
216 using namespace boost::asio;
217 std::string s;
218 s.reserve(buffer_size(bs));
219 for (auto const& b : bs)
220 s.append(buffer_cast<char const*>(b), buffer_size(b));
221 return parse(s, root);
222}
223
250
251} // namespace Json
252
253#endif // CPPTL_JSON_READER_H_INCLUDED
T append(T... args)
Unserialize a JSON document into a Value.
Definition: json_reader.h:39
Reader()=default
Constructs a Reader allowing all features for parsing.
Errors errors_
Definition: json_reader.h:203
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_
Definition: json_reader.h:207
Location begin_
Definition: json_reader.h:205
bool match(Location pattern, int patternLength)
void skipSpaces()
bool expectToken(TokenType type, Token &token, char const *message)
bool decodeNumber(Token &token)
Char const * Location
Definition: json_reader.h:42
bool decodeUnicodeEscapeSequence(Token &token, Location &current, Location end, unsigned int &unicode)
Location lastValueEnd_
Definition: json_reader.h:208
bool readString()
bool readCppStyleComment()
Location end_
Definition: json_reader.h:206
static constexpr unsigned nest_limit
Definition: json_reader.h:92
bool readToken(Token &token)
bool readValue(unsigned depth)
Value & currentValue()
Value * lastValue_
Definition: json_reader.h:209
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.
Definition: json_reader.cpp:78
bool decodeUnicodeCodePoint(Token &token, Location &current, Location end, unsigned int &unicode)
@ tokenMemberSeparator
Definition: json_reader.h:108
bool decodeString(Token &token)
bool readArray(Token &token, unsigned depth)
Char getNextChar()
bool addError(std::string const &message, Token &token, Location extra=0)
bool readComment()
void skipUntilSpace()
bool readCStyleComment()
Reader::TokenType readNumber()
bool readObject(Token &token, unsigned depth)
std::string document_
Definition: json_reader.h:204
Represents a JSON value.
Definition: json_value.h:148
JSON (JavaScript Object Notation).
Definition: json_errors.h:25
std::istream & operator>>(std::istream &, Value &)
Read from 'sin' into 'root'.
T reserve(T... args)