1#![cfg(not(feature = "preserve_order"))]
2#![allow(clippy::assertions_on_result_states)]
3
4use serde_json::{json, Deserializer, Value};
5
6// Rustfmt issue https://github.com/rust-lang-nursery/rustfmt/issues/2740
7#[rustfmt::skip]
8macro_rules! test_stream {
9 ($data:expr, $ty:ty, |$stream:ident| $test:block) => {
10 {
11 let de = Deserializer::from_str($data);
12 let mut $stream = de.into_iter::<$ty>();
13 assert_eq!($stream.byte_offset(), 0);
14 $test
15 }
16 {
17 let de = Deserializer::from_slice($data.as_bytes());
18 let mut $stream = de.into_iter::<$ty>();
19 assert_eq!($stream.byte_offset(), 0);
20 $test
21 }
22 {
23 let mut bytes = $data.as_bytes();
24 let de = Deserializer::from_reader(&mut bytes);
25 let mut $stream = de.into_iter::<$ty>();
26 assert_eq!($stream.byte_offset(), 0);
27 $test
28 }
29 };
30}
31
32#[test]
33fn test_json_stream_newlines() {
34 let data = "{\"x\":39} {\"x\":40}{\"x\":41}\n{\"x\":42}";
35
36 test_stream!(data, Value, |stream| {
37 assert_eq!(stream.next().unwrap().unwrap()["x"], 39);
38 assert_eq!(stream.byte_offset(), 8);
39
40 assert_eq!(stream.next().unwrap().unwrap()["x"], 40);
41 assert_eq!(stream.byte_offset(), 17);
42
43 assert_eq!(stream.next().unwrap().unwrap()["x"], 41);
44 assert_eq!(stream.byte_offset(), 25);
45
46 assert_eq!(stream.next().unwrap().unwrap()["x"], 42);
47 assert_eq!(stream.byte_offset(), 34);
48
49 assert!(stream.next().is_none());
50 assert_eq!(stream.byte_offset(), 34);
51 });
52}
53
54#[test]
55fn test_json_stream_trailing_whitespaces() {
56 let data = "{\"x\":42} \t\n";
57
58 test_stream!(data, Value, |stream| {
59 assert_eq!(stream.next().unwrap().unwrap()["x"], 42);
60 assert_eq!(stream.byte_offset(), 8);
61
62 assert!(stream.next().is_none());
63 assert_eq!(stream.byte_offset(), 11);
64 });
65}
66
67#[test]
68fn test_json_stream_truncated() {
69 let data = "{\"x\":40}\n{\"x\":";
70
71 test_stream!(data, Value, |stream| {
72 assert_eq!(stream.next().unwrap().unwrap()["x"], 40);
73 assert_eq!(stream.byte_offset(), 8);
74
75 assert!(stream.next().unwrap().unwrap_err().is_eof());
76 assert_eq!(stream.byte_offset(), 9);
77 });
78}
79
80#[test]
81fn test_json_stream_truncated_decimal() {
82 let data = "{\"x\":4.";
83
84 test_stream!(data, Value, |stream| {
85 assert!(stream.next().unwrap().unwrap_err().is_eof());
86 assert_eq!(stream.byte_offset(), 0);
87 });
88}
89
90#[test]
91fn test_json_stream_truncated_negative() {
92 let data = "{\"x\":-";
93
94 test_stream!(data, Value, |stream| {
95 assert!(stream.next().unwrap().unwrap_err().is_eof());
96 assert_eq!(stream.byte_offset(), 0);
97 });
98}
99
100#[test]
101fn test_json_stream_truncated_exponent() {
102 let data = "{\"x\":4e";
103
104 test_stream!(data, Value, |stream| {
105 assert!(stream.next().unwrap().unwrap_err().is_eof());
106 assert_eq!(stream.byte_offset(), 0);
107 });
108}
109
110#[test]
111fn test_json_stream_empty() {
112 let data = "";
113
114 test_stream!(data, Value, |stream| {
115 assert!(stream.next().is_none());
116 assert_eq!(stream.byte_offset(), 0);
117 });
118}
119
120#[test]
121fn test_json_stream_primitive() {
122 let data = "{} true{}1[]\nfalse\"hey\"2 ";
123
124 test_stream!(data, Value, |stream| {
125 assert_eq!(stream.next().unwrap().unwrap(), json!({}));
126 assert_eq!(stream.byte_offset(), 2);
127
128 assert_eq!(stream.next().unwrap().unwrap(), true);
129 assert_eq!(stream.byte_offset(), 7);
130
131 assert_eq!(stream.next().unwrap().unwrap(), json!({}));
132 assert_eq!(stream.byte_offset(), 9);
133
134 assert_eq!(stream.next().unwrap().unwrap(), 1);
135 assert_eq!(stream.byte_offset(), 10);
136
137 assert_eq!(stream.next().unwrap().unwrap(), json!([]));
138 assert_eq!(stream.byte_offset(), 12);
139
140 assert_eq!(stream.next().unwrap().unwrap(), false);
141 assert_eq!(stream.byte_offset(), 18);
142
143 assert_eq!(stream.next().unwrap().unwrap(), "hey");
144 assert_eq!(stream.byte_offset(), 23);
145
146 assert_eq!(stream.next().unwrap().unwrap(), 2);
147 assert_eq!(stream.byte_offset(), 24);
148
149 assert!(stream.next().is_none());
150 assert_eq!(stream.byte_offset(), 25);
151 });
152}
153
154#[test]
155fn test_json_stream_invalid_literal() {
156 let data = "truefalse";
157
158 test_stream!(data, Value, |stream| {
159 let second = stream.next().unwrap().unwrap_err();
160 assert_eq!(second.to_string(), "trailing characters at line 1 column 5");
161 });
162}
163
164#[test]
165fn test_json_stream_invalid_number() {
166 let data = "1true";
167
168 test_stream!(data, Value, |stream| {
169 let second = stream.next().unwrap().unwrap_err();
170 assert_eq!(second.to_string(), "trailing characters at line 1 column 2");
171 });
172}
173
174#[test]
175fn test_error() {
176 let data = "true wrong false";
177
178 test_stream!(data, Value, |stream| {
179 assert_eq!(stream.next().unwrap().unwrap(), true);
180 assert!(stream.next().unwrap().is_err());
181 assert!(stream.next().is_none());
182 });
183}
184