json_streaming/blocking/
mod.rs

1//! TODO move this to the structs
2//! When [JsonObject] or [JsonArray] instances go out of scope, they write their respective closing
3//!  brackets automatically (although they also have optional `end()` functions). This is
4//!  convenient, but it means there is no explicit function call for returning potential
5//!  `io::Error`s. These errors are stored internally so they don't get lost, and the [JsonWriter]
6//!  has a `flush()` function that returns such an error if it exists. The idiom is the same that
7//!  e.g. the standard library's `BufRead` uses.
8
9pub(crate) mod json_writer;
10pub(crate) mod object;
11pub(crate) mod array;
12pub(crate) mod read;
13pub (crate) mod io;
14
15#[allow(unused_imports)]
16pub use array::*;
17#[allow(unused_imports)]
18pub use io::*;
19#[allow(unused_imports)]
20pub use json_writer::*;
21#[allow(unused_imports)]
22pub use object::*;
23
24#[allow(unused_imports)]
25pub use read::*;
26
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31    use std::io;
32    use crate::shared::*;
33
34    fn do_write_json<F: JsonFormatter>(o: &mut JsonObject<Vec<u8>, F, DefaultFloatFormat>) -> io::Result<()> {
35        o.write_string_value("abc", "yo")?;
36        o.write_string_value("xyz", "yo")?;
37
38        {
39            let mut na = o.start_array("aaaa")?;
40            na.write_string_value("111")?;
41            na.write_string_value("11")?;
42            na.start_object()?.end()?;
43            na.start_array()?.end()?;
44
45            na.write_null_value()?;
46            na.write_bool_value(true)?;
47            na.write_bool_value(false)?;
48            na.write_i32_value(-23987)?;
49            na.write_u128_value(23987u128)?;
50            na.write_f64_value(23.235)?;
51            na.write_f64_value(f64::INFINITY)?;
52            na.write_f64_value(f64::NAN)?;
53            na.write_f32_value(23.235)?;
54            na.write_f32_value(f32::INFINITY)?;
55            na.write_f32_value(f32::NAN)?;
56        }
57
58        {
59            let mut nested = o.start_object("ooo")?;
60            nested.write_string_value("lll", "whatever")?;
61            nested.start_array("ar")?;
62        }
63
64        Ok(())
65    }
66
67    fn do_test_combined<F: JsonFormatter>(mut writer: JsonWriter<Vec<u8>, F, DefaultFloatFormat>, expected: &str) -> io::Result<()> {
68        do_write_json(&mut JsonObject::new(&mut writer)?)?;
69
70        let s = writer.into_inner()?.to_vec();
71        let s = String::from_utf8(s).unwrap();
72
73        assert_eq!(s, expected);
74        Ok(())
75    }
76
77    #[test]
78    fn test_write_combined_compact() -> io::Result<()> {
79        let mut buf = Vec::new();
80        do_test_combined(JsonWriter::new_compact(&mut buf),
81            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":[]}}"#
82        )
83    }
84
85    #[test]
86    fn test_write_combined_pretty() -> io::Result<()> {
87        let mut buf = Vec::new();
88        do_test_combined(JsonWriter::new_pretty(&mut buf),
89            r#"{
90  "abc": "yo",
91  "xyz": "yo",
92  "aaaa": [
93    "111",
94    "11",
95    {},
96    [],
97    null,
98    true,
99    false,
100    -23987,
101    23987,
102    23.235,
103    null,
104    null,
105    23.235,
106    null,
107    null
108  ],
109  "ooo": {
110    "lll": "whatever",
111    "ar": []
112  }
113}"#
114        )
115    }
116}