93 bool coerce_utf8 =
false,
bool allow_empty_null =
false,
94 bool loose_float_number_conversion =
false) {
98 if (split == json.
length()) {
100 for (
int i = 0;
i < json.
length(); ++
i) {
107 return parser.FinishParse();
116 result =
parser.Parse(rest);
118 result =
parser.FinishParse();
128 bool allow_empty_null =
false,
129 bool loose_float_number_conversion =
false) {
131 loose_float_number_conversion);
140 bool allow_empty_null =
false) {
164 for (
int i = 0;
i <=
str.length(); ++
i) {
165 ow_.RenderBool(
"",
true);
172 for (
int i = 0;
i <=
str.length(); ++
i) {
173 ow_.RenderBool(
"",
false);
180 for (
int i = 0;
i <=
str.length(); ++
i) {
189 for (
int i = 0;
i <=
str.length(); ++
i) {
190 ow_.StartObject(
"")->EndObject();
197 for (
int i = 0;
i <=
str.length(); ++
i) {
198 ow_.StartList(
"")->EndList();
206 for (
int i = 0;
i <=
str.length(); ++
i) {
207 ow_.RenderDouble(
"", 42.5);
214 for (
int i = 0;
i <
str.length(); ++
i) {
215 ow_.RenderDouble(
"", 1.2345e-10);
222 for (
int i = 0;
i <=
str.length(); ++
i) {
223 ow_.RenderDouble(
"", -1045.235);
230 for (
int i = 0;
i <=
str.length(); ++
i) {
231 ow_.RenderUint64(
"", 123456);
238 for (
int i = 0;
i <=
str.length(); ++
i) {
239 ow_.RenderInt64(
"", -79497823553162765
LL);
246 for (
int i = 0;
i <=
str.length(); ++
i) {
247 ow_.RenderUint64(
"", 11779497823553162765
ULL);
254 for (
int i = 0;
i <=
str.length(); ++
i) {
255 DoErrorTest(
str,
i,
"Octal/hex numbers are not valid JSON values.");
258 for (
int i = 0;
i <=
str.length(); ++
i) {
259 DoErrorTest(
str,
i,
"Octal/hex numbers are not valid JSON values.");
265 for (
int i = 0;
i <=
str.length(); ++
i) {
266 DoErrorTest(
str,
i,
"Octal/hex numbers are not valid JSON values.");
269 for (
int i = 0;
i <=
str.length(); ++
i) {
270 DoErrorTest(
str,
i,
"Octal/hex numbers are not valid JSON values.");
273 for (
int i = 0;
i <=
str.length(); ++
i) {
274 DoErrorTest(
str,
i,
"Unable to parse number.");
281 for (
int i = 0;
i <=
str.length(); ++
i) {
282 ow_.RenderString(
"",
"");
289 for (
int i = 0;
i <=
str.length(); ++
i) {
290 ow_.RenderString(
"",
"");
297 for (
int i = 0;
i <=
str.length(); ++
i) {
298 ow_.RenderString(
"",
"Some String");
305 for (
int i = 0;
i <=
str.length(); ++
i) {
306 ow_.RenderString(
"",
"Another String");
314 "{'s': true, \"d\": false, key: null, snake_key: [], camelKey: {}}";
315 for (
int i = 0;
i <=
str.length(); ++
i) {
317 ->RenderBool(
"s",
true)
318 ->RenderBool(
"d",
false)
320 ->StartList(
"snake_key")
322 ->StartObject(
"camelKey")
332 for (
int i = 0;
i <=
str.length(); ++
i) {
334 ->RenderBool(
"",
true)
335 ->RenderBool(
"",
false)
337 ->RenderString(
"",
"one")
338 ->RenderString(
"",
"two")
347 "[[22, -127, 45.3, -1056.4, 11779497823553162765], {'key': true}]";
348 for (
int i = 0;
i <=
str.length(); ++
i) {
351 ->RenderUint64(
"", 22)
352 ->RenderInt64(
"", -127)
353 ->RenderDouble(
"", 45.3)
354 ->RenderDouble(
"", -1056.4)
355 ->RenderUint64(
"", 11779497823553162765
ULL)
358 ->RenderBool(
"key",
true)
367 TEST_F(JsonStreamParserTest, ObjectValues) {
369 "{t: true, f: false, n: null, s: 'a string', d: \"another string\", pi: "
370 "22, ni: -127, pd: 45.3, nd: -1056.4, pl: 11779497823553162765, l: [[]], "
372 for (
int i = 0;
i <=
str.length(); ++
i) {
374 ->RenderBool(
"t",
true)
375 ->RenderBool(
"f",
false)
377 ->RenderString(
"s",
"a string")
378 ->RenderString(
"d",
"another string")
379 ->RenderUint64(
"pi", 22)
380 ->RenderInt64(
"ni", -127)
381 ->RenderDouble(
"pd", 45.3)
382 ->RenderDouble(
"nd", -1056.4)
383 ->RenderUint64(
"pl", 11779497823553162765
ULL)
389 ->RenderBool(
"key",
true)
397 TEST_F(JsonStreamParserTest, RejectNonUtf8WhenNotCoerced) {
398 StringPiece json =
"{\"address\":\xFF\"חרושת 23, רעננה, ישראל\"}";
399 for (
int i = 0;
i <= json.length(); ++
i) {
400 DoErrorTest(json,
i,
"Encountered non UTF-8 code points.");
402 json =
"{\"address\": \"חרושת 23,\xFFרעננה, ישראל\"}";
403 for (
int i = 0;
i <= json.length(); ++
i) {
404 DoErrorTest(json,
i,
"Encountered non UTF-8 code points.");
406 DoErrorTest(
"\xFF{}", 0,
"Encountered non UTF-8 code points.");
410 TEST_F(JsonStreamParserTest, UnicodeEscaping) {
411 StringPiece
str =
"[\"\\u0639\\u0631\\u0628\\u0649\"]";
412 for (
int i = 0;
i <=
str.length(); ++
i) {
414 ->RenderString(
"",
"\xD8\xB9\xD8\xB1\xD8\xA8\xD9\x89")
421 TEST_F(JsonStreamParserTest, UnicodeSurrogatePairEscaping) {
423 "[\"\\u0bee\\ud800\\uddf1\\uD80C\\uDDA4\\uD83d\\udC1D\\uD83C\\uDF6F\"]";
424 for (
int i = 0;
i <=
str.length(); ++
i) {
427 "\xE0\xAF\xAE\xF0\x90\x87\xB1\xF0\x93\x86\xA4\xF0"
428 "\x9F\x90\x9D\xF0\x9F\x8D\xAF")
435 TEST_F(JsonStreamParserTest, UnicodeEscapingInvalidCodePointWhenNotCoerced) {
437 StringPiece
str =
"[\"\\ude36\"]";
438 for (
int i = 0;
i <=
str.length(); ++
i) {
439 DoErrorTest(
str,
i,
"Invalid unicode code point.");
443 TEST_F(JsonStreamParserTest, UnicodeEscapingMissingLowSurrogateWhenNotCoerced) {
445 StringPiece
str =
"[\"\\ud83d\"]";
446 for (
int i = 0;
i <=
str.length(); ++
i) {
447 DoErrorTest(
str,
i,
"Missing low surrogate.");
450 str =
"[\"\\ud83d|ude36\"]";
451 for (
int i = 0;
i <=
str.length(); ++
i) {
452 DoErrorTest(
str,
i,
"Missing low surrogate.");
455 str =
"[\"\\ud83d\\ude--\"]";
456 for (
int i = 0;
i <=
str.length(); ++
i) {
457 DoErrorTest(
str,
i,
"Invalid escape sequence.");
460 str =
"[\"\\ud83d\\ud83d\"]";
461 for (
int i = 0;
i <=
str.length(); ++
i) {
462 DoErrorTest(
str,
i,
"Invalid low surrogate.");
467 TEST_F(JsonStreamParserTest, AsciiEscaping) {
469 "[\"\\b\", \"\\ning\", \"test\\f\", \"\\r\\t\", \"test\\\\\\ving\"]";
470 for (
int i = 0;
i <=
str.length(); ++
i) {
472 ->RenderString(
"",
"\b")
473 ->RenderString(
"",
"\ning")
474 ->RenderString(
"",
"test\f")
475 ->RenderString(
"",
"\r\t")
476 ->RenderString(
"",
"test\\\ving")
483 TEST_F(JsonStreamParserTest, TrailingCommas) {
484 StringPiece
str =
"[['a',true,], {b: null,},]";
485 for (
int i = 0;
i <=
str.length(); ++
i) {
488 ->RenderString(
"",
"a")
489 ->RenderBool(
"",
true)
502 TEST_F(JsonStreamParserTest, ExtraTextAfterTrue) {
503 StringPiece
str =
"truee";
504 for (
int i = 0;
i <=
str.length(); ++
i) {
505 ow_.RenderBool(
"",
true);
506 DoErrorTest(
str,
i,
"Parsing terminated before end of input.");
510 TEST_F(JsonStreamParserTest, InvalidNumberDashOnly) {
511 StringPiece
str =
"-";
512 for (
int i = 0;
i <=
str.length(); ++
i) {
513 DoErrorTest(
str,
i,
"Unable to parse number.");
517 TEST_F(JsonStreamParserTest, InvalidNumberDashName) {
518 StringPiece
str =
"-foo";
519 for (
int i = 0;
i <=
str.length(); ++
i) {
520 DoErrorTest(
str,
i,
"Unable to parse number.");
524 TEST_F(JsonStreamParserTest, InvalidLiteralInArray) {
525 StringPiece
str =
"[nule]";
526 for (
int i = 0;
i <=
str.length(); ++
i) {
528 DoErrorTest(
str,
i,
"Unexpected token.");
532 TEST_F(JsonStreamParserTest, InvalidLiteralInObject) {
533 StringPiece
str =
"{123false}";
534 for (
int i = 0;
i <=
str.length(); ++
i) {
536 DoErrorTest(
str,
i,
"Expected an object key or }.");
541 TEST_F(JsonStreamParserTest, MismatchedSingleQuotedLiteral) {
542 StringPiece
str =
"'Some str\"";
543 for (
int i = 0;
i <=
str.length(); ++
i) {
544 DoErrorTest(
str,
i,
"Closing quote expected in string.");
548 TEST_F(JsonStreamParserTest, MismatchedDoubleQuotedLiteral) {
549 StringPiece
str =
"\"Another string that ends poorly!'";
550 for (
int i = 0;
i <=
str.length(); ++
i) {
551 DoErrorTest(
str,
i,
"Closing quote expected in string.");
556 TEST_F(JsonStreamParserTest, UnterminatedLiteralString) {
557 StringPiece
str =
"\"Forgot the rest of i";
558 for (
int i = 0;
i <=
str.length(); ++
i) {
559 DoErrorTest(
str,
i,
"Closing quote expected in string.");
563 TEST_F(JsonStreamParserTest, UnterminatedStringEscape) {
564 StringPiece
str =
"\"Forgot the rest of \\";
565 for (
int i = 0;
i <=
str.length(); ++
i) {
566 DoErrorTest(
str,
i,
"Closing quote expected in string.");
570 TEST_F(JsonStreamParserTest, UnterminatedStringInArray) {
571 StringPiece
str =
"[\"Forgot to close the string]";
572 for (
int i = 0;
i <=
str.length(); ++
i) {
574 DoErrorTest(
str,
i,
"Closing quote expected in string.");
578 TEST_F(JsonStreamParserTest, UnterminatedStringInObject) {
579 StringPiece
str =
"{f: \"Forgot to close the string}";
580 for (
int i = 0;
i <=
str.length(); ++
i) {
582 DoErrorTest(
str,
i,
"Closing quote expected in string.");
586 TEST_F(JsonStreamParserTest, UnterminatedObject) {
587 StringPiece
str =
"{";
588 for (
int i = 0;
i <=
str.length(); ++
i) {
590 DoErrorTest(
str,
i,
"Unexpected end of string.");
596 TEST_F(JsonStreamParserTest, MismatchedCloseObject) {
597 StringPiece
str =
"{'key': true]";
598 for (
int i = 0;
i <=
str.length(); ++
i) {
599 ow_.StartObject(
"")->RenderBool(
"key",
true);
600 DoErrorTest(
str,
i,
"Expected , or } after key:value pair.");
604 TEST_F(JsonStreamParserTest, MismatchedCloseArray) {
605 StringPiece
str =
"[true, null}";
606 for (
int i = 0;
i <=
str.length(); ++
i) {
607 ow_.StartList(
"")->RenderBool(
"",
true)->RenderNull(
"");
608 DoErrorTest(
str,
i,
"Expected , or ] after array value.");
613 TEST_F(JsonStreamParserTest, InvalidNumericObjectKey) {
614 StringPiece
str =
"{42: true}";
615 for (
int i = 0;
i <=
str.length(); ++
i) {
617 DoErrorTest(
str,
i,
"Expected an object key or }.");
621 TEST_F(JsonStreamParserTest, InvalidLiteralObjectInObject) {
622 StringPiece
str =
"{{bob: true}}";
623 for (
int i = 0;
i <=
str.length(); ++
i) {
625 DoErrorTest(
str,
i,
"Expected an object key or }.");
629 TEST_F(JsonStreamParserTest, InvalidLiteralArrayInObject) {
630 StringPiece
str =
"{[null]}";
631 for (
int i = 0;
i <=
str.length(); ++
i) {
633 DoErrorTest(
str,
i,
"Expected an object key or }.");
637 TEST_F(JsonStreamParserTest, InvalidLiteralValueInObject) {
638 StringPiece
str =
"{false}";
639 for (
int i = 0;
i <=
str.length(); ++
i) {
641 DoErrorTest(
str,
i,
"Expected an object key or }.");
645 TEST_F(JsonStreamParserTest, MissingColonAfterStringInObject) {
646 StringPiece
str =
"{\"key\"}";
647 for (
int i = 0;
i <=
str.length(); ++
i) {
649 DoErrorTest(
str,
i,
"Expected : between key:value pair.");
653 TEST_F(JsonStreamParserTest, MissingColonAfterKeyInObject) {
654 StringPiece
str =
"{key}";
655 for (
int i = 0;
i <=
str.length(); ++
i) {
657 DoErrorTest(
str,
i,
"Expected : between key:value pair.");
661 TEST_F(JsonStreamParserTest, EndOfTextAfterKeyInObject) {
662 StringPiece
str =
"{key";
663 for (
int i = 0;
i <=
str.length(); ++
i) {
665 DoErrorTest(
str,
i,
"Unexpected end of string.");
669 TEST_F(JsonStreamParserTest, MissingValueAfterColonInObject) {
670 StringPiece
str =
"{key:}";
671 for (
int i = 0;
i <=
str.length(); ++
i) {
673 DoErrorTest(
str,
i,
"Unexpected token.");
677 TEST_F(JsonStreamParserTest, MissingCommaBetweenObjectEntries) {
678 StringPiece
str =
"{key:20 'hello': true}";
679 for (
int i = 0;
i <=
str.length(); ++
i) {
680 ow_.StartObject(
"")->RenderUint64(
"key", 20);
681 DoErrorTest(
str,
i,
"Expected , or } after key:value pair.");
685 TEST_F(JsonStreamParserTest, InvalidLiteralAsObjectKey) {
686 StringPiece
str =
"{false: 20}";
687 for (
int i = 0;
i <=
str.length(); ++
i) {
689 DoErrorTest(
str,
i,
"Expected an object key or }.");
693 TEST_F(JsonStreamParserTest, ExtraCharactersAfterObject) {
694 StringPiece
str =
"{}}";
695 for (
int i = 0;
i <=
str.length(); ++
i) {
696 ow_.StartObject(
"")->EndObject();
697 DoErrorTest(
str,
i,
"Parsing terminated before end of input.");
701 TEST_F(JsonStreamParserTest, PositiveNumberTooBigIsDouble) {
702 StringPiece
str =
"18446744073709551616";
703 for (
int i = 0;
i <=
str.length(); ++
i) {
704 ow_.RenderDouble(
"", 18446744073709552000.0);
709 TEST_F(JsonStreamParserTest, NegativeNumberTooBigIsDouble) {
710 StringPiece
str =
"-18446744073709551616";
711 for (
int i = 0;
i <=
str.length(); ++
i) {
712 ow_.RenderDouble(
"", -18446744073709551616.0);
717 TEST_F(JsonStreamParserTest, DoubleTooBig) {
718 StringPiece
str =
"[1.89769e+308]";
719 for (
int i = 0;
i <=
str.length(); ++
i) {
721 DoErrorTest(
str,
i,
"Number exceeds the range of double.");
723 str =
"[-1.89769e+308]";
724 for (
int i = 0;
i <=
str.length(); ++
i) {
726 DoErrorTest(
str,
i,
"Number exceeds the range of double.");
732 TEST_F(JsonStreamParserTest, UnfinishedEscape) {
733 StringPiece
str =
"\"\\";
734 for (
int i = 0;
i <=
str.length(); ++
i) {
735 DoErrorTest(
str,
i,
"Closing quote expected in string.");
740 TEST_F(JsonStreamParserTest, UnfinishedUnicodeEscape) {
741 StringPiece
str =
"\"\\u";
742 for (
int i = 0;
i <=
str.length(); ++
i) {
743 DoErrorTest(
str,
i,
"Illegal hex string.");
748 TEST_F(JsonStreamParserTest, UnicodeEscapeCutOff) {
749 StringPiece
str =
"\"\\u12";
750 for (
int i = 0;
i <=
str.length(); ++
i) {
751 DoErrorTest(
str,
i,
"Illegal hex string.");
756 TEST_F(JsonStreamParserTest, BracketedUnicodeEscape) {
757 StringPiece
str =
"\"\\u{1f36f}\"";
758 for (
int i = 0;
i <=
str.length(); ++
i) {
759 DoErrorTest(
str,
i,
"Invalid escape sequence.");
764 TEST_F(JsonStreamParserTest, UnicodeEscapeInvalidCharacters) {
765 StringPiece
str =
"\"\\u12$4hello";
766 for (
int i = 0;
i <=
str.length(); ++
i) {
767 DoErrorTest(
str,
i,
"Invalid escape sequence.");
772 TEST_F(JsonStreamParserTest, UnicodeEscapeLowHalfSurrogateInvalidCharacters) {
773 StringPiece
str =
"\"\\ud800\\udcfg\"";
774 for (
int i = 0;
i <=
str.length(); ++
i) {
775 DoErrorTest(
str,
i,
"Invalid escape sequence.");
780 TEST_F(JsonStreamParserTest, ExtraCommaInObject) {
781 StringPiece
str =
"{'k1': true,,'k2': false}";
782 for (
int i = 0;
i <=
str.length(); ++
i) {
783 ow_.StartObject(
"")->RenderBool(
"k1",
true);
784 DoErrorTest(
str,
i,
"Expected an object key or }.");
788 TEST_F(JsonStreamParserTest, ExtraCommaInArray) {
789 StringPiece
str =
"[true,,false}";
790 for (
int i = 0;
i <=
str.length(); ++
i) {
791 ow_.StartList(
"")->RenderBool(
"",
true);
792 DoErrorTest(
str,
i,
"Unexpected token.");
797 TEST_F(JsonStreamParserTest, ExtraTextAfterLiteral) {
798 StringPiece
str =
"'hello', 'world'";
799 for (
int i = 0;
i <=
str.length(); ++
i) {
800 ow_.RenderString(
"",
"hello");
801 DoErrorTest(
str,
i,
"Parsing terminated before end of input.");
805 TEST_F(JsonStreamParserTest, ExtraTextAfterObject) {
806 StringPiece
str =
"{'key': true} 'oops'";
807 for (
int i = 0;
i <=
str.length(); ++
i) {
808 ow_.StartObject(
"")->RenderBool(
"key",
true)->EndObject();
809 DoErrorTest(
str,
i,
"Parsing terminated before end of input.");
813 TEST_F(JsonStreamParserTest, ExtraTextAfterArray) {
814 StringPiece
str =
"[null] 'oops'";
815 for (
int i = 0;
i <=
str.length(); ++
i) {
816 ow_.StartList(
"")->RenderNull(
"")->EndList();
817 DoErrorTest(
str,
i,
"Parsing terminated before end of input.");
822 TEST_F(JsonStreamParserTest, UnknownCharactersAsValue) {
823 StringPiece
str =
"*";
824 for (
int i = 0;
i <=
str.length(); ++
i) {
825 DoErrorTest(
str,
i,
"Expected a value.");
829 TEST_F(JsonStreamParserTest, UnknownCharactersInArray) {
830 StringPiece
str =
"[*]";
831 for (
int i = 0;
i <=
str.length(); ++
i) {
833 DoErrorTest(
str,
i,
"Expected a value or ] within an array.");
837 TEST_F(JsonStreamParserTest, UnknownCharactersInObject) {
838 StringPiece
str =
"{'key': *}";
839 for (
int i = 0;
i <=
str.length(); ++
i) {
841 DoErrorTest(
str,
i,
"Expected a value.");
845 TEST_F(JsonStreamParserTest, DeepNestJsonNotExceedLimit) {
851 StrAppend(&
str,
"{'nest64':'v1', 'nest64': false, 'nest64': ['v2']}");
857 ow_.StartObject(
"a");
859 ow_.RenderString(
"nest64",
"v1")
860 ->RenderBool(
"nest64",
false)
861 ->StartList(
"nest64")
862 ->RenderString(
"",
"v2")
871 TEST_F(JsonStreamParserTest, DeepNestJsonExceedLimit) {
879 "{'nest11' : [{'nest12' : null,},],"
880 "'nest21' : {'nest22' : {'nest23' : false}}}");
885 "Message too deep. Max recursion depth reached for key 'nest22'");