xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
json
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
13
namespace
json
{
14
19
class
Reader
20
{
21
public
:
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
78
getFormattedErrorMessages
()
const
;
79
80
static
constexpr
unsigned
kNestLimit
{25};
81
82
private
:
83
enum class
TokenType
{
84
EndOfStream
= 0,
85
ObjectBegin
,
86
ObjectEnd
,
87
ArrayBegin
,
88
ArrayEnd
,
89
String
,
90
Integer
,
91
Double
,
92
True
,
93
False
,
94
Null
,
95
ArraySeparator
,
96
MemberSeparator
,
97
Comment
,
98
Error
99
};
100
101
class
Token
102
{
103
public
:
104
explicit
Token
() =
default
;
105
106
TokenType
type
;
107
Location
start
;
108
Location
end
;
109
};
110
111
class
ErrorInfo
112
{
113
public
:
114
explicit
ErrorInfo
() =
default
;
115
116
Token
token
{};
117
std::string
message
;
118
Location
extra
{};
119
};
120
121
using
Errors
=
std::deque<ErrorInfo>
;
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
134
readCStyleComment
();
135
bool
136
readCppStyleComment
();
137
bool
138
readString
();
139
Reader::TokenType
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
158
decodeUnicodeEscapeSequence
(
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
170
skipUntilSpace
();
171
Value
&
172
currentValue
();
173
Char
174
getNextChar
();
175
void
176
getLocationLineAndColumn
(
Location
location,
int
& line,
int
& column)
const
;
177
std::string
178
getLocationLineAndColumn
(
Location
location)
const
;
179
void
180
skipCommentTokens
(
Token
& token);
181
182
using
Nodes
=
std::stack<Value*>
;
183
Nodes
nodes_
;
184
Errors
errors_
;
185
std::string
document_
;
186
Location
begin_
{};
187
Location
end_
{};
188
Location
current_
{};
189
Location
lastValueEnd_
{};
190
Value
*
lastValue_
{};
191
};
192
193
template
<
class
BufferSequence>
194
bool
195
Reader::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
230
std::istream
&
231
operator>>
(
std::istream
&,
Value
&);
232
233
}
// namespace json
std::string::append
T append(T... args)
std::istream
std::string
json::Reader::ErrorInfo::extra
Location extra
Definition
json_reader.h:118
json::Reader::ErrorInfo::message
std::string message
Definition
json_reader.h:117
json::Reader::ErrorInfo::token
Token token
Definition
json_reader.h:116
json::Reader::ErrorInfo::ErrorInfo
ErrorInfo()=default
json::Reader::Token
Definition
json_reader.h:102
json::Reader::Token::end
Location end
Definition
json_reader.h:108
json::Reader::Token::Token
Token()=default
json::Reader::Token::start
Location start
Definition
json_reader.h:107
json::Reader::Token::type
TokenType type
Definition
json_reader.h:106
json::Reader::current_
Location current_
Definition
json_reader.h:188
json::Reader::parse
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
Definition
json_reader.cpp:63
json::Reader::recoverFromError
bool recoverFromError(TokenType skipUntilToken)
Definition
json_reader.cpp:819
json::Reader::Reader
Reader()=default
Constructs a Reader allowing all features for parsing.
json::Reader::decodeUnicodeCodePoint
bool decodeUnicodeCodePoint(Token &token, Location ¤t, Location end, unsigned int &unicode)
Definition
json_reader.cpp:724
json::Reader::readObject
bool readObject(Token &token, unsigned depth)
Definition
json_reader.cpp:409
json::Reader::getFormattedErrorMessages
std::string getFormattedErrorMessages() const
Returns a user friendly string that list errors in the parsed document.
Definition
json_reader.cpp:899
json::Reader::skipCommentTokens
void skipCommentTokens(Token &token)
Definition
json_reader.cpp:169
json::Reader::addError
bool addError(std::string const &message, Token &token, Location extra=nullptr)
Definition
json_reader.cpp:808
json::Reader::decodeDouble
bool decodeDouble(Token &token)
Definition
json_reader.cpp:608
json::Reader::end_
Location end_
Definition
json_reader.h:187
json::Reader::readComment
bool readComment()
Definition
json_reader.cpp:314
json::Reader::skipSpaces
void skipSpaces()
Definition
json_reader.cpp:278
json::Reader::kNestLimit
static constexpr unsigned kNestLimit
Definition
json_reader.h:80
json::Reader::readArray
bool readArray(Token &token, unsigned depth)
Definition
json_reader.cpp:479
json::Reader::lastValue_
Value * lastValue_
Definition
json_reader.h:190
json::Reader::readNumber
Reader::TokenType readNumber()
Definition
json_reader.cpp:356
json::Reader::getLocationLineAndColumn
void getLocationLineAndColumn(Location location, int &line, int &column) const
Definition
json_reader.cpp:860
json::Reader::Nodes
std::stack< Value * > Nodes
Definition
json_reader.h:182
json::Reader::Location
Char const * Location
Definition
json_reader.h:23
json::Reader::decodeNumber
bool decodeNumber(Token &token)
Definition
json_reader.cpp:529
json::Reader::currentValue
Value & currentValue()
Definition
json_reader.cpp:845
json::Reader::begin_
Location begin_
Definition
json_reader.h:186
json::Reader::document_
std::string document_
Definition
json_reader.h:185
json::Reader::getNextChar
Char getNextChar()
Definition
json_reader.cpp:851
json::Reader::readString
bool readString()
Definition
json_reader.cpp:387
json::Reader::TokenType
TokenType
Definition
json_reader.h:83
json::Reader::TokenType::ArrayEnd
@ ArrayEnd
Definition
json_reader.h:88
json::Reader::TokenType::Comment
@ Comment
Definition
json_reader.h:97
json::Reader::TokenType::String
@ String
Definition
json_reader.h:89
json::Reader::TokenType::ArrayBegin
@ ArrayBegin
Definition
json_reader.h:87
json::Reader::TokenType::MemberSeparator
@ MemberSeparator
Definition
json_reader.h:96
json::Reader::TokenType::ObjectEnd
@ ObjectEnd
Definition
json_reader.h:86
json::Reader::TokenType::Integer
@ Integer
Definition
json_reader.h:90
json::Reader::TokenType::Null
@ Null
Definition
json_reader.h:94
json::Reader::TokenType::EndOfStream
@ EndOfStream
Definition
json_reader.h:84
json::Reader::TokenType::Double
@ Double
Definition
json_reader.h:91
json::Reader::TokenType::ObjectBegin
@ ObjectBegin
Definition
json_reader.h:85
json::Reader::TokenType::ArraySeparator
@ ArraySeparator
Definition
json_reader.h:95
json::Reader::TokenType::True
@ True
Definition
json_reader.h:92
json::Reader::TokenType::False
@ False
Definition
json_reader.h:93
json::Reader::skipUntilSpace
void skipUntilSpace()
json::Reader::readValue
bool readValue(unsigned depth)
Definition
json_reader.cpp:119
json::Reader::readCStyleComment
bool readCStyleComment()
Definition
json_reader.cpp:328
json::Reader::readCppStyleComment
bool readCppStyleComment()
Definition
json_reader.cpp:342
json::Reader::expectToken
bool expectToken(TokenType type, Token &token, char const *message)
Definition
json_reader.cpp:178
json::Reader::errors_
Errors errors_
Definition
json_reader.h:184
json::Reader::Errors
std::deque< ErrorInfo > Errors
Definition
json_reader.h:121
json::Reader::lastValueEnd_
Location lastValueEnd_
Definition
json_reader.h:189
json::Reader::decodeUnicodeEscapeSequence
bool decodeUnicodeEscapeSequence(Token &token, Location ¤t, Location end, unsigned int &unicode)
Definition
json_reader.cpp:763
json::Reader::addErrorAndRecover
bool addErrorAndRecover(std::string const &message, Token &token, TokenType skipUntilToken)
Definition
json_reader.cpp:838
json::Reader::match
bool match(Location pattern, int patternLength)
Definition
json_reader.cpp:296
json::Reader::readToken
bool readToken(Token &token)
Definition
json_reader.cpp:189
json::Reader::nodes_
Nodes nodes_
Definition
json_reader.h:183
json::Reader::decodeString
bool decodeString(Token &token)
Definition
json_reader.cpp:633
json::Reader::Char
char Char
Definition
json_reader.h:22
json::Value
Represents a JSON value.
Definition
json_value.h:117
deque
istream
boost::asio
Definition
Port.h:17
json
JSON (JavaScript Object Notation).
Definition
json_errors.h:5
json::operator>>
std::istream & operator>>(std::istream &, Value &)
Read from 'sin' into 'root'.
Definition
json_reader.cpp:916
std::string::reserve
T reserve(T... args)
stack
string
json::Error
Definition
json_errors.h:8
Generated by
1.17.0