Skip to main content

json_streaming/blocking/
mod.rs

1
2pub(crate) mod json_writer;
3pub(crate) mod object;
4pub(crate) mod array;
5pub(crate) mod read;
6pub (crate) mod io;
7
8#[allow(unused_imports)]
9pub use array::*;
10#[allow(unused_imports)]
11pub use io::*;
12#[allow(unused_imports)]
13pub use json_writer::*;
14#[allow(unused_imports)]
15pub use object::*;
16
17#[allow(unused_imports)]
18pub use read::*;
19
20
21#[cfg(test)]
22mod tests {
23    use super::*;
24    use std::io;
25    use crate::shared::*;
26
27    fn do_write_json<F: JsonFormatter>(o: &mut JsonObject<Vec<u8>, F, DefaultFloatFormat>) -> io::Result<()> {
28        o.write_string_value("abc", "yo")?;
29        o.write_string_value("xyz", "yo")?;
30
31        {
32            let mut na = o.start_array("aaaa")?;
33            na.write_string_value("111")?;
34            na.write_string_value("11")?;
35            na.start_object()?.end()?;
36            na.start_array()?.end()?;
37
38            na.write_null_value()?;
39            na.write_bool_value(true)?;
40            na.write_bool_value(false)?;
41            na.write_i32_value(-23987)?;
42            na.write_u128_value(23987u128)?;
43            na.write_f64_value(23.235)?;
44            na.write_f64_value(f64::INFINITY)?;
45            na.write_f64_value(f64::NAN)?;
46            na.write_f32_value(23.235)?;
47            na.write_f32_value(f32::INFINITY)?;
48            na.write_f32_value(f32::NAN)?;
49        }
50
51        {
52            let mut nested = o.start_object("ooo")?;
53            nested.write_string_value("lll", "whatever")?;
54            nested.start_array("ar")?;
55        }
56
57        Ok(())
58    }
59
60    fn do_test_combined<F: JsonFormatter>(mut writer: JsonWriter<Vec<u8>, F, DefaultFloatFormat>, expected: &str) -> io::Result<()> {
61        do_write_json(&mut JsonObject::new(&mut writer)?)?;
62
63        let s = writer.into_inner()?.to_vec();
64        let s = String::from_utf8(s).unwrap();
65
66        assert_eq!(s, expected);
67        Ok(())
68    }
69
70    #[test]
71    fn test_write_combined_compact() -> io::Result<()> {
72        let mut buf = Vec::new();
73        do_test_combined(JsonWriter::new_compact(&mut buf),
74            r#"{"abc":"yo","xyz":"yo","aaaa":["111","11",{},[],null,true,false,-23987,23987,23.235,null,null,23.235,null,null],"ooo":{"lll":"whatever","ar":[]}}"#
75        )
76    }
77
78    #[test]
79    fn test_write_combined_pretty() -> io::Result<()> {
80        let mut buf = Vec::new();
81        do_test_combined(JsonWriter::new_pretty(&mut buf),
82            r#"{
83  "abc": "yo",
84  "xyz": "yo",
85  "aaaa": [
86    "111",
87    "11",
88    {},
89    [],
90    null,
91    true,
92    false,
93    -23987,
94    23987,
95    23.235,
96    null,
97    null,
98    23.235,
99    null,
100    null
101  ],
102  "ooo": {
103    "lll": "whatever",
104    "ar": []
105  }
106}"#
107        )
108    }
109}