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#include <boost/asio/buffer.hpp>
28#include <stack>
29
30namespace Json {
31
36class Reader
37{
38public:
39 using Char = char;
40 using Location = const Char*;
41
45 Reader() = default;
46
54 bool
55 parse(std::string const& document, Value& root);
56
64 bool
65 parse(const char* beginDoc, const char* endDoc, Value& root);
66
69 bool
70 parse(std::istream& is, Value& root);
71
78 template <class BufferSequence>
79 bool
80 parse(Value& root, BufferSequence const& bs);
81
89
90 static constexpr unsigned nest_limit{25};
91
92private:
93 enum TokenType {
109 };
110
111 class Token
112 {
113 public:
114 explicit Token() = default;
115
119 };
120
122 {
123 public:
124 explicit ErrorInfo() = default;
125
129 };
130
132
133 bool
134 expectToken(TokenType type, Token& token, const char* message);
135 bool
136 readToken(Token& token);
137 void
138 skipSpaces();
139 bool
140 match(Location pattern, int patternLength);
141 bool
142 readComment();
143 bool
145 bool
147 bool
148 readString();
150 readNumber();
151 bool
152 readValue(unsigned depth);
153 bool
154 readObject(Token& token, unsigned depth);
155 bool
156 readArray(Token& token, unsigned depth);
157 bool
158 decodeNumber(Token& token);
159 bool
160 decodeString(Token& token);
161 bool
162 decodeString(Token& token, std::string& decoded);
163 bool
164 decodeDouble(Token& token);
165 bool
167 Token& token,
168 Location& current,
169 Location end,
170 unsigned int& unicode);
171 bool
173 Token& token,
174 Location& current,
175 Location end,
176 unsigned int& unicode);
177 bool
178 addError(std::string const& message, Token& token, Location extra = 0);
179 bool
180 recoverFromError(TokenType skipUntilToken);
181 bool
183 std::string const& message,
184 Token& token,
185 TokenType skipUntilToken);
186 void
188 Value&
189 currentValue();
190 Char
191 getNextChar();
192 void
193 getLocationLineAndColumn(Location location, int& line, int& column) const;
195 getLocationLineAndColumn(Location location) const;
196 void
197 skipCommentTokens(Token& token);
198
208};
209
210template <class BufferSequence>
211bool
212Reader::parse(Value& root, BufferSequence const& bs)
213{
214 using namespace boost::asio;
215 std::string s;
216 s.reserve(buffer_size(bs));
217 for (auto const& b : bs)
218 s.append(buffer_cast<char const*>(b), buffer_size(b));
219 return parse(s, root);
220}
221
248
249} // namespace Json
250
251#endif // CPPTL_JSON_READER_H_INCLUDED
T append(T... args)
Unserialize a JSON document into a Value.
Definition: json_reader.h:37
Reader()=default
Constructs a Reader allowing all features for parsing.
Errors errors_
Definition: json_reader.h:201
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:205
Location begin_
Definition: json_reader.h:203
bool match(Location pattern, int patternLength)
void skipSpaces()
bool decodeNumber(Token &token)
bool expectToken(TokenType type, Token &token, const char *message)
bool decodeUnicodeEscapeSequence(Token &token, Location &current, Location end, unsigned int &unicode)
Location lastValueEnd_
Definition: json_reader.h:206
bool readString()
bool readCppStyleComment()
Location end_
Definition: json_reader.h:204
static constexpr unsigned nest_limit
Definition: json_reader.h:90
bool readToken(Token &token)
bool readValue(unsigned depth)
Value & currentValue()
Value * lastValue_
Definition: json_reader.h:207
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:73
bool decodeUnicodeCodePoint(Token &token, Location &current, Location end, unsigned int &unicode)
@ tokenMemberSeparator
Definition: json_reader.h:106
bool decodeString(Token &token)
bool readArray(Token &token, unsigned depth)
Char getNextChar()
const Char * Location
Definition: json_reader.h:40
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:202
Represents a JSON value.
Definition: json_value.h:147
JSON (JavaScript Object Notation).
Definition: json_errors.h:25
std::istream & operator>>(std::istream &, Value &)
Read from 'sin' into 'root'.
T reserve(T... args)