6 #if !defined(JSON_IS_AMALGAMATION) 11 #endif // if !defined(JSON_IS_AMALGAMATION) 21 #if defined(_MSC_VER) && _MSC_VER < 1500 // VC++ 8.0 and below 22 #define snprintf _snprintf 25 #if defined(_MSC_VER) && _MSC_VER >= 1400 // VC++ 8.0 27 #pragma warning(disable : 4996) 35 #if __cplusplus >= 201103L 45 : allowComments_(true), strictRoot_(false),
46 allowDroppedNullPlaceholders_(false), allowNumericKeys_(false) {}
63 for (; begin < end; ++begin)
64 if (*begin ==
'\n' || *begin ==
'\r')
73 : errors_(), document_(), begin_(), end_(), current_(), lastValueEnd_(),
74 lastValue_(), commentsBefore_(), features_(
Features::
all()),
78 : errors_(), document_(), begin_(), end_(), current_(), lastValueEnd_(),
79 lastValue_(), commentsBefore_(), features_(features), collectComments_() {
85 const char* begin = document_.c_str();
86 const char* end = begin + document_.length();
87 return parse(begin, end, root, collectComments);
99 std::getline(sin, doc, (
char)EOF);
100 return parse(doc, root, collectComments);
106 bool collectComments) {
108 collectComments =
false;
113 collectComments_ = collectComments;
117 commentsBefore_ =
"";
119 while (!nodes_.empty())
124 bool successful = readValue();
126 skipCommentTokens(token);
127 if (collectComments_ && !commentsBefore_.empty())
133 token.type_ = tokenError;
134 token.start_ = beginDoc;
137 "A valid JSON document must be either an array or an object value.",
145 bool Reader::readValue() {
154 skipCommentTokens(token);
155 bool successful =
true;
157 if (collectComments_ && !commentsBefore_.empty()) {
159 commentsBefore_ =
"";
162 switch (token.type_) {
163 case tokenObjectBegin:
164 successful = readObject(token);
167 case tokenArrayBegin:
168 successful = readArray(token);
172 successful = decodeNumber(token);
175 successful = decodeString(token);
201 case tokenArraySeparator:
217 return addError(
"Syntax error: value, object or array expected.", token);
220 if (collectComments_) {
221 lastValueEnd_ = current_;
222 lastValue_ = ¤tValue();
229 void Reader::skipCommentTokens(Token& token) {
233 }
while (token.type_ == tokenComment);
239 bool Reader::readToken(Token& token) {
241 token.start_ = current_;
242 Char c = getNextChar();
246 token.type_ = tokenObjectBegin;
249 token.type_ = tokenObjectEnd;
252 token.type_ = tokenArrayBegin;
255 token.type_ = tokenArrayEnd;
258 token.type_ = tokenString;
262 token.type_ = tokenComment;
276 token.type_ = tokenNumber;
280 token.type_ = tokenTrue;
281 ok = match(
"rue", 3);
284 token.type_ = tokenFalse;
285 ok = match(
"alse", 4);
288 token.type_ = tokenNull;
289 ok = match(
"ull", 3);
292 token.type_ = tokenArraySeparator;
295 token.type_ = tokenMemberSeparator;
298 token.type_ = tokenEndOfStream;
305 token.type_ = tokenError;
306 token.end_ = current_;
310 void Reader::skipSpaces() {
311 while (current_ != end_) {
313 if (c ==
' ' || c ==
'\t' || c ==
'\r' || c ==
'\n')
320 bool Reader::match(
Location pattern,
int patternLength) {
321 if (end_ - current_ < patternLength)
323 int index = patternLength;
325 if (current_[index] != pattern[index])
327 current_ += patternLength;
331 bool Reader::readComment() {
332 Location commentBegin = current_ - 1;
333 Char c = getNextChar();
334 bool successful =
false;
336 successful = readCStyleComment();
338 successful = readCppStyleComment();
342 if (collectComments_) {
349 addComment(commentBegin, current_, placement);
355 std::string normalized;
356 normalized.reserve(end - begin);
358 while (current != end) {
361 if (current != end && *current ==
'\n')
375 assert(collectComments_);
376 const std::string& normalized =
normalizeEOL(begin, end);
378 assert(lastValue_ != 0);
379 lastValue_->
setComment(normalized, placement);
381 commentsBefore_ += normalized;
385 bool Reader::readCStyleComment() {
386 while (current_ != end_) {
387 Char c = getNextChar();
388 if (c ==
'*' && *current_ ==
'/')
391 return getNextChar() ==
'/';
394 bool Reader::readCppStyleComment() {
395 while (current_ != end_) {
396 Char c = getNextChar();
401 if (current_ != end_ && *current_ ==
'\n')
410 void Reader::readNumber() {
411 const char *p = current_;
414 while (c >=
'0' && c <=
'9')
415 c = (current_ = p) < end_ ? *p++ : 0;
418 c = (current_ = p) < end_ ? *p++ : 0;
419 while (c >=
'0' && c <=
'9')
420 c = (current_ = p) < end_ ? *p++ : 0;
423 if (c ==
'e' || c ==
'E') {
424 c = (current_ = p) < end_ ? *p++ : 0;
425 if (c ==
'+' || c ==
'-')
426 c = (current_ = p) < end_ ? *p++ : 0;
427 while (c >=
'0' && c <=
'9')
428 c = (current_ = p) < end_ ? *p++ : 0;
432 bool Reader::readString() {
434 while (current_ != end_) {
444 bool Reader::readObject(Token& tokenStart) {
450 while (readToken(tokenName)) {
451 bool initialTokenOk =
true;
452 while (tokenName.type_ == tokenComment && initialTokenOk)
453 initialTokenOk = readToken(tokenName);
456 if (tokenName.type_ == tokenObjectEnd && name.empty())
459 if (tokenName.type_ == tokenString) {
460 if (!decodeString(tokenName, name))
461 return recoverFromError(tokenObjectEnd);
464 if (!decodeNumber(tokenName, numberName))
465 return recoverFromError(tokenObjectEnd);
472 if (!readToken(colon) || colon.type_ != tokenMemberSeparator) {
473 return addErrorAndRecover(
474 "Missing ':' after object member name", colon, tokenObjectEnd);
476 Value& value = currentValue()[name];
478 bool ok = readValue();
481 return recoverFromError(tokenObjectEnd);
484 if (!readToken(comma) ||
485 (comma.type_ != tokenObjectEnd && comma.type_ != tokenArraySeparator &&
486 comma.type_ != tokenComment)) {
487 return addErrorAndRecover(
488 "Missing ',' or '}' in object declaration", comma, tokenObjectEnd);
490 bool finalizeTokenOk =
true;
491 while (comma.type_ == tokenComment && finalizeTokenOk)
492 finalizeTokenOk = readToken(comma);
493 if (comma.type_ == tokenObjectEnd)
496 return addErrorAndRecover(
497 "Missing '}' or object member name", tokenName, tokenObjectEnd);
500 bool Reader::readArray(Token& tokenStart) {
505 if (*current_ ==
']')
513 Value& value = currentValue()[index++];
515 bool ok = readValue();
518 return recoverFromError(tokenArrayEnd);
522 ok = readToken(token);
523 while (token.type_ == tokenComment && ok) {
524 ok = readToken(token);
527 (token.type_ != tokenArraySeparator && token.type_ != tokenArrayEnd);
528 if (!ok || badTokenType) {
529 return addErrorAndRecover(
530 "Missing ',' or ']' in array declaration", token, tokenArrayEnd);
532 if (token.type_ == tokenArrayEnd)
538 bool Reader::decodeNumber(Token& token) {
540 if (!decodeNumber(token, decoded))
548 bool Reader::decodeNumber(Token& token,
Value& decoded) {
553 bool isNegative = *current ==
'-';
560 Value::LargestUInt threshold = maxIntegerValue / 10;
561 Value::LargestUInt value = 0;
562 while (current < token.end_) {
564 if (c < '0' || c >
'9')
565 return decodeDouble(token, decoded);
567 if (value >= threshold) {
572 if (value > threshold || current != token.end_ ||
573 digit > maxIntegerValue % 10) {
574 return decodeDouble(token, decoded);
577 value = value * 10 + digit;
588 bool Reader::decodeDouble(Token& token) {
590 if (!decodeDouble(token, decoded))
598 bool Reader::decodeDouble(Token& token,
Value& decoded) {
600 std::string buffer(token.start_, token.end_);
601 std::istringstream is(buffer);
603 return addError(
"'" + std::string(token.start_, token.end_) +
604 "' is not a number.",
610 bool Reader::decodeString(Token& token) {
611 std::string decoded_string;
612 if (!decodeString(token, decoded_string))
614 Value decoded(decoded_string);
621 bool Reader::decodeString(Token& token, std::string& decoded) {
622 decoded.reserve(token.end_ - token.start_ - 2);
623 Location current = token.start_ + 1;
625 while (current != end) {
629 else if (c ==
'\\') {
631 return addError(
"Empty escape sequence in string", token, current);
632 Char escape = *current++;
659 unsigned int unicode;
660 if (!decodeUnicodeCodePoint(token, current, end, unicode))
665 return addError(
"Bad escape sequence in string", token, current);
674 bool Reader::decodeUnicodeCodePoint(Token& token,
677 unsigned int& unicode) {
679 if (!decodeUnicodeEscapeSequence(token, current, end, unicode))
681 if (unicode >= 0xD800 && unicode <= 0xDBFF) {
683 if (end - current < 6)
685 "additional six characters expected to parse unicode surrogate pair.",
688 unsigned int surrogatePair;
689 if (*(current++) ==
'\\' && *(current++) ==
'u') {
690 if (decodeUnicodeEscapeSequence(token, current, end, surrogatePair)) {
691 unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF);
695 return addError(
"expecting another \\u token to begin the second half of " 696 "a unicode surrogate pair",
703 bool Reader::decodeUnicodeEscapeSequence(Token& token,
706 unsigned int& unicode) {
707 if (end - current < 4)
709 "Bad unicode escape sequence in string: four digits expected.",
713 for (
int index = 0; index < 4; ++index) {
716 if (c >=
'0' && c <=
'9')
718 else if (c >=
'a' && c <=
'f')
719 unicode += c -
'a' + 10;
720 else if (c >=
'A' && c <=
'F')
721 unicode += c -
'A' + 10;
724 "Bad unicode escape sequence in string: hexadecimal digit expected.",
732 Reader::addError(
const std::string& message, Token& token,
Location extra) {
735 info.message_ = message;
737 errors_.push_back(info);
741 bool Reader::recoverFromError(TokenType skipUntilToken) {
742 int errorCount = int(errors_.size());
745 if (!readToken(skip))
746 errors_.resize(errorCount);
747 if (skip.type_ == skipUntilToken || skip.type_ == tokenEndOfStream)
750 errors_.resize(errorCount);
754 bool Reader::addErrorAndRecover(
const std::string& message,
756 TokenType skipUntilToken) {
757 addError(message, token);
758 return recoverFromError(skipUntilToken);
761 Value& Reader::currentValue() {
return *(nodes_.top()); }
764 if (current_ == end_)
769 void Reader::getLocationLineAndColumn(
Location location,
775 while (current < location && current != end_) {
778 if (*current ==
'\n')
780 lastLineStart = current;
782 }
else if (c ==
'\n') {
783 lastLineStart = current;
788 column = int(location - lastLineStart) + 1;
792 std::string Reader::getLocationLineAndColumn(
Location location)
const {
794 getLocationLineAndColumn(location, line, column);
795 char buffer[18 + 16 + 16 + 1];
796 #if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__) 798 _snprintf(buffer,
sizeof(buffer),
"Line %d, Column %d", line, column);
800 sprintf_s(buffer,
sizeof(buffer),
"Line %d, Column %d", line, column);
803 snprintf(buffer,
sizeof(buffer),
"Line %d, Column %d", line, column);
814 std::string formattedMessage;
815 for (Errors::const_iterator itError = errors_.begin();
816 itError != errors_.end();
818 const ErrorInfo& error = *itError;
820 "* " + getLocationLineAndColumn(error.token_.start_) +
"\n";
821 formattedMessage +=
" " + error.message_ +
"\n";
824 "See " + getLocationLineAndColumn(error.extra_) +
" for detail.\n";
826 return formattedMessage;
830 std::vector<Reader::StructuredError> allErrors;
831 for (Errors::const_iterator itError = errors_.begin();
832 itError != errors_.end();
834 const ErrorInfo& error = *itError;
838 structured.
message = error.message_;
839 allErrors.push_back(structured);
845 size_t length = end_ - begin_;
850 token.type_ = tokenError;
855 info.message_ = message;
857 errors_.push_back(info);
862 size_t length = end_ - begin_;
868 token.type_ = tokenError;
873 info.message_ = message;
875 errors_.push_back(info);
880 return !errors_.size();
886 static OurFeatures all();
890 bool allowDroppedNullPlaceholders_;
891 bool allowNumericKeys_;
892 bool allowSingleQuotes_;
901 OurFeatures::OurFeatures()
902 : allowComments_(
true), strictRoot_(
false)
903 , allowDroppedNullPlaceholders_(
false), allowNumericKeys_(
false)
904 , allowSingleQuotes_(
false)
905 , failIfExtra_(
false)
909 OurFeatures OurFeatures::all() {
return OurFeatures(); }
925 OurReader(OurFeatures
const& features);
926 bool parse(
const char* beginDoc,
929 bool collectComments =
true);
937 OurReader(OurReader
const&);
938 void operator=(OurReader
const&);
941 tokenEndOfStream = 0,
952 tokenMemberSeparator,
967 std::string message_;
971 typedef std::deque<ErrorInfo> Errors;
973 bool readToken(Token& token);
975 bool match(Location pattern,
int patternLength);
977 bool readCStyleComment();
978 bool readCppStyleComment();
980 bool readStringSingleQuote();
983 bool readObject(Token& token);
984 bool readArray(Token& token);
985 bool decodeNumber(Token& token);
986 bool decodeNumber(Token& token,
Value& decoded);
987 bool decodeString(Token& token);
988 bool decodeString(Token& token, std::string& decoded);
989 bool decodeDouble(Token& token);
990 bool decodeDouble(Token& token,
Value& decoded);
991 bool decodeUnicodeCodePoint(Token& token,
994 unsigned int& unicode);
995 bool decodeUnicodeEscapeSequence(Token& token,
998 unsigned int& unicode);
999 bool addError(
const std::string& message, Token& token, Location extra = 0);
1000 bool recoverFromError(TokenType skipUntilToken);
1001 bool addErrorAndRecover(
const std::string& message,
1003 TokenType skipUntilToken);
1004 void skipUntilSpace();
1005 Value& currentValue();
1008 getLocationLineAndColumn(Location location,
int& line,
int& column)
const;
1009 std::string getLocationLineAndColumn(Location location)
const;
1011 void skipCommentTokens(Token& token);
1013 typedef std::stack<Value*> Nodes;
1016 std::string document_;
1020 Location lastValueEnd_;
1022 std::string commentsBefore_;
1025 OurFeatures
const features_;
1026 bool collectComments_;
1031 OurReader::OurReader(OurFeatures
const& features)
1032 : errors_(), document_(), begin_(), end_(), current_(), lastValueEnd_(),
1033 lastValue_(), commentsBefore_(), features_(features), collectComments_() {
1036 bool OurReader::parse(
const char* beginDoc,
1039 bool collectComments) {
1040 if (!features_.allowComments_) {
1041 collectComments =
false;
1046 collectComments_ = collectComments;
1050 commentsBefore_ =
"";
1052 while (!nodes_.empty())
1057 bool successful = readValue();
1059 skipCommentTokens(token);
1060 if (features_.failIfExtra_) {
1061 if (token.type_ != tokenError && token.type_ != tokenEndOfStream) {
1062 addError(
"Extra non-whitespace after JSON value.", token);
1066 if (collectComments_ && !commentsBefore_.empty())
1068 if (features_.strictRoot_) {
1072 token.type_ = tokenError;
1073 token.start_ = beginDoc;
1074 token.end_ = endDoc;
1076 "A valid JSON document must be either an array or an object value.",
1084 bool OurReader::readValue() {
1085 if (stackDepth_ >= features_.stackLimit_)
throwRuntimeError(
"Exceeded stackLimit in readValue().");
1088 skipCommentTokens(token);
1089 bool successful =
true;
1091 if (collectComments_ && !commentsBefore_.empty()) {
1093 commentsBefore_ =
"";
1096 switch (token.type_) {
1097 case tokenObjectBegin:
1098 successful = readObject(token);
1099 currentValue().setOffsetLimit(current_ - begin_);
1101 case tokenArrayBegin:
1102 successful = readArray(token);
1103 currentValue().setOffsetLimit(current_ - begin_);
1106 successful = decodeNumber(token);
1109 successful = decodeString(token);
1114 currentValue().swapPayload(v);
1115 currentValue().setOffsetStart(token.start_ - begin_);
1116 currentValue().setOffsetLimit(token.end_ - begin_);
1122 currentValue().swapPayload(v);
1123 currentValue().setOffsetStart(token.start_ - begin_);
1124 currentValue().setOffsetLimit(token.end_ - begin_);
1131 currentValue().setOffsetStart(token.start_ - begin_);
1132 currentValue().setOffsetLimit(token.end_ - begin_);
1135 case tokenArraySeparator:
1136 case tokenObjectEnd:
1138 if (features_.allowDroppedNullPlaceholders_) {
1144 currentValue().setOffsetStart(current_ - begin_ - 1);
1145 currentValue().setOffsetLimit(current_ - begin_);
1149 currentValue().setOffsetStart(token.start_ - begin_);
1150 currentValue().setOffsetLimit(token.end_ - begin_);
1151 return addError(
"Syntax error: value, object or array expected.", token);
1154 if (collectComments_) {
1155 lastValueEnd_ = current_;
1156 lastValue_ = ¤tValue();
1163 void OurReader::skipCommentTokens(Token& token) {
1164 if (features_.allowComments_) {
1167 }
while (token.type_ == tokenComment);
1173 bool OurReader::readToken(Token& token) {
1175 token.start_ = current_;
1176 Char c = getNextChar();
1180 token.type_ = tokenObjectBegin;
1183 token.type_ = tokenObjectEnd;
1186 token.type_ = tokenArrayBegin;
1189 token.type_ = tokenArrayEnd;
1192 token.type_ = tokenString;
1196 if (features_.allowSingleQuotes_) {
1197 token.type_ = tokenString;
1198 ok = readStringSingleQuote();
1202 token.type_ = tokenComment;
1216 token.type_ = tokenNumber;
1220 token.type_ = tokenTrue;
1221 ok = match(
"rue", 3);
1224 token.type_ = tokenFalse;
1225 ok = match(
"alse", 4);
1228 token.type_ = tokenNull;
1229 ok = match(
"ull", 3);
1232 token.type_ = tokenArraySeparator;
1235 token.type_ = tokenMemberSeparator;
1238 token.type_ = tokenEndOfStream;
1245 token.type_ = tokenError;
1246 token.end_ = current_;
1250 void OurReader::skipSpaces() {
1251 while (current_ != end_) {
1253 if (c ==
' ' || c ==
'\t' || c ==
'\r' || c ==
'\n')
1260 bool OurReader::match(
Location pattern,
int patternLength) {
1261 if (end_ - current_ < patternLength)
1263 int index = patternLength;
1265 if (current_[index] != pattern[index])
1267 current_ += patternLength;
1271 bool OurReader::readComment() {
1272 Location commentBegin = current_ - 1;
1273 Char c = getNextChar();
1274 bool successful =
false;
1276 successful = readCStyleComment();
1278 successful = readCppStyleComment();
1282 if (collectComments_) {
1289 addComment(commentBegin, current_, placement);
1296 assert(collectComments_);
1297 const std::string& normalized =
normalizeEOL(begin, end);
1299 assert(lastValue_ != 0);
1300 lastValue_->setComment(normalized, placement);
1302 commentsBefore_ += normalized;
1306 bool OurReader::readCStyleComment() {
1307 while (current_ != end_) {
1308 Char c = getNextChar();
1309 if (c ==
'*' && *current_ ==
'/')
1312 return getNextChar() ==
'/';
1315 bool OurReader::readCppStyleComment() {
1316 while (current_ != end_) {
1317 Char c = getNextChar();
1322 if (current_ != end_ && *current_ ==
'\n')
1331 void OurReader::readNumber() {
1332 const char *p = current_;
1335 while (c >=
'0' && c <=
'9')
1336 c = (current_ = p) < end_ ? *p++ : 0;
1339 c = (current_ = p) < end_ ? *p++ : 0;
1340 while (c >=
'0' && c <=
'9')
1341 c = (current_ = p) < end_ ? *p++ : 0;
1344 if (c ==
'e' || c ==
'E') {
1345 c = (current_ = p) < end_ ? *p++ : 0;
1346 if (c ==
'+' || c ==
'-')
1347 c = (current_ = p) < end_ ? *p++ : 0;
1348 while (c >=
'0' && c <=
'9')
1349 c = (current_ = p) < end_ ? *p++ : 0;
1352 bool OurReader::readString() {
1354 while (current_ != end_) {
1365 bool OurReader::readStringSingleQuote() {
1367 while (current_ != end_) {
1377 bool OurReader::readObject(Token& tokenStart) {
1381 currentValue().swapPayload(init);
1382 currentValue().setOffsetStart(tokenStart.start_ - begin_);
1383 while (readToken(tokenName)) {
1384 bool initialTokenOk =
true;
1385 while (tokenName.type_ == tokenComment && initialTokenOk)
1386 initialTokenOk = readToken(tokenName);
1387 if (!initialTokenOk)
1389 if (tokenName.type_ == tokenObjectEnd && name.empty())
1392 if (tokenName.type_ == tokenString) {
1393 if (!decodeString(tokenName, name))
1394 return recoverFromError(tokenObjectEnd);
1395 }
else if (tokenName.type_ == tokenNumber && features_.allowNumericKeys_) {
1397 if (!decodeNumber(tokenName, numberName))
1398 return recoverFromError(tokenObjectEnd);
1405 if (!readToken(colon) || colon.type_ != tokenMemberSeparator) {
1406 return addErrorAndRecover(
1407 "Missing ':' after object member name", colon, tokenObjectEnd);
1410 if (features_.rejectDupKeys_ && currentValue().isMember(name)) {
1411 std::string msg =
"Duplicate key: '" + name +
"'";
1412 return addErrorAndRecover(
1413 msg, tokenName, tokenObjectEnd);
1415 Value& value = currentValue()[name];
1416 nodes_.push(&value);
1417 bool ok = readValue();
1420 return recoverFromError(tokenObjectEnd);
1423 if (!readToken(comma) ||
1424 (comma.type_ != tokenObjectEnd && comma.type_ != tokenArraySeparator &&
1425 comma.type_ != tokenComment)) {
1426 return addErrorAndRecover(
1427 "Missing ',' or '}' in object declaration", comma, tokenObjectEnd);
1429 bool finalizeTokenOk =
true;
1430 while (comma.type_ == tokenComment && finalizeTokenOk)
1431 finalizeTokenOk = readToken(comma);
1432 if (comma.type_ == tokenObjectEnd)
1435 return addErrorAndRecover(
1436 "Missing '}' or object member name", tokenName, tokenObjectEnd);
1439 bool OurReader::readArray(Token& tokenStart) {
1441 currentValue().swapPayload(init);
1442 currentValue().setOffsetStart(tokenStart.start_ - begin_);
1444 if (*current_ ==
']')
1447 readToken(endArray);
1452 Value& value = currentValue()[index++];
1453 nodes_.push(&value);
1454 bool ok = readValue();
1457 return recoverFromError(tokenArrayEnd);
1461 ok = readToken(token);
1462 while (token.type_ == tokenComment && ok) {
1463 ok = readToken(token);
1466 (token.type_ != tokenArraySeparator && token.type_ != tokenArrayEnd);
1467 if (!ok || badTokenType) {
1468 return addErrorAndRecover(
1469 "Missing ',' or ']' in array declaration", token, tokenArrayEnd);
1471 if (token.type_ == tokenArrayEnd)
1477 bool OurReader::decodeNumber(Token& token) {
1479 if (!decodeNumber(token, decoded))
1482 currentValue().setOffsetStart(token.start_ - begin_);
1483 currentValue().setOffsetLimit(token.end_ - begin_);
1487 bool OurReader::decodeNumber(Token& token,
Value& decoded) {
1492 bool isNegative = *current ==
'-';
1499 Value::LargestUInt threshold = maxIntegerValue / 10;
1500 Value::LargestUInt value = 0;
1501 while (current < token.end_) {
1502 Char c = *current++;
1503 if (c < '0' || c >
'9')
1504 return decodeDouble(token, decoded);
1506 if (value >= threshold) {
1511 if (value > threshold || current != token.end_ ||
1512 digit > maxIntegerValue % 10) {
1513 return decodeDouble(token, decoded);
1516 value = value * 10 + digit;
1527 bool OurReader::decodeDouble(Token& token) {
1529 if (!decodeDouble(token, decoded))
1532 currentValue().setOffsetStart(token.start_ - begin_);
1533 currentValue().setOffsetLimit(token.end_ - begin_);
1537 bool OurReader::decodeDouble(Token& token,
Value& decoded) {
1540 std::string buffer( token.start_, token.end_ );
1541 std::istringstream is(buffer);
1544 return addError(
"'" + std::string(token.start_, token.end_) +
1545 "' is not a number.",
1551 bool OurReader::decodeString(Token& token) {
1552 std::string decoded_string;
1553 if (!decodeString(token, decoded_string))
1555 Value decoded(decoded_string);
1556 currentValue().swapPayload(decoded);
1557 currentValue().setOffsetStart(token.start_ - begin_);
1558 currentValue().setOffsetLimit(token.end_ - begin_);
1562 bool OurReader::decodeString(Token& token, std::string& decoded) {
1563 decoded.reserve(token.end_ - token.start_ - 2);
1564 Location current = token.start_ + 1;
1566 while (current != end) {
1567 Char c = *current++;
1570 else if (c ==
'\\') {
1572 return addError(
"Empty escape sequence in string", token, current);
1573 Char escape = *current++;
1600 unsigned int unicode;
1601 if (!decodeUnicodeCodePoint(token, current, end, unicode))
1606 return addError(
"Bad escape sequence in string", token, current);
1615 bool OurReader::decodeUnicodeCodePoint(Token& token,
1618 unsigned int& unicode) {
1620 if (!decodeUnicodeEscapeSequence(token, current, end, unicode))
1622 if (unicode >= 0xD800 && unicode <= 0xDBFF) {
1624 if (end - current < 6)
1626 "additional six characters expected to parse unicode surrogate pair.",
1629 unsigned int surrogatePair;
1630 if (*(current++) ==
'\\' && *(current++) ==
'u') {
1631 if (decodeUnicodeEscapeSequence(token, current, end, surrogatePair)) {
1632 unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF);
1636 return addError(
"expecting another \\u token to begin the second half of " 1637 "a unicode surrogate pair",
1644 bool OurReader::decodeUnicodeEscapeSequence(Token& token,
1647 unsigned int& unicode) {
1648 if (end - current < 4)
1650 "Bad unicode escape sequence in string: four digits expected.",
1654 for (
int index = 0; index < 4; ++index) {
1655 Char c = *current++;
1657 if (c >=
'0' && c <=
'9')
1659 else if (c >=
'a' && c <=
'f')
1660 unicode += c -
'a' + 10;
1661 else if (c >=
'A' && c <=
'F')
1662 unicode += c -
'A' + 10;
1665 "Bad unicode escape sequence in string: hexadecimal digit expected.",
1673 OurReader::addError(
const std::string& message, Token& token,
Location extra) {
1675 info.token_ = token;
1676 info.message_ = message;
1677 info.extra_ = extra;
1678 errors_.push_back(info);
1682 bool OurReader::recoverFromError(TokenType skipUntilToken) {
1683 int errorCount = int(errors_.size());
1686 if (!readToken(skip))
1687 errors_.resize(errorCount);
1688 if (skip.type_ == skipUntilToken || skip.type_ == tokenEndOfStream)
1691 errors_.resize(errorCount);
1695 bool OurReader::addErrorAndRecover(
const std::string& message,
1697 TokenType skipUntilToken) {
1698 addError(message, token);
1699 return recoverFromError(skipUntilToken);
1702 Value& OurReader::currentValue() {
return *(nodes_.top()); }
1704 OurReader::Char OurReader::getNextChar() {
1705 if (current_ == end_)
1710 void OurReader::getLocationLineAndColumn(
Location location,
1712 int& column)
const {
1716 while (current < location && current != end_) {
1717 Char c = *current++;
1719 if (*current ==
'\n')
1721 lastLineStart = current;
1723 }
else if (c ==
'\n') {
1724 lastLineStart = current;
1729 column = int(location - lastLineStart) + 1;
1733 std::string OurReader::getLocationLineAndColumn(
Location location)
const {
1735 getLocationLineAndColumn(location, line, column);
1736 char buffer[18 + 16 + 16 + 1];
1737 #if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__) 1739 _snprintf(buffer,
sizeof(buffer),
"Line %d, Column %d", line, column);
1741 sprintf_s(buffer,
sizeof(buffer),
"Line %d, Column %d", line, column);
1744 snprintf(buffer,
sizeof(buffer),
"Line %d, Column %d", line, column);
1749 std::string OurReader::getFormattedErrorMessages()
const {
1750 std::string formattedMessage;
1751 for (Errors::const_iterator itError = errors_.begin();
1752 itError != errors_.end();
1754 const ErrorInfo& error = *itError;
1756 "* " + getLocationLineAndColumn(error.token_.start_) +
"\n";
1757 formattedMessage +=
" " + error.message_ +
"\n";
1760 "See " + getLocationLineAndColumn(error.extra_) +
" for detail.\n";
1762 return formattedMessage;
1765 std::vector<OurReader::StructuredError> OurReader::getStructuredErrors()
const {
1766 std::vector<OurReader::StructuredError> allErrors;
1767 for (Errors::const_iterator itError = errors_.begin();
1768 itError != errors_.end();
1770 const ErrorInfo& error = *itError;
1771 OurReader::StructuredError structured;
1772 structured.offset_start = error.token_.start_ - begin_;
1773 structured.offset_limit = error.token_.end_ - begin_;
1774 structured.message = error.message_;
1775 allErrors.push_back(structured);
1780 bool OurReader::pushError(
const Value& value,
const std::string& message) {
1781 size_t length = end_ - begin_;
1786 token.type_ = tokenError;
1790 info.token_ = token;
1791 info.message_ = message;
1793 errors_.push_back(info);
1797 bool OurReader::pushError(
const Value& value,
const std::string& message,
const Value& extra) {
1798 size_t length = end_ - begin_;
1804 token.type_ = tokenError;
1808 info.token_ = token;
1809 info.message_ = message;
1811 errors_.push_back(info);
1815 bool OurReader::good()
const {
1816 return !errors_.size();
1821 bool const collectComments_;
1825 bool collectComments,
1826 OurFeatures
const& features)
1827 : collectComments_(collectComments)
1831 char const* beginDoc,
char const* endDoc,
1832 Value* root, std::string* errs) {
1833 bool ok = reader_.parse(beginDoc, endDoc, *root, collectComments_);
1835 *errs = reader_.getFormattedErrorMessages();
1843 setDefaults(&settings_);
1849 bool collectComments = settings_[
"collectComments"].asBool();
1850 OurFeatures features = OurFeatures::all();
1851 features.allowComments_ = settings_[
"allowComments"].asBool();
1852 features.strictRoot_ = settings_[
"strictRoot"].asBool();
1853 features.allowDroppedNullPlaceholders_ = settings_[
"allowDroppedNullPlaceholders"].asBool();
1854 features.allowNumericKeys_ = settings_[
"allowNumericKeys"].asBool();
1855 features.allowSingleQuotes_ = settings_[
"allowSingleQuotes"].asBool();
1856 features.stackLimit_ = settings_[
"stackLimit"].asInt();
1857 features.failIfExtra_ = settings_[
"failIfExtra"].asBool();
1858 features.rejectDupKeys_ = settings_[
"rejectDupKeys"].asBool();
1859 return new OurCharReader(collectComments, features);
1863 valid_keys->clear();
1864 valid_keys->insert(
"collectComments");
1865 valid_keys->insert(
"allowComments");
1866 valid_keys->insert(
"strictRoot");
1867 valid_keys->insert(
"allowDroppedNullPlaceholders");
1868 valid_keys->insert(
"allowNumericKeys");
1869 valid_keys->insert(
"allowSingleQuotes");
1870 valid_keys->insert(
"stackLimit");
1871 valid_keys->insert(
"failIfExtra");
1872 valid_keys->insert(
"rejectDupKeys");
1877 if (!invalid) invalid = &my_invalid;
1879 std::set<std::string> valid_keys;
1882 size_t n = keys.size();
1883 for (
size_t i = 0; i < n; ++i) {
1884 std::string
const& key = keys[i];
1885 if (valid_keys.find(key) == valid_keys.end()) {
1886 inv[key] = settings_[key];
1889 return 0u == inv.
size();
1893 return settings_[key];
1899 (*settings)[
"allowComments"] =
false;
1900 (*settings)[
"strictRoot"] =
true;
1901 (*settings)[
"allowDroppedNullPlaceholders"] =
false;
1902 (*settings)[
"allowNumericKeys"] =
false;
1903 (*settings)[
"allowSingleQuotes"] =
false;
1904 (*settings)[
"failIfExtra"] =
true;
1905 (*settings)[
"rejectDupKeys"] =
true;
1912 (*settings)[
"collectComments"] =
true;
1913 (*settings)[
"allowComments"] =
true;
1914 (*settings)[
"strictRoot"] =
false;
1915 (*settings)[
"allowDroppedNullPlaceholders"] =
false;
1916 (*settings)[
"allowNumericKeys"] =
false;
1917 (*settings)[
"allowSingleQuotes"] =
false;
1918 (*settings)[
"stackLimit"] = 1000;
1919 (*settings)[
"failIfExtra"] =
false;
1920 (*settings)[
"rejectDupKeys"] =
false;
1929 Value* root, std::string* errs)
1931 std::ostringstream ssin;
1932 ssin << sin.rdbuf();
1933 std::string doc = ssin.str();
1934 char const* begin = doc.data();
1935 char const* end = begin + doc.size();
1938 return reader->parse(begin, end, root, errs);
1947 "Error from reader: %s",
static std::string codePointToUTF8(unsigned int cp)
Converts a unicode code-point to UTF-8.
std::string asString() const
Embedded zeroes are possible.
std::vector< std::string > Members
virtual CharReader * newCharReader() const
Allocate a CharReader via operator new().
static void strictMode(Json::Value *settings)
Same as old Features::strictMode().
array value (ordered list)
std::auto_ptr< CharReader > CharReaderPtr
bool parseFromStream(CharReader::Factory const &, std::istream &, Value *root, std::string *errs)
Consume entire stream and use its begin/end.
object value (collection of name/value pairs).
std::istream & operator>>(std::istream &, Value &)
Read from 'sin' into 'root'.
std::string getFormatedErrorMessages() const
Returns a user friendly string that list errors in the parsed document.
void swapPayload(Value &other)
Swap values but leave comments and source offsets in place.
void setOffsetStart(size_t start)
Value & operator[](std::string key)
A simple way to update a specific setting.
static const Int maxInt
Maximum signed int value that can be stored in a Json::Value.
Json::LargestUInt LargestUInt
Features()
Initialize the configuration like JsonConfig::allFeatures;.
An error tagged with where in the JSON text it was encountered.
std::vector< StructuredError > getStructuredErrors() const
Returns a vector of structured erros encounted while parsing.
void setComment(const char *comment, CommentPlacement placement)
static const LargestInt minLargestInt
Minimum signed integer value that can be stored in a Json::Value.
bool allowComments_
true if comments are allowed. Default: true.
bool allowNumericKeys_
true if numeric object key are allowed. Default: false.
size_t getOffsetLimit() const
bool good() const
Return whether there are any errors.
bool parse(const std::string &document, Value &root, bool collectComments=true)
Read a Value from a JSON document.
JSON (JavaScript Object Notation).
bool allowDroppedNullPlaceholders_
true if dropped null placeholders are allowed. Default: false.
bool validate(Json::Value *invalid) const
Json::LargestInt LargestInt
static int const stackLimit_g
void throwRuntimeError(std::string const &msg)
used internally
static void setDefaults(Json::Value *settings)
Called by ctor, but you can use this to reset settings_.
Interface for reading JSON from a char array.
ArrayIndex size() const
Number of values in array or object.
virtual ~CharReaderBuilder()
void setOffsetLimit(size_t limit)
static Features all()
A configuration that allows all features and assumes all strings are UTF-8.
static std::string normalizeEOL(Reader::Location begin, Reader::Location end)
a comment on the line after a value (only make sense for
bool pushError(const Value &value, const std::string &message)
Add a semantic error message.
static Features strictMode()
A configuration that is strictly compatible with the JSON specification.
bool strictRoot_
true if root must be either an array or an object value.
Build a CharReader implementation.
size_t getOffsetStart() const
static void getValidReaderKeys(std::set< std::string > *valid_keys)
static bool containsNewLine(Reader::Location begin, Reader::Location end)
Configuration passed to reader and writer.
virtual CharReader * newCharReader() const =0
Allocate a CharReader via operator new().
a comment placed on the line before a value
Reader()
Constructs a Reader allowing all features for parsing.
std::string getFormattedErrorMessages() const
Returns a user friendly string that list errors in the parsed document.
a comment just after a value on the same line
static const LargestUInt maxLargestUInt
Maximum unsigned integer value that can be stored in a Json::Value.