1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
use json_event_parser::{JsonEvent, SliceJsonParser, WriterJsonSerializer};
#[test]
fn test_recovery() {
let entries = [
(b"[nonono]".as_slice(), "[]"),
(b"[a]", "[]"),
(b"[1,]", "[1]"),
(b"{\"foo\":1,}", "{\"foo\":1}"),
(b"{\"foo\" 1}", "{\"foo\":1}"),
(b"[1 2]", "[1,2]"),
(b"[\"\x00\"]", "[]"),
(b"[\"\\uD888\\u1234\"]", "[]"),
];
for (input, expected_output) in entries {
let mut reader = SliceJsonParser::new(input);
let mut writer = WriterJsonSerializer::new(Vec::new());
loop {
match reader.parse_next() {
Ok(JsonEvent::Eof) => break,
Ok(event) => writer.serialize_event(event).unwrap(),
Err(_) => (),
}
}
let actual_output = String::from_utf8(writer.finish().unwrap()).unwrap();
assert_eq!(
expected_output,
actual_output,
"on {}",
String::from_utf8_lossy(input)
);
}
}
#[test]
fn test_error_messages() {
let entries = [
(
b"".as_slice(),
"Parser error at line 1 column 1: Unexpected end of file, a value was expected",
),
(
b"\n}",
"Parser error at line 2 column 1: Unexpected closing curly bracket, no array to close",
),
(
b"\r\n}",
"Parser error at line 2 column 1: Unexpected closing curly bracket, no array to close",
),
(
b"\"\n\"",
"Parser error at line 1 column 2: '\n' is not allowed in JSON strings",
),
(
b"\"\\uDCFF\\u0000\"",
"Parser error at line 1 between columns 2 and column 8: \\uDCFF is not a valid high surrogate",
)
];
for (json, error) in entries {
assert_eq!(
SliceJsonParser::new(json)
.parse_next()
.unwrap_err()
.to_string(),
error
);
}
}
|