20 #include <ripple/basics/contract.h>
21 #include <ripple/json/json_reader.h>
42 result[0] =
static_cast<char>(cp);
47 result[1] =
static_cast<char>(0x80 | (0x3f & cp));
48 result[0] =
static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
50 else if (cp <= 0xFFFF)
53 result[2] =
static_cast<char>(0x80 | (0x3f & cp));
54 result[1] = 0x80 |
static_cast<char>((0x3f & (cp >> 6)));
55 result[0] = 0xE0 |
static_cast<char>((0xf & (cp >> 12)));
57 else if (cp <= 0x10FFFF)
60 result[3] =
static_cast<char>(0x80 | (0x3f & cp));
61 result[2] =
static_cast<char>(0x80 | (0x3f & (cp >> 6)));
62 result[1] =
static_cast<char>(0x80 | (0x3f & (cp >> 12)));
63 result[0] =
static_cast<char>(0xF0 | (0x7 & (cp >> 18)));
78 return parse(begin, end, root);
93 return parse(doc, root);
122 "A valid JSON document must be either an array or an object value.",
136 return addError(
"Syntax error: maximum nesting depth exceeded", token);
137 bool successful =
true;
175 "Syntax error: value, object or array expected.", token);
195 if (token.
type_ != type)
253 ok =
match(
"rue", 3);
258 ok =
match(
"alse", 4);
263 ok =
match(
"ull", 3);
297 if (c ==
' ' || c ==
'\t' || c ==
'\r' || c ==
'\n')
310 int index = patternLength;
313 if (
current_[index] != pattern[index])
355 if (c ==
'\r' || c ==
'\n')
365 static char const extended_tokens[] = {
'.',
'e',
'E',
'+',
'-'};
376 if (!std::isdigit(
static_cast<unsigned char>(*
current_)))
383 if (ret ==
std::end(extended_tokens))
423 bool initialTokenOk =
true;
452 return addError(
"Key '" + name +
"' appears twice.", tokenName);
469 "Missing ',' or '}' in object declaration",
474 bool finalizeTokenOk =
true;
525 if (!ok || badTokenType)
528 "Missing ',' or ']' in array declaration",
544 bool isNegative = *current ==
'-';
549 if (current == token.
end_)
553 "' is not a valid number.",
563 "The JSON integer overflow logic will need to be reworked.");
569 if (c < '0' || c >
'9')
573 "' is not a number.",
577 value = (value * 10) + (c -
'0');
581 if (current != token.
end_)
585 "' exceeds the allowable range.",
597 "' exceeds the allowable range.",
609 "' exceeds the allowable range.",
627 const int bufferSize = 32;
633 return addError(
"Unable to parse token length", token);
640 char format[] =
"%lf";
641 if (length <= bufferSize)
643 Char buffer[bufferSize + 1];
644 memcpy(buffer, token.
start_, length);
646 count = sscanf(buffer, format, &value);
651 count = sscanf(buffer.
c_str(), format, &value);
680 while (current != end)
690 "Empty escape sequence in string", token, current);
692 Char escape = *current++;
729 unsigned int unicode;
740 "Bad escape sequence in string", token, current);
757 unsigned int& unicode)
762 if (unicode >= 0xD800 && unicode <= 0xDBFF)
765 if (end - current < 6)
767 "additional six characters expected to parse unicode surrogate "
772 unsigned int surrogatePair;
774 if (*(current++) ==
'\\' && *(current++) ==
'u')
778 unicode = 0x10000 + ((unicode & 0x3FF) << 10) +
779 (surrogatePair & 0x3FF);
786 "expecting another \\u token to begin the second half of a "
787 "unicode surrogate pair",
800 unsigned int& unicode)
802 if (end - current < 4)
804 "Bad unicode escape sequence in string: four digits expected.",
810 for (
int index = 0; index < 4; ++index)
815 if (c >=
'0' && c <=
'9')
817 else if (c >=
'a' && c <=
'f')
818 unicode += c -
'a' + 10;
819 else if (c >=
'A' && c <=
'F')
820 unicode += c -
'A' + 10;
823 "Bad unicode escape sequence in string: hexadecimal digit "
895 while (current < location && current !=
end_)
901 if (*current ==
'\n')
904 lastLineStart = current;
909 lastLineStart = current;
915 column = int(location - lastLineStart) + 1;
924 char buffer[18 + 16 + 16 + 1];
925 sprintf(buffer,
"Line %d, Column %d", line, column);
941 formattedMessage +=
" " +
error.message_ +
"\n";
944 formattedMessage +=
"See " +
948 return formattedMessage;
955 bool ok = reader.
parse(sin, root);
bool addError(std::string const &message, Token &token, Location extra=0)
void skipCommentTokens(Token &token)
@ arrayValue
array value (ordered list)
bool decodeDouble(Token &token)
bool decodeString(Token &token)
bool readObject(Token &token, unsigned depth)
bool isNull() const
isNull() tests to see if this field is null.
Unserialize a JSON document into a Value.
bool expectToken(TokenType type, Token &token, const char *message)
bool readValue(unsigned depth)
JSON (JavaScript Object Notation).
bool decodeNumber(Token &token)
@ objectValue
object value (collection of name/value pairs).
bool match(Location pattern, int patternLength)
bool decodeUnicodeCodePoint(Token &token, Location ¤t, Location end, unsigned int &unicode)
void getLocationLineAndColumn(Location location, int &line, int &column) const
bool decodeUnicodeEscapeSequence(Token &token, Location ¤t, Location end, unsigned int &unicode)
static const UInt maxUInt
static std::string codePointToUTF8(unsigned int cp)
std::istream & operator>>(std::istream &sin, Value &root)
Read from 'sin' into 'root'.
static constexpr unsigned nest_limit
bool readToken(Token &token)
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
std::string getFormatedErrorMessages() const
Returns a user friendly string that list errors in the parsed document.
bool recoverFromError(TokenType skipUntilToken)
bool readArray(Token &token, unsigned depth)
Reader::TokenType readNumber()
bool readCppStyleComment()
bool addErrorAndRecover(std::string const &message, Token &token, TokenType skipUntilToken)