rippled
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 <ripple/json/json_forwards.h>
26 #include <ripple/json/json_value.h>
27 #include <boost/asio/buffer.hpp>
28 #include <stack>
29 
30 namespace Json {
31 
36 class Reader
37 {
38 public:
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 
92 private:
93  enum TokenType {
109  };
110 
111  class Token
112  {
113  public:
114  explicit Token() = default;
115 
119  };
120 
121  class ErrorInfo
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
187  skipUntilSpace();
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 
210 template <class BufferSequence>
211 bool
212 Reader::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
Json::Reader::tokenFalse
@ tokenFalse
Definition: json_reader.h:103
Json::Reader::addError
bool addError(std::string const &message, Token &token, Location extra=0)
Definition: json_reader.cpp:834
std::string
STL class.
Json::Reader::Token::start_
Location start_
Definition: json_reader.h:117
Json::Reader::skipCommentTokens
void skipCommentTokens(Token &token)
Definition: json_reader.cpp:183
Json::Reader::Token
Definition: json_reader.h:111
Json::Reader::current_
Location current_
Definition: json_reader.h:205
Json::Reader::decodeDouble
bool decodeDouble(Token &token)
Definition: json_reader.cpp:625
std::string::reserve
T reserve(T... args)
Json::Reader::nodes_
Nodes nodes_
Definition: json_reader.h:200
Json::Reader::decodeString
bool decodeString(Token &token)
Definition: json_reader.cpp:663
Json::Reader::readCStyleComment
bool readCStyleComment()
Definition: json_reader.cpp:336
stack
Json::Reader::Token::Token
Token()=default
Json::Reader::tokenComment
@ tokenComment
Definition: json_reader.h:107
Json::Reader::readObject
bool readObject(Token &token, unsigned depth)
Definition: json_reader.cpp:416
Json::Reader::ErrorInfo
Definition: json_reader.h:121
Json::Reader::ErrorInfo::ErrorInfo
ErrorInfo()=default
Json::Reader
Unserialize a JSON document into a Value.
Definition: json_reader.h:36
Json::Reader::expectToken
bool expectToken(TokenType type, Token &token, const char *message)
Definition: json_reader.cpp:192
Json::Reader::ErrorInfo::extra_
Location extra_
Definition: json_reader.h:128
Json::Reader::tokenTrue
@ tokenTrue
Definition: json_reader.h:102
Json::Reader::readValue
bool readValue(unsigned depth)
Definition: json_reader.cpp:132
Json::Reader::tokenMemberSeparator
@ tokenMemberSeparator
Definition: json_reader.h:106
Json::Reader::lastValueEnd_
Location lastValueEnd_
Definition: json_reader.h:206
Json::Reader::Token::type_
TokenType type_
Definition: json_reader.h:116
Json
JSON (JavaScript Object Notation).
Definition: json_reader.cpp:27
Json::Reader::ErrorInfo::token_
Token token_
Definition: json_reader.h:126
Json::Reader::document_
std::string document_
Definition: json_reader.h:202
Json::Reader::decodeNumber
bool decodeNumber(Token &token)
Definition: json_reader.cpp:542
Json::Reader::end_
Location end_
Definition: json_reader.h:204
Json::Reader::match
bool match(Location pattern, int patternLength)
Definition: json_reader.cpp:306
Json::Reader::tokenObjectBegin
@ tokenObjectBegin
Definition: json_reader.h:95
Json::Reader::tokenEndOfStream
@ tokenEndOfStream
Definition: json_reader.h:94
Json::Reader::decodeUnicodeCodePoint
bool decodeUnicodeCodePoint(Token &token, Location &current, Location end, unsigned int &unicode)
Definition: json_reader.cpp:754
boost::asio
Definition: Overlay.h:41
Json::Reader::getLocationLineAndColumn
void getLocationLineAndColumn(Location location, int &line, int &column) const
Definition: json_reader.cpp:889
Json::Reader::decodeUnicodeEscapeSequence
bool decodeUnicodeEscapeSequence(Token &token, Location &current, Location end, unsigned int &unicode)
Definition: json_reader.cpp:797
std::deque< ErrorInfo >
Json::Reader::currentValue
Value & currentValue()
Definition: json_reader.cpp:874
Json::Reader::begin_
Location begin_
Definition: json_reader.h:203
Json::Reader::TokenType
TokenType
Definition: json_reader.h:93
Json::Reader::tokenInteger
@ tokenInteger
Definition: json_reader.h:100
std::string::append
T append(T... args)
Json::Reader::skipSpaces
void skipSpaces()
Definition: json_reader.cpp:292
Json::Reader::tokenArrayBegin
@ tokenArrayBegin
Definition: json_reader.h:97
Json::Reader::readComment
bool readComment()
Definition: json_reader.cpp:322
Json::Reader::Token::end_
Location end_
Definition: json_reader.h:118
Json::Reader::Char
char Char
Definition: json_reader.h:39
Json::Reader::tokenObjectEnd
@ tokenObjectEnd
Definition: json_reader.h:96
Json::Reader::Location
const Char * Location
Definition: json_reader.h:40
Json::Reader::tokenDouble
@ tokenDouble
Definition: json_reader.h:101
Json::Reader::errors_
Errors errors_
Definition: json_reader.h:201
Json::Reader::tokenArrayEnd
@ tokenArrayEnd
Definition: json_reader.h:98
Json::operator>>
std::istream & operator>>(std::istream &sin, Value &root)
Read from 'sin' into 'root'.
Definition: json_reader.cpp:953
Json::Reader::Reader
Reader()=default
Constructs a Reader allowing all features for parsing.
Json::Reader::nest_limit
static constexpr unsigned nest_limit
Definition: json_reader.h:90
Json::Reader::readToken
bool readToken(Token &token)
Definition: json_reader.cpp:203
Json::Reader::parse
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
Definition: json_reader.cpp:74
Json::Reader::getFormatedErrorMessages
std::string getFormatedErrorMessages() const
Returns a user friendly string that list errors in the parsed document.
Definition: json_reader.cpp:931
Json::Reader::lastValue_
Value * lastValue_
Definition: json_reader.h:207
Json::Reader::recoverFromError
bool recoverFromError(TokenType skipUntilToken)
Definition: json_reader.cpp:845
Json::Reader::readArray
bool readArray(Token &token, unsigned depth)
Definition: json_reader.cpp:489
Json::Reader::readNumber
Reader::TokenType readNumber()
Definition: json_reader.cpp:364
Json::Reader::ErrorInfo::message_
std::string message_
Definition: json_reader.h:127
Json::Reader::readCppStyleComment
bool readCppStyleComment()
Definition: json_reader.cpp:350
Json::Reader::skipUntilSpace
void skipUntilSpace()
std::istream
STL class.
Json::Reader::getNextChar
Char getNextChar()
Definition: json_reader.cpp:880
Json::Reader::tokenNull
@ tokenNull
Definition: json_reader.h:104
Json::Reader::tokenString
@ tokenString
Definition: json_reader.h:99
Json::Reader::addErrorAndRecover
bool addErrorAndRecover(std::string const &message, Token &token, TokenType skipUntilToken)
Definition: json_reader.cpp:864
Json::Reader::tokenError
@ tokenError
Definition: json_reader.h:108
Json::Value
Represents a JSON value.
Definition: json_value.h:145
Json::Reader::readString
bool readString()
Definition: json_reader.cpp:398
Json::Reader::tokenArraySeparator
@ tokenArraySeparator
Definition: json_reader.h:105