20#include <xrpl/basics/contract.h>
21#include <xrpl/json/json_reader.h>
22#include <xrpl/json/json_value.h>
47 result[0] =
static_cast<char>(cp);
52 result[1] =
static_cast<char>(0x80 | (0x3f & cp));
53 result[0] =
static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
55 else if (cp <= 0xFFFF)
58 result[2] =
static_cast<char>(0x80 | (0x3f & cp));
59 result[1] = 0x80 |
static_cast<char>((0x3f & (cp >> 6)));
60 result[0] = 0xE0 |
static_cast<char>((0xf & (cp >> 12)));
62 else if (cp <= 0x10FFFF)
65 result[3] =
static_cast<char>(0x80 | (0x3f & cp));
66 result[2] =
static_cast<char>(0x80 | (0x3f & (cp >> 6)));
67 result[1] =
static_cast<char>(0x80 | (0x3f & (cp >> 12)));
68 result[0] =
static_cast<char>(0xF0 | (0x7 & (cp >> 18)));
83 return parse(begin, end, root);
98 return parse(doc, root);
119 if (!root.isNull() && !root.isArray() && !root.isObject())
127 "A valid JSON document must be either an array or an object value.",
141 return addError(
"Syntax error: maximum nesting depth exceeded", token);
142 bool successful =
true;
180 "Syntax error: value, object or array expected.", token);
200 if (token.
type_ != type)
258 ok =
match(
"rue", 3);
263 ok =
match(
"alse", 4);
268 ok =
match(
"ull", 3);
302 if (c ==
' ' || c ==
'\t' || c ==
'\r' || c ==
'\n')
315 int index = patternLength;
318 if (
current_[index] != pattern[index])
360 if (c ==
'\r' || c ==
'\n')
370 static char const extended_tokens[] = {
'.',
'e',
'E',
'+',
'-'};
381 if (!std::isdigit(
static_cast<unsigned char>(*
current_)))
388 if (ret ==
std::end(extended_tokens))
428 bool initialTokenOk =
true;
457 return addError(
"Key '" + name +
"' appears twice.", tokenName);
474 "Missing ',' or '}' in object declaration",
479 bool finalizeTokenOk =
true;
530 if (!ok || badTokenType)
533 "Missing ',' or ']' in array declaration",
549 bool isNegative = *current ==
'-';
554 if (current == token.
end_)
558 "' is not a valid number.",
568 "The JSON integer overflow logic will need to be reworked.");
574 if (c < '0' || c >
'9')
578 "' is not a number.",
582 value = (value * 10) + (c -
'0');
586 if (current != token.
end_)
590 "' exceeds the allowable range.",
602 "' exceeds the allowable range.",
614 "' exceeds the allowable range.",
632 const int bufferSize = 32;
638 return addError(
"Unable to parse token length", token);
645 char format[] =
"%lf";
646 if (length <= bufferSize)
648 Char buffer[bufferSize + 1];
649 memcpy(buffer, token.
start_, length);
651 count = sscanf(buffer, format, &value);
656 count = sscanf(buffer.
c_str(), format, &value);
685 while (current != end)
695 "Empty escape sequence in string", token, current);
697 Char escape = *current++;
734 unsigned int unicode;
745 "Bad escape sequence in string", token, current);
762 unsigned int& unicode)
767 if (unicode >= 0xD800 && unicode <= 0xDBFF)
770 if (end - current < 6)
772 "additional six characters expected to parse unicode surrogate "
777 unsigned int surrogatePair;
779 if (*(current++) ==
'\\' && *(current++) ==
'u')
783 unicode = 0x10000 + ((unicode & 0x3FF) << 10) +
784 (surrogatePair & 0x3FF);
791 "expecting another \\u token to begin the second half of a "
792 "unicode surrogate pair",
805 unsigned int& unicode)
807 if (end - current < 4)
809 "Bad unicode escape sequence in string: four digits expected.",
815 for (
int index = 0; index < 4; ++index)
820 if (c >=
'0' && c <=
'9')
822 else if (c >=
'a' && c <=
'f')
823 unicode += c -
'a' + 10;
824 else if (c >=
'A' && c <=
'F')
825 unicode += c -
'A' + 10;
828 "Bad unicode escape sequence in string: hexadecimal digit "
900 while (current < location && current !=
end_)
906 if (*current ==
'\n')
909 lastLineStart = current;
914 lastLineStart = current;
920 column = int(location - lastLineStart) + 1;
945 formattedMessage +=
" " +
error.message_ +
"\n";
948 formattedMessage +=
"See " +
952 return formattedMessage;
959 bool ok = reader.
parse(sin, root);
Unserialize a JSON document into a Value.
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.
bool match(Location pattern, int patternLength)
bool decodeNumber(Token &token)
bool expectToken(TokenType type, Token &token, const char *message)
bool decodeUnicodeEscapeSequence(Token &token, Location ¤t, Location end, unsigned int &unicode)
bool readCppStyleComment()
static constexpr unsigned nest_limit
bool readToken(Token &token)
bool readValue(unsigned depth)
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.
bool decodeUnicodeCodePoint(Token &token, Location ¤t, Location end, unsigned int &unicode)
bool decodeString(Token &token)
bool readArray(Token &token, unsigned depth)
bool addError(std::string const &message, Token &token, Location extra=0)
Reader::TokenType readNumber()
bool readObject(Token &token, unsigned depth)
static const UInt maxUInt
JSON (JavaScript Object Notation).
std::istream & operator>>(std::istream &, Value &)
Read from 'sin' into 'root'.
@ arrayValue
array value (ordered list)
@ objectValue
object value (collection of name/value pairs).
static std::string codePointToUTF8(unsigned int cp)