1#include <xrpl/basics/contract.h>
2#include <xrpl/json/json_reader.h>
3#include <xrpl/json/json_value.h>
28 result[0] =
static_cast<char>(cp);
33 result[1] =
static_cast<char>(0x80 | (0x3f & cp));
34 result[0] =
static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
36 else if (cp <= 0xFFFF)
39 result[2] =
static_cast<char>(0x80 | (0x3f & cp));
40 result[1] = 0x80 |
static_cast<char>((0x3f & (cp >> 6)));
41 result[0] = 0xE0 |
static_cast<char>((0xf & (cp >> 12)));
43 else if (cp <= 0x10FFFF)
46 result[3] =
static_cast<char>(0x80 | (0x3f & cp));
47 result[2] =
static_cast<char>(0x80 | (0x3f & (cp >> 6)));
48 result[1] =
static_cast<char>(0x80 | (0x3f & (cp >> 12)));
49 result[0] =
static_cast<char>(0xF0 | (0x7 & (cp >> 18)));
64 return parse(begin, end, root);
79 return parse(doc, root);
100 if (!root.isNull() && !root.isArray() && !root.isObject())
108 "A valid JSON document must be either an array or an object value.",
122 return addError(
"Syntax error: maximum nesting depth exceeded", token);
123 bool successful =
true;
161 "Syntax error: value, object or array expected.", token);
181 if (token.
type_ != type)
239 ok =
match(
"rue", 3);
244 ok =
match(
"alse", 4);
249 ok =
match(
"ull", 3);
283 if (c ==
' ' || c ==
'\t' || c ==
'\r' || c ==
'\n')
296 int index = patternLength;
299 if (
current_[index] != pattern[index])
341 if (c ==
'\r' || c ==
'\n')
351 static char const extended_tokens[] = {
'.',
'e',
'E',
'+',
'-'};
369 if (ret ==
std::end(extended_tokens))
409 bool initialTokenOk =
true;
438 return addError(
"Key '" + name +
"' appears twice.", tokenName);
455 "Missing ',' or '}' in object declaration",
460 bool finalizeTokenOk =
true;
511 if (!ok || badTokenType)
514 "Missing ',' or ']' in array declaration",
530 bool isNegative = *current ==
'-';
535 if (current == token.
end_)
539 "' is not a valid number.",
549 "The JSON integer overflow logic will need to be reworked.");
555 if (c <
'0' || c >
'9')
559 "' is not a number.",
563 value = (value * 10) + (c -
'0');
567 if (current != token.
end_)
571 "' exceeds the allowable range.",
583 "' exceeds the allowable range.",
595 "' exceeds the allowable range.",
613 int const bufferSize = 32;
619 return addError(
"Unable to parse token length", token);
626 char format[] =
"%lf";
627 if (length <= bufferSize)
629 Char buffer[bufferSize + 1];
630 memcpy(buffer, token.
start_, length);
632 count = sscanf(buffer, format, &value);
637 count = sscanf(buffer.
c_str(), format, &value);
666 while (current != end)
676 "Empty escape sequence in string", token, current);
678 Char escape = *current++;
715 unsigned int unicode;
726 "Bad escape sequence in string", token, current);
743 unsigned int& unicode)
748 if (unicode >= 0xD800 && unicode <= 0xDBFF)
751 if (end - current < 6)
753 "additional six characters expected to parse unicode surrogate "
758 unsigned int surrogatePair;
760 if (*(current++) ==
'\\' && *(current++) ==
'u')
764 unicode = 0x10000 + ((unicode & 0x3FF) << 10) +
765 (surrogatePair & 0x3FF);
772 "expecting another \\u token to begin the second half of a "
773 "unicode surrogate pair",
786 unsigned int& unicode)
788 if (end - current < 4)
790 "Bad unicode escape sequence in string: four digits expected.",
796 for (
int index = 0; index < 4; ++index)
801 if (c >=
'0' && c <=
'9')
803 else if (c >=
'a' && c <=
'f')
804 unicode += c -
'a' + 10;
805 else if (c >=
'A' && c <=
'F')
806 unicode += c -
'A' + 10;
809 "Bad unicode escape sequence in string: hexadecimal digit "
881 while (current < location && current !=
end_)
887 if (*current ==
'\n')
890 lastLineStart = current;
895 lastLineStart = current;
901 column = int(location - lastLineStart) + 1;
926 formattedMessage +=
" " +
error.message_ +
"\n";
929 formattedMessage +=
"See " +
933 return formattedMessage;
940 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 expectToken(TokenType type, Token &token, char const *message)
bool decodeNumber(Token &token)
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 UInt const 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)