20 #include <ripple/basics/contract.h>
21 #include <ripple/json/json_reader.h>
45 result[0] =
static_cast<char>(cp);
50 result[1] =
static_cast<char>(0x80 | (0x3f & cp));
51 result[0] =
static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
53 else if (cp <= 0xFFFF)
56 result[2] =
static_cast<char>(0x80 | (0x3f & cp));
57 result[1] = 0x80 |
static_cast<char>((0x3f & (cp >> 6)));
58 result[0] = 0xE0 |
static_cast<char>((0xf & (cp >> 12)));
60 else if (cp <= 0x10FFFF)
63 result[3] =
static_cast<char>(0x80 | (0x3f & cp));
64 result[2] =
static_cast<char>(0x80 | (0x3f & (cp >> 6)));
65 result[1] =
static_cast<char>(0x80 | (0x3f & (cp >> 12)));
66 result[0] =
static_cast<char>(0xF0 | (0x7 & (cp >> 18)));
81 return parse(begin, end, root);
96 return parse(doc, root);
117 if (!root.isNull() && !root.isArray() && !root.isObject())
125 "A valid JSON document must be either an array or an object value.",
139 return addError(
"Syntax error: maximum nesting depth exceeded", token);
140 bool successful =
true;
178 "Syntax error: value, object or array expected.", token);
198 if (token.
type_ != type)
256 ok =
match(
"rue", 3);
261 ok =
match(
"alse", 4);
266 ok =
match(
"ull", 3);
300 if (c ==
' ' || c ==
'\t' || c ==
'\r' || c ==
'\n')
313 int index = patternLength;
316 if (
current_[index] != pattern[index])
358 if (c ==
'\r' || c ==
'\n')
368 static char const extended_tokens[] = {
'.',
'e',
'E',
'+',
'-'};
379 if (!std::isdigit(
static_cast<unsigned char>(*
current_)))
386 if (ret ==
std::end(extended_tokens))
426 bool initialTokenOk =
true;
455 return addError(
"Key '" + name +
"' appears twice.", tokenName);
472 "Missing ',' or '}' in object declaration",
477 bool finalizeTokenOk =
true;
528 if (!ok || badTokenType)
531 "Missing ',' or ']' in array declaration",
547 bool isNegative = *current ==
'-';
552 if (current == token.
end_)
556 "' is not a valid number.",
566 "The JSON integer overflow logic will need to be reworked.");
572 if (c < '0' || c >
'9')
576 "' is not a number.",
580 value = (value * 10) + (c -
'0');
584 if (current != token.
end_)
588 "' exceeds the allowable range.",
600 "' exceeds the allowable range.",
612 "' exceeds the allowable range.",
630 const int bufferSize = 32;
636 return addError(
"Unable to parse token length", token);
643 char format[] =
"%lf";
644 if (length <= bufferSize)
646 Char buffer[bufferSize + 1];
647 memcpy(buffer, token.
start_, length);
649 count = sscanf(buffer, format, &value);
654 count = sscanf(buffer.
c_str(), format, &value);
683 while (current != end)
693 "Empty escape sequence in string", token, current);
695 Char escape = *current++;
732 unsigned int unicode;
743 "Bad escape sequence in string", token, current);
760 unsigned int& unicode)
765 if (unicode >= 0xD800 && unicode <= 0xDBFF)
768 if (end - current < 6)
770 "additional six characters expected to parse unicode surrogate "
775 unsigned int surrogatePair;
777 if (*(current++) ==
'\\' && *(current++) ==
'u')
781 unicode = 0x10000 + ((unicode & 0x3FF) << 10) +
782 (surrogatePair & 0x3FF);
789 "expecting another \\u token to begin the second half of a "
790 "unicode surrogate pair",
803 unsigned int& unicode)
805 if (end - current < 4)
807 "Bad unicode escape sequence in string: four digits expected.",
813 for (
int index = 0; index < 4; ++index)
818 if (c >=
'0' && c <=
'9')
820 else if (c >=
'a' && c <=
'f')
821 unicode += c -
'a' + 10;
822 else if (c >=
'A' && c <=
'F')
823 unicode += c -
'A' + 10;
826 "Bad unicode escape sequence in string: hexadecimal digit "
898 while (current < location && current !=
end_)
904 if (*current ==
'\n')
907 lastLineStart = current;
912 lastLineStart = current;
918 column = int(location - lastLineStart) + 1;
927 char buffer[18 + 16 + 16 + 1];
928 sprintf(buffer,
"Line %d, Column %d", line, column);
944 formattedMessage +=
" " +
error.message_ +
"\n";
947 formattedMessage +=
"See " +
951 return formattedMessage;
958 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)
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)