forked from serde-rs/json
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an ArrayDeserializer to read a JSON array as a stream
This mimics the StreamDeserializer API and implements issue serde-rs#404.
- Loading branch information
Yorhel
committed
Mar 19, 2019
1 parent
69bfbfc
commit 80af0b5
Showing
2 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
#![cfg(not(feature = "preserve_order"))] | ||
|
||
extern crate serde; | ||
|
||
#[macro_use] | ||
extern crate serde_json; | ||
|
||
use serde_json::{Deserializer, Value}; | ||
|
||
// Rustfmt issue https://github.com/rust-lang-nursery/rustfmt/issues/2740 | ||
#[cfg_attr(rustfmt, rustfmt_skip)] | ||
macro_rules! test_stream { | ||
($data:expr, $ty:ty, |$stream:ident| $test:block) => { | ||
{ | ||
let de = Deserializer::from_str($data); | ||
let mut $stream = de.into_array_iter::<$ty>(); | ||
$test | ||
} | ||
{ | ||
let de = Deserializer::from_slice($data.as_bytes()); | ||
let mut $stream = de.into_array_iter::<$ty>(); | ||
$test | ||
} | ||
{ | ||
let mut bytes = $data.as_bytes(); | ||
let de = Deserializer::from_reader(&mut bytes); | ||
let mut $stream = de.into_array_iter::<$ty>(); | ||
$test | ||
} | ||
}; | ||
} | ||
|
||
#[test] | ||
fn test_json_array_empty() { | ||
let data = "[]"; | ||
|
||
test_stream!(data, Value, |stream| { | ||
assert!(stream.next().is_none()); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_json_array_whitespace() { | ||
let data = "\r [\n{\"x\":42}\t, {\"y\":43}\n] \t\n"; | ||
|
||
test_stream!(data, Value, |stream| { | ||
assert_eq!(stream.next().unwrap().unwrap()["x"], 42); | ||
|
||
assert_eq!(stream.next().unwrap().unwrap()["y"], 43); | ||
|
||
assert!(stream.next().is_none()); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_json_array_truncated() { | ||
let data = "[{\"x\":40},{\"x\":"; | ||
|
||
test_stream!(data, Value, |stream| { | ||
assert_eq!(stream.next().unwrap().unwrap()["x"], 40); | ||
|
||
assert!(stream.next().unwrap().unwrap_err().is_eof()); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_json_array_primitive() { | ||
let data = "[{}, true, 1, [], 1.0, \"hey\", null]"; | ||
|
||
test_stream!(data, Value, |stream| { | ||
assert_eq!(stream.next().unwrap().unwrap(), json!({})); | ||
|
||
assert_eq!(stream.next().unwrap().unwrap(), true); | ||
|
||
assert_eq!(stream.next().unwrap().unwrap(), 1); | ||
|
||
assert_eq!(stream.next().unwrap().unwrap(), json!([])); | ||
|
||
assert_eq!(stream.next().unwrap().unwrap(), 1.0); | ||
|
||
assert_eq!(stream.next().unwrap().unwrap(), "hey"); | ||
|
||
assert_eq!(stream.next().unwrap().unwrap(), Value::Null); | ||
|
||
assert!(stream.next().is_none()); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_json_array_tailing_data() { | ||
let data = "[]e"; | ||
|
||
test_stream!(data, Value, |stream| { | ||
let second = stream.next().unwrap().unwrap_err(); | ||
assert_eq!(second.to_string(), "trailing characters at line 1 column 3"); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_json_array_tailing_comma() { | ||
let data = "[true,]"; | ||
|
||
test_stream!(data, Value, |stream| { | ||
assert_eq!(stream.next().unwrap().unwrap(), true); | ||
|
||
let second = stream.next().unwrap().unwrap_err(); | ||
assert_eq!(second.to_string(), "trailing comma at line 1 column 7"); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_json_array_eof() { | ||
let data = ""; | ||
|
||
test_stream!(data, Value, |stream| { | ||
let second = stream.next().unwrap().unwrap_err(); | ||
assert_eq!(second.to_string(), "EOF while parsing a value at line 1 column 0"); | ||
}); | ||
} |