20 #include <ripple/basics/contract.h>
21 #include <ripple/json/json_reader.h>
44 result[0] =
static_cast<char> (cp);
49 result[1] =
static_cast<char> (0x80 | (0x3f & cp));
50 result[0] =
static_cast<char> (0xC0 | (0x1f & (cp >> 6)));
52 else if (cp <= 0xFFFF)
55 result[2] =
static_cast<char> (0x80 | (0x3f & cp));
56 result[1] = 0x80 |
static_cast<char> ((0x3f & (cp >> 6)));
57 result[0] = 0xE0 |
static_cast<char> ((0xf & (cp >> 12)));
59 else if (cp <= 0x10FFFF)
62 result[3] =
static_cast<char> (0x80 | (0x3f & cp));
63 result[2] =
static_cast<char> (0x80 | (0x3f & (cp >> 6)));
64 result[1] =
static_cast<char> (0x80 | (0x3f & (cp >> 12)));
65 result[0] =
static_cast<char> (0xF0 | (0x7 & (cp >> 18)));
82 return parse ( begin, end, root );
99 return parse ( doc, root );
127 addError (
"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;
144 switch ( token.
type_ )
179 return addError (
"Syntax error: value, object or array expected.", token );
202 if ( token.
type_ != type )
261 ok =
match (
"rue", 3 );
266 ok =
match (
"alse", 4 );
271 ok =
match (
"ull", 3 );
306 if ( c ==
' ' || c ==
'\t' || c ==
'\r' || c ==
'\n' )
321 int index = patternLength;
324 if (
current_[index] != pattern[index] )
368 if ( c ==
'\r' || c ==
'\n' )
378 static char const extended_tokens[] = {
'.',
'e',
'E',
'+',
'-' };
389 if (!std::isdigit (
static_cast<unsigned char>(*
current_)))
394 if (ret ==
std::end (extended_tokens))
435 bool initialTokenOk =
true;
438 initialTokenOk =
readToken ( tokenName );
440 if ( !initialTokenOk )
465 return addError (
"Key '" + name +
"' appears twice.", tokenName );
487 bool finalizeTokenOk =
true;
540 if ( !ok || badTokenType )
558 bool isNegative = *current ==
'-';
563 if (current == token.
end_)
566 "' is not a valid number.", token );
574 "The JSON integer overflow logic will need to be reworked.");
580 if ( c < '0' || c >
'9' )
583 "' is not a number.", token );
586 value = (value * 10) + (c -
'0');
590 if (current != token.
end_)
593 "' exceeds the allowable range.", token );
603 "' exceeds the allowable range.", token );
613 "' exceeds the allowable range.", token );
630 const int bufferSize = 32;
635 return addError(
"Unable to parse token length", token );
642 char format[] =
"%lf";
643 if ( length <= bufferSize )
645 Char buffer[bufferSize+1];
646 memcpy( buffer, token.
start_, length );
648 count = sscanf( buffer, format, &value );
653 count = sscanf( buffer.
c_str(), format, &value );
683 while ( current != end )
689 else if ( c ==
'\\' )
691 if ( current == end )
692 return addError (
"Empty escape sequence in string", token, current );
694 Char escape = *current++;
732 unsigned int unicode;
742 return addError (
"Bad escape sequence in string", token, current );
758 unsigned int& unicode )
764 if (unicode >= 0xD800 && unicode <= 0xDBFF)
767 if (end - current < 6)
768 return addError (
"additional six characters expected to parse unicode surrogate pair.", token, current );
770 unsigned int surrogatePair;
772 if (* (current++) ==
'\\' && * (current++) ==
'u')
776 unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF);
782 return addError (
"expecting another \\u token to begin the second half of a unicode surrogate pair", token, current );
792 unsigned int& unicode )
794 if ( end - current < 4 )
795 return addError (
"Bad unicode escape sequence in string: four digits expected.", token, current );
799 for (
int index = 0; index < 4; ++index )
804 if ( c >=
'0' && c <=
'9' )
806 else if ( c >=
'a' && c <=
'f' )
807 unicode += c -
'a' + 10;
808 else if ( c >=
'A' && c <=
'F' )
809 unicode += c -
'A' + 10;
811 return addError (
"Bad unicode escape sequence in string: hexadecimal digit expected.", token, current );
888 while ( current < location && current !=
end_ )
894 if ( *current ==
'\n' )
897 lastLineStart = current;
900 else if ( c ==
'\n' )
902 lastLineStart = current;
908 column = int (location - lastLineStart) + 1;
918 char buffer[18 + 16 + 16 + 1];
919 sprintf ( buffer,
"Line %d, Column %d", line, column );
929 for ( Errors::const_iterator itError =
errors_.
begin ();
935 formattedMessage +=
" " +
error.message_ +
"\n";
941 return formattedMessage;
948 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)