Skip to content

Commit

Permalink
Add the json-checker
Browse files Browse the repository at this point in the history
  • Loading branch information
yaomer committed Nov 12, 2021
1 parent da512e3 commit 1225718
Show file tree
Hide file tree
Showing 39 changed files with 155 additions and 6 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

然后需要使用`c++17`来编译。

```
$ clang++ -std=c++17 json-check.cc
```

[json-checker](https://www.json.org/JSON_checker/)用于测试parse是否能正常工作。

---
假如我们有以下`json`文件,就叫做`m.json`吧。
```
{
Expand Down
36 changes: 36 additions & 0 deletions json-check.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "json.h"

#include <iostream>

json::value jv;

bool check(const std::string& filename)
{
if (!jv.parsefile(filename)) {
std::cout << "check failed: " << filename.c_str() << ": " << jv.as_string().c_str() << "\n";
return false;
}
std::cout << "check pass " << filename.c_str() << "\n";
if (filename.rfind("fail1.json") != std::string::npos) {
std::cout << "(Now it can be any JSON value)\n";
} else if (filename.rfind("fail18.json") != std::string::npos) {
std::cout << "(We have no maximum nesting depth limit)\n";
}
return true;
}

int main()
{
char buf[32];
std::string check_dir = "json-checker/";
std::cout << "============CHECK fail1.json ~ fail33.json============\n";
for (int i = 1; i <= 33; i++) {
sprintf(buf, "fail%d.json", i);
check(check_dir + buf);
}
std::cout << "============CHECK pass1.json ~ pass3.json============\n";
for (int i = 1; i <= 3; i++) {
sprintf(buf, "pass%d.json", i);
check(check_dir + buf);
}
}
1 change: 1 addition & 0 deletions json-checker/fail1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"A JSON payload should be an object or array, not a string."
1 change: 1 addition & 0 deletions json-checker/fail10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Extra value after close": true} "misplaced quoted value"
1 change: 1 addition & 0 deletions json-checker/fail11.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Illegal expression": 1 + 2}
1 change: 1 addition & 0 deletions json-checker/fail12.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Illegal invocation": alert()}
1 change: 1 addition & 0 deletions json-checker/fail13.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Numbers cannot have leading zeroes": 013}
1 change: 1 addition & 0 deletions json-checker/fail14.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Numbers cannot be hex": 0x14}
1 change: 1 addition & 0 deletions json-checker/fail15.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["Illegal backslash escape: \x15"]
1 change: 1 addition & 0 deletions json-checker/fail16.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[\naked]
1 change: 1 addition & 0 deletions json-checker/fail17.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["Illegal backslash escape: \017"]
1 change: 1 addition & 0 deletions json-checker/fail18.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]
1 change: 1 addition & 0 deletions json-checker/fail19.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Missing colon" null}
1 change: 1 addition & 0 deletions json-checker/fail2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["Unclosed array"
1 change: 1 addition & 0 deletions json-checker/fail20.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Double colon":: null}
1 change: 1 addition & 0 deletions json-checker/fail21.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Comma instead of colon", null}
1 change: 1 addition & 0 deletions json-checker/fail22.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["Colon instead of comma": false]
1 change: 1 addition & 0 deletions json-checker/fail23.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["Bad value", truth]
1 change: 1 addition & 0 deletions json-checker/fail24.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
['single quote']
1 change: 1 addition & 0 deletions json-checker/fail25.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[" tab character in string "]
1 change: 1 addition & 0 deletions json-checker/fail26.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["tab\ character\ in\ string\ "]
2 changes: 2 additions & 0 deletions json-checker/fail27.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
["line
break"]
2 changes: 2 additions & 0 deletions json-checker/fail28.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
["line\
break"]
1 change: 1 addition & 0 deletions json-checker/fail29.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0e]
1 change: 1 addition & 0 deletions json-checker/fail3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{unquoted_key: "keys must be quoted"}
1 change: 1 addition & 0 deletions json-checker/fail30.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0e+]
1 change: 1 addition & 0 deletions json-checker/fail31.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0e+-1]
1 change: 1 addition & 0 deletions json-checker/fail32.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Comma instead if closing brace": true,
1 change: 1 addition & 0 deletions json-checker/fail33.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["mismatch"}
1 change: 1 addition & 0 deletions json-checker/fail4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["extra comma",]
1 change: 1 addition & 0 deletions json-checker/fail5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["double extra comma",,]
1 change: 1 addition & 0 deletions json-checker/fail6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ , "<-- missing value"]
1 change: 1 addition & 0 deletions json-checker/fail7.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["Comma after the close"],
1 change: 1 addition & 0 deletions json-checker/fail8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["Extra close"]]
1 change: 1 addition & 0 deletions json-checker/fail9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Extra comma": true,}
58 changes: 58 additions & 0 deletions json-checker/pass1.json
Original file line number Diff line number Diff line change
@@ -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": "&#34; \u0022 %22 0x22 034 &#x22;",
"\/\\\"\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 change: 1 addition & 0 deletions json-checker/pass2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]
6 changes: 6 additions & 0 deletions json-checker/pass3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"JSON Test Pattern pass3": {
"The outermost value": "must be an object or array.",
"In this test": "It is an object."
}
}
18 changes: 12 additions & 6 deletions json.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include <variant>
#include <fstream>

#include <iostream>

namespace json {

enum ValueType {
Expand Down Expand Up @@ -265,6 +263,8 @@ enum error_code {
invalid_value_type,
invalid_object,
invalid_array,
invalid_string_tab,
invalid_string_break,
invalid_escape,
invalid_unicode,
invalid_unicode_surrogate,
Expand Down Expand Up @@ -342,9 +342,11 @@ class parser {
object parse_object()
{
object o;
skipspace();
if (c == '}') return o;
backward();
while (true) {
skipspace();
if (c == '}') break;
if (c != '"') throw invalid_object;
auto key = parse_string();
skipspace();
Expand Down Expand Up @@ -395,6 +397,8 @@ class parser {
switch (nextchar()) {
case '\\': parse_escape(s); break;
case '\"': return s;
case '\t': throw invalid_string_tab;
case '\n': throw invalid_string_break;
default: s.push_back(c); break;
}
}
Expand Down Expand Up @@ -559,6 +563,8 @@ const char *parser::get_error_string(error_code code)
"invalid_value_type",
"invalid_object",
"invalid_array",
"invalid_string_tab",
"invalid_string_break",
"invalid_escape",
"invalid_unicode",
"invalid_unicode_surrogate",
Expand Down Expand Up @@ -708,15 +714,15 @@ class writer {
// 1 bit is treated as an ASCII character,
// in which case UTF-8 is compatible with ASCII.
buf.push_back(c);
} else if (!(c & 0x20)) {
} else if ((c & 0xE0) == 0xC0) { // 110xxxxx
if (i + 1 >= s.size()) throw invalid_unicode;
char c2 = s[++i];
// xxx|xx xx|xxxx
put_unicode(0,
(c & 0x1C) >> 2,
((c & 0x03) << 2) | ((c2 & 0x30) >> 4),
(c2 & 0x0F));
} else if (!(c & 0x10)) {
} else if ((c & 0xF0) == 0xE0) { // 1110xxxx
if (i + 2 >= s.size()) throw invalid_unicode;
char c2 = s[++i], c3 = s[++i];
// xxxx |xxxx|xx xx|xxxx
Expand All @@ -725,7 +731,7 @@ class writer {
((c2 & 0x03) << 2) | ((c3 & 0x30) >> 4),
(c3 & 0x0F));
} else { // SURROGATE-PAIR
assert(!(c & 0x08));
assert((c & 0xF8) == 0xF0); // 11110xxx
if (i + 3 >= s.size()) throw invalid_unicode;
char c2 = s[++i], c3 = s[++i], c4 = s[++i];
unsigned u = 0;
Expand Down

0 comments on commit 1225718

Please sign in to comment.