1#include <xrpl/json/json_reader.h>
3#include <xrpl/basics/contract.h>
4#include <xrpl/json/json_value.h>
6#include <fast_float/fast_float.h>
7#include <fast_float/parse_number.h>
32 result[0] =
static_cast<char>(cp);
37 result[1] =
static_cast<char>(0x80 | (0x3f & cp));
38 result[0] =
static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
40 else if (cp <= 0xFFFF)
43 result[2] =
static_cast<char>(0x80 | (0x3f & cp));
44 result[1] = 0x80 |
static_cast<char>((0x3f & (cp >> 6)));
45 result[0] = 0xE0 |
static_cast<char>((0xf & (cp >> 12)));
47 else if (cp <= 0x10FFFF)
50 result[3] =
static_cast<char>(0x80 | (0x3f & cp));
51 result[2] =
static_cast<char>(0x80 | (0x3f & (cp >> 6)));
52 result[1] =
static_cast<char>(0x80 | (0x3f & (cp >> 12)));
53 result[0] =
static_cast<char>(0xF0 | (0x7 & (cp >> 18)));
67 char const* end = begin +
document_.length();
68 return parse(begin, end, root);
83 return parse(doc, root);
104 if (!root.isNull() && !root.isArray() && !root.isObject())
109 token.
start = beginDoc;
111 addError(
"A valid JSON document must be either an array or an object value.", token);
124 return addError(
"Syntax error: maximum nesting depth exceeded", token);
125 bool successful =
true;
162 return addError(
"Syntax error: value, object or array expected.", token);
182 if (token.
type != type)
240 ok =
match(
"rue", 3);
245 ok =
match(
"alse", 4);
250 ok =
match(
"ull", 3);
284 if (c ==
' ' || c ==
'\t' || c ==
'\r' || c ==
'\n')
301 int index = patternLength;
303 while ((index--) != 0)
305 if (
current_[index] != pattern[index])
348 if (c ==
'\r' || c ==
'\n')
358 static char const kExtendedTokens[] = {
'.',
'e',
'E',
'+',
'-'};
369 if (std::isdigit(
static_cast<unsigned char>(*
current_)) == 0)
373 if (ret ==
std::end(kExtendedTokens))
417 bool initialTokenOk =
true;
446 return addError(
"Key '" + name +
"' appears twice.", tokenName);
466 bool finalizeTokenOk =
true;
512 bool const badTokenType =
515 if (!ok || badTokenType)
532 bool const isNegative = *current ==
'-';
537 if (current == token.
end)
549 "The JSON integer overflow logic will need to be reworked.");
553 Char const c = *current++;
555 if (c <
'0' || c >
'9')
561 value = (value * 10) + (c -
'0');
565 if (current != token.
end)
613 return addError(
"Unable to parse token length", token);
617 auto const [ptr, ec] = fast_float::from_chars(token.
start, token.
end, value);
651 while (current != end)
653 Char const c = *current++;
662 return addError(
"Empty escape sequence in string", token, current);
664 Char const escape = *current++;
701 unsigned int unicode = 0;
711 return addError(
"Bad escape sequence in string", token, current);
729 if (unicode >= 0xD800 && unicode <= 0xDBFF)
732 if (end - current < 6)
735 "additional six characters expected to parse unicode surrogate "
741 unsigned int surrogatePair = 0;
743 if (*current !=
'\\' || *(current + 1) !=
'u')
746 "expecting another \\u token to begin the second half of a unicode surrogate pair",
756 unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF);
767 unsigned int& unicode)
769 if (end - current < 4)
772 "Bad unicode escape sequence in string: four digits expected.", token, current);
777 for (
int index = 0; index < 4; ++index)
779 Char const c = *current++;
782 if (c >=
'0' && c <=
'9')
786 else if (c >=
'a' && c <=
'f')
788 unicode += c -
'a' + 10;
790 else if (c >=
'A' && c <=
'F')
792 unicode += c -
'A' + 10;
797 "Bad unicode escape sequence in string: hexadecimal digit "
821 int const errorCount = int(
errors_.size());
866 while (current < location && current !=
end_)
868 Char const c = *current++;
872 if (*current ==
'\n')
875 lastLineStart = current;
880 lastLineStart = current;
886 column = int(location - lastLineStart) + 1;
893 int line = 0, column = 0;
903 for (
auto const& error :
errors_)
906 formattedMessage +=
" " + error.message +
"\n";
908 if (error.extra !=
nullptr)
912 return formattedMessage;
919 bool const ok = reader.
parse(sin, root);
Unserialize a JSON document into a Value.
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
bool recoverFromError(TokenType skipUntilToken)
bool decodeUnicodeCodePoint(Token &token, Location ¤t, 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)
static constexpr unsigned kNestLimit
bool readArray(Token &token, unsigned depth)
Reader::TokenType readNumber()
void getLocationLineAndColumn(Location location, int &line, int &column) const
bool decodeNumber(Token &token)
bool readValue(unsigned depth)
bool readCppStyleComment()
bool expectToken(TokenType type, Token &token, char const *message)
bool decodeUnicodeEscapeSequence(Token &token, Location ¤t, 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)
static constexpr Int kMaxInt
static constexpr UInt kMaxUInt
JSON (JavaScript Object Notation).
std::istream & operator>>(std::istream &, Value &)
Read from 'sin' into 'root'.
static std::string codePointToUTF8(unsigned int cp)
@ Array
array value (ordered list)
@ Object
object value (collection of name/value pairs).
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)