mirror of
https://github.com/nlohmann/json
synced 2024-12-24 01:30:08 +00:00
added compliance test cases
This commit is contained in:
parent
9f9d293bad
commit
869035a6c9
50
src/json.hpp
50
src/json.hpp
@ -3631,7 +3631,7 @@ class basic_json
|
||||
static const unsigned char yybm[] =
|
||||
{
|
||||
0, 64, 64, 64, 64, 64, 64, 64,
|
||||
64, 96, 96, 64, 64, 96, 64, 64,
|
||||
64, 32, 32, 64, 64, 32, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64,
|
||||
64, 64, 64, 64, 64, 64, 64, 64,
|
||||
96, 64, 0, 64, 64, 64, 64, 64,
|
||||
@ -3914,11 +3914,26 @@ basic_json_parser_25:
|
||||
basic_json_parser_26:
|
||||
yyaccept = 0;
|
||||
yych = *(m_marker = ++m_cursor);
|
||||
if (yych <= 0x00)
|
||||
if (yych <= '\n')
|
||||
{
|
||||
if (yych <= 0x00)
|
||||
{
|
||||
goto basic_json_parser_19;
|
||||
}
|
||||
if (yych <= 0x08)
|
||||
{
|
||||
goto basic_json_parser_31;
|
||||
}
|
||||
goto basic_json_parser_19;
|
||||
}
|
||||
goto basic_json_parser_31;
|
||||
else
|
||||
{
|
||||
if (yych == '\r')
|
||||
{
|
||||
goto basic_json_parser_19;
|
||||
}
|
||||
goto basic_json_parser_31;
|
||||
}
|
||||
basic_json_parser_27:
|
||||
++m_cursor;
|
||||
{
|
||||
@ -3939,7 +3954,7 @@ basic_json_parser_31:
|
||||
{
|
||||
goto basic_json_parser_30;
|
||||
}
|
||||
if (yych <= 0x00)
|
||||
if (yych <= '\r')
|
||||
{
|
||||
goto basic_json_parser_32;
|
||||
}
|
||||
@ -4395,7 +4410,7 @@ basic_json_parser_59:
|
||||
m_buffer.erase(0, static_cast<size_t>(offset_start));
|
||||
std::string line;
|
||||
std::getline(*m_stream, line);
|
||||
m_buffer += line;
|
||||
m_buffer += "\n" + line; // add line with newline symbol
|
||||
|
||||
m_content = reinterpret_cast<const lexer_char_t*>(m_buffer.c_str());
|
||||
m_start = m_content;
|
||||
@ -4641,6 +4656,9 @@ basic_json_parser_59:
|
||||
return result;
|
||||
}
|
||||
|
||||
// no comma is expected here
|
||||
unexpect(lexer::token_type::value_separator);
|
||||
|
||||
// otherwise: parse key-value pairs
|
||||
do
|
||||
{
|
||||
@ -4707,6 +4725,9 @@ basic_json_parser_59:
|
||||
return result;
|
||||
}
|
||||
|
||||
// no comma is expected here
|
||||
unexpect(lexer::token_type::value_separator);
|
||||
|
||||
// otherwise: parse values
|
||||
do
|
||||
{
|
||||
@ -4796,11 +4817,8 @@ basic_json_parser_59:
|
||||
|
||||
default:
|
||||
{
|
||||
std::string error_msg = "parse error - unexpected \'";
|
||||
error_msg += m_lexer.get_token();
|
||||
error_msg += "\' (";
|
||||
error_msg += lexer::token_type_name(last_token) + ")";
|
||||
throw std::invalid_argument(error_msg);
|
||||
// the last token was unexpected
|
||||
unexpect(last_token);
|
||||
}
|
||||
}
|
||||
|
||||
@ -4830,6 +4848,18 @@ basic_json_parser_59:
|
||||
}
|
||||
}
|
||||
|
||||
inline void unexpect(typename lexer::token_type t) const
|
||||
{
|
||||
if (t == last_token)
|
||||
{
|
||||
std::string error_msg = "parse error - unexpected \'";
|
||||
error_msg += m_lexer.get_token();
|
||||
error_msg += "\' (";
|
||||
error_msg += lexer::token_type_name(last_token) + ")";
|
||||
throw std::invalid_argument(error_msg);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
/// levels of recursion
|
||||
int depth = 0;
|
||||
|
@ -3669,7 +3669,7 @@ class basic_json
|
||||
// string
|
||||
quotation_mark = [\"];
|
||||
escape = [\\];
|
||||
unescaped = [^\"\\\000];
|
||||
unescaped = [^\"\\\000\t\n\r];
|
||||
single_escaped = [\"\\/bfnrt];
|
||||
unicode_escaped = [u][0-9a-fA-F]{4};
|
||||
escaped = escape (single_escaped | unicode_escaped);
|
||||
@ -3701,7 +3701,7 @@ class basic_json
|
||||
m_buffer.erase(0, static_cast<size_t>(offset_start));
|
||||
std::string line;
|
||||
std::getline(*m_stream, line);
|
||||
m_buffer += line;
|
||||
m_buffer += "\n" + line; // add line with newline symbol
|
||||
|
||||
m_content = reinterpret_cast<const lexer_char_t*>(m_buffer.c_str());
|
||||
m_start = m_content;
|
||||
@ -3947,6 +3947,9 @@ class basic_json
|
||||
return result;
|
||||
}
|
||||
|
||||
// no comma is expected here
|
||||
unexpect(lexer::token_type::value_separator);
|
||||
|
||||
// otherwise: parse key-value pairs
|
||||
do
|
||||
{
|
||||
@ -4013,6 +4016,9 @@ class basic_json
|
||||
return result;
|
||||
}
|
||||
|
||||
// no comma is expected here
|
||||
unexpect(lexer::token_type::value_separator);
|
||||
|
||||
// otherwise: parse values
|
||||
do
|
||||
{
|
||||
@ -4102,11 +4108,8 @@ class basic_json
|
||||
|
||||
default:
|
||||
{
|
||||
std::string error_msg = "parse error - unexpected \'";
|
||||
error_msg += m_lexer.get_token();
|
||||
error_msg += "\' (";
|
||||
error_msg += lexer::token_type_name(last_token) + ")";
|
||||
throw std::invalid_argument(error_msg);
|
||||
// the last token was unexpected
|
||||
unexpect(last_token);
|
||||
}
|
||||
}
|
||||
|
||||
@ -4136,6 +4139,18 @@ class basic_json
|
||||
}
|
||||
}
|
||||
|
||||
inline void unexpect(typename lexer::token_type t) const
|
||||
{
|
||||
if (t == last_token)
|
||||
{
|
||||
std::string error_msg = "parse error - unexpected \'";
|
||||
error_msg += m_lexer.get_token();
|
||||
error_msg += "\' (";
|
||||
error_msg += lexer::token_type_name(last_token) + ")";
|
||||
throw std::invalid_argument(error_msg);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
/// levels of recursion
|
||||
int depth = 0;
|
||||
|
1
test/json_tests/fail1.json
Normal file
1
test/json_tests/fail1.json
Normal file
@ -0,0 +1 @@
|
||||
"A JSON payload should be an object or array, not a string."
|
1
test/json_tests/fail10.json
Normal file
1
test/json_tests/fail10.json
Normal file
@ -0,0 +1 @@
|
||||
{"Extra value after close": true} "misplaced quoted value"
|
1
test/json_tests/fail11.json
Normal file
1
test/json_tests/fail11.json
Normal file
@ -0,0 +1 @@
|
||||
{"Illegal expression": 1 + 2}
|
1
test/json_tests/fail12.json
Normal file
1
test/json_tests/fail12.json
Normal file
@ -0,0 +1 @@
|
||||
{"Illegal invocation": alert()}
|
1
test/json_tests/fail13.json
Normal file
1
test/json_tests/fail13.json
Normal file
@ -0,0 +1 @@
|
||||
{"Numbers cannot have leading zeroes": 013}
|
1
test/json_tests/fail14.json
Normal file
1
test/json_tests/fail14.json
Normal file
@ -0,0 +1 @@
|
||||
{"Numbers cannot be hex": 0x14}
|
1
test/json_tests/fail15.json
Normal file
1
test/json_tests/fail15.json
Normal file
@ -0,0 +1 @@
|
||||
["Illegal backslash escape: \x15"]
|
1
test/json_tests/fail16.json
Normal file
1
test/json_tests/fail16.json
Normal file
@ -0,0 +1 @@
|
||||
[\naked]
|
1
test/json_tests/fail17.json
Normal file
1
test/json_tests/fail17.json
Normal file
@ -0,0 +1 @@
|
||||
["Illegal backslash escape: \017"]
|
1
test/json_tests/fail18.json
Normal file
1
test/json_tests/fail18.json
Normal file
@ -0,0 +1 @@
|
||||
[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]
|
1
test/json_tests/fail19.json
Normal file
1
test/json_tests/fail19.json
Normal file
@ -0,0 +1 @@
|
||||
{"Missing colon" null}
|
1
test/json_tests/fail2.json
Normal file
1
test/json_tests/fail2.json
Normal file
@ -0,0 +1 @@
|
||||
["Unclosed array"
|
1
test/json_tests/fail20.json
Normal file
1
test/json_tests/fail20.json
Normal file
@ -0,0 +1 @@
|
||||
{"Double colon":: null}
|
1
test/json_tests/fail21.json
Normal file
1
test/json_tests/fail21.json
Normal file
@ -0,0 +1 @@
|
||||
{"Comma instead of colon", null}
|
1
test/json_tests/fail22.json
Normal file
1
test/json_tests/fail22.json
Normal file
@ -0,0 +1 @@
|
||||
["Colon instead of comma": false]
|
1
test/json_tests/fail23.json
Normal file
1
test/json_tests/fail23.json
Normal file
@ -0,0 +1 @@
|
||||
["Bad value", truth]
|
1
test/json_tests/fail24.json
Normal file
1
test/json_tests/fail24.json
Normal file
@ -0,0 +1 @@
|
||||
['single quote']
|
1
test/json_tests/fail25.json
Normal file
1
test/json_tests/fail25.json
Normal file
@ -0,0 +1 @@
|
||||
[" tab character in string "]
|
1
test/json_tests/fail26.json
Normal file
1
test/json_tests/fail26.json
Normal file
@ -0,0 +1 @@
|
||||
["tab\ character\ in\ string\ "]
|
2
test/json_tests/fail27.json
Normal file
2
test/json_tests/fail27.json
Normal file
@ -0,0 +1,2 @@
|
||||
["line
|
||||
break"]
|
2
test/json_tests/fail28.json
Normal file
2
test/json_tests/fail28.json
Normal file
@ -0,0 +1,2 @@
|
||||
["line\
|
||||
break"]
|
1
test/json_tests/fail29.json
Normal file
1
test/json_tests/fail29.json
Normal file
@ -0,0 +1 @@
|
||||
[0e]
|
1
test/json_tests/fail3.json
Normal file
1
test/json_tests/fail3.json
Normal file
@ -0,0 +1 @@
|
||||
{unquoted_key: "keys must be quoted"}
|
1
test/json_tests/fail30.json
Normal file
1
test/json_tests/fail30.json
Normal file
@ -0,0 +1 @@
|
||||
[0e+]
|
1
test/json_tests/fail31.json
Normal file
1
test/json_tests/fail31.json
Normal file
@ -0,0 +1 @@
|
||||
[0e+-1]
|
1
test/json_tests/fail32.json
Normal file
1
test/json_tests/fail32.json
Normal file
@ -0,0 +1 @@
|
||||
{"Comma instead if closing brace": true,
|
1
test/json_tests/fail33.json
Normal file
1
test/json_tests/fail33.json
Normal file
@ -0,0 +1 @@
|
||||
["mismatch"}
|
1
test/json_tests/fail4.json
Normal file
1
test/json_tests/fail4.json
Normal file
@ -0,0 +1 @@
|
||||
["extra comma",]
|
1
test/json_tests/fail5.json
Normal file
1
test/json_tests/fail5.json
Normal file
@ -0,0 +1 @@
|
||||
["double extra comma",,]
|
1
test/json_tests/fail6.json
Normal file
1
test/json_tests/fail6.json
Normal file
@ -0,0 +1 @@
|
||||
[ , "<-- missing value"]
|
1
test/json_tests/fail7.json
Normal file
1
test/json_tests/fail7.json
Normal file
@ -0,0 +1 @@
|
||||
["Comma after the close"],
|
1
test/json_tests/fail8.json
Normal file
1
test/json_tests/fail8.json
Normal file
@ -0,0 +1 @@
|
||||
["Extra close"]]
|
1
test/json_tests/fail9.json
Normal file
1
test/json_tests/fail9.json
Normal file
@ -0,0 +1 @@
|
||||
{"Extra comma": true,}
|
58
test/json_tests/pass1.json
Normal file
58
test/json_tests/pass1.json
Normal file
@ -0,0 +1,58 @@
|
||||
[
|
||||
"JSON Test Pattern pass1",
|
||||
{"object with 1 member":["array with 1 element"]},
|
||||
{},
|
||||
[],
|
||||
-42,
|
||||
true,
|
||||
false,
|
||||
null,
|
||||
{
|
||||
"integer": 1234567890,
|
||||
"real": -9876.543210,
|
||||
"e": 0.123456789e-12,
|
||||
"E": 1.234567890E+34,
|
||||
"": 23456789012E66,
|
||||
"zero": 0,
|
||||
"one": 1,
|
||||
"space": " ",
|
||||
"quote": "\"",
|
||||
"backslash": "\\",
|
||||
"controls": "\b\f\n\r\t",
|
||||
"slash": "/ & \/",
|
||||
"alpha": "abcdefghijklmnopqrstuvwyz",
|
||||
"ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
|
||||
"digit": "0123456789",
|
||||
"0123456789": "digit",
|
||||
"special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?",
|
||||
"hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
|
||||
"true": true,
|
||||
"false": false,
|
||||
"null": null,
|
||||
"array":[ ],
|
||||
"object":{ },
|
||||
"address": "50 St. James Street",
|
||||
"url": "http://www.JSON.org/",
|
||||
"comment": "// /* <!-- --",
|
||||
"# -- --> */": " ",
|
||||
" s p a c e d " :[1,2 , 3
|
||||
|
||||
,
|
||||
|
||||
4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7],
|
||||
"jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}",
|
||||
"quotes": "" \u0022 %22 0x22 034 "",
|
||||
"\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"
|
||||
: "A key can be any string"
|
||||
},
|
||||
0.5 ,98.6
|
||||
,
|
||||
99.44
|
||||
,
|
||||
|
||||
1066,
|
||||
1e1,
|
||||
0.1e1,
|
||||
1e-1,
|
||||
1e00,2e+00,2e-00
|
||||
,"rosebud"]
|
1
test/json_tests/pass2.json
Normal file
1
test/json_tests/pass2.json
Normal file
@ -0,0 +1 @@
|
||||
[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]
|
6
test/json_tests/pass3.json
Normal file
6
test/json_tests/pass3.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"JSON Test Pattern pass3": {
|
||||
"The outermost value": "must be an object or array.",
|
||||
"In this test": "It is an object."
|
||||
}
|
||||
}
|
@ -7398,16 +7398,17 @@ TEST_CASE("parser class")
|
||||
|
||||
// exotic test cases for full coverage
|
||||
{
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "\"\\u000\n1\"";
|
||||
CHECK(json::parser(ss).parse().get<json::string_t>() == "\x01");
|
||||
}
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "\"\\u00\n01\"";
|
||||
CHECK(json::parser(ss).parse().get<json::string_t>() == "\x01");
|
||||
}
|
||||
// that one got illegal
|
||||
//{
|
||||
// std::stringstream ss;
|
||||
// ss << "\"\\u000\n1\"";
|
||||
// CHECK(json::parser(ss).parse().get<json::string_t>() == "\x01");
|
||||
//}
|
||||
//{
|
||||
// std::stringstream ss;
|
||||
// ss << "\"\\u00\n01\"";
|
||||
// CHECK(json::parser(ss).parse().get<json::string_t>() == "\x01");
|
||||
//}
|
||||
}
|
||||
|
||||
CHECK(json::parser("\"\\u0001\"").parse().get<json::string_t>() == "\x01");
|
||||
@ -8439,6 +8440,73 @@ TEST_CASE("concepts")
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("JSON compliance")
|
||||
{
|
||||
// test cases are from http://json.org/JSON_checker/
|
||||
|
||||
SECTION("expected failures")
|
||||
{
|
||||
for (auto filename :
|
||||
{
|
||||
//"test/json_tests/fail1.json",
|
||||
"test/json_tests/fail2.json",
|
||||
"test/json_tests/fail3.json",
|
||||
"test/json_tests/fail4.json",
|
||||
"test/json_tests/fail5.json",
|
||||
"test/json_tests/fail6.json",
|
||||
"test/json_tests/fail7.json",
|
||||
"test/json_tests/fail8.json",
|
||||
"test/json_tests/fail9.json",
|
||||
"test/json_tests/fail10.json",
|
||||
"test/json_tests/fail11.json",
|
||||
"test/json_tests/fail12.json",
|
||||
"test/json_tests/fail13.json",
|
||||
"test/json_tests/fail14.json",
|
||||
"test/json_tests/fail15.json",
|
||||
"test/json_tests/fail16.json",
|
||||
"test/json_tests/fail17.json",
|
||||
//"test/json_tests/fail18.json",
|
||||
"test/json_tests/fail19.json",
|
||||
"test/json_tests/fail20.json",
|
||||
"test/json_tests/fail21.json",
|
||||
"test/json_tests/fail22.json",
|
||||
"test/json_tests/fail23.json",
|
||||
"test/json_tests/fail24.json",
|
||||
"test/json_tests/fail25.json",
|
||||
"test/json_tests/fail26.json",
|
||||
"test/json_tests/fail27.json",
|
||||
"test/json_tests/fail28.json",
|
||||
"test/json_tests/fail29.json",
|
||||
"test/json_tests/fail30.json",
|
||||
"test/json_tests/fail31.json",
|
||||
"test/json_tests/fail32.json",
|
||||
"test/json_tests/fail33.json"
|
||||
})
|
||||
{
|
||||
CAPTURE(filename);
|
||||
json j;
|
||||
std::ifstream f(filename);
|
||||
CHECK_THROWS_AS(j << f, std::invalid_argument);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("expected passes")
|
||||
{
|
||||
for (auto filename :
|
||||
{
|
||||
"test/json_tests/pass1.json",
|
||||
"test/json_tests/pass2.json",
|
||||
"test/json_tests/pass3.json"
|
||||
})
|
||||
{
|
||||
CAPTURE(filename);
|
||||
json j;
|
||||
std::ifstream f(filename);
|
||||
CHECK_NOTHROW(j << f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("regression tests")
|
||||
{
|
||||
SECTION("issue #60 - Double quotation mark is not parsed correctly")
|
||||
|
Loading…
Reference in New Issue
Block a user