json_streaming/nonblocking/
mod.rs1pub(crate) mod array;
2pub(crate) mod io;
3pub(crate) mod json_writer;
4pub(crate) mod object;
5pub(crate) mod read;
6
7#[cfg(not(test))]
8#[allow(unused_imports)]
9pub use array::*;
10#[allow(unused_imports)]
11pub use io::*;
12#[allow(unused_imports)]
13pub use json_writer::*;
14#[cfg(not(test))]
15#[allow(unused_imports)]
16pub use object::*;
17
18#[allow(unused_imports)]
19pub use read::*;
20
21
22#[cfg(test)]
23mod tests {
24 use crate::nonblocking::json_writer::JsonWriter;
25 use crate::nonblocking::object::JsonObject;
26 use crate::shared::*;
27 use std::io;
28
29 async fn do_write_json<F: JsonFormatter>(o: &mut JsonObject<'_, '_, Vec<u8>, F, DefaultFloatFormat>) -> io::Result<()> {
30 o.write_string_value("abc", "yo").await?;
31 o.write_string_value("xyz", "yo").await?;
32
33 let mut na = o.start_array("aaaa").await?;
34 na.write_string_value("111").await?;
35 na.write_string_value("11").await?;
36 na.start_object().await?.end().await?;
37 na.start_array().await?.end().await?;
38
39 na.write_null_value().await?;
40 na.write_bool_value(true).await?;
41 na.write_bool_value(false).await?;
42 na.write_i32_value(-23987).await?;
43 na.write_u128_value(23987u128).await?;
44 na.write_f64_value(23.235).await?;
45 na.write_f64_value(f64::INFINITY).await?;
46 na.write_f64_value(f64::NAN).await?;
47 na.write_f32_value(23.235).await?;
48 na.write_f32_value(f32::INFINITY).await?;
49 na.write_f32_value(f32::NAN).await?;
50 na.end().await?;
51
52 let mut nested = o.start_object("ooo").await?;
53 nested.write_string_value("lll", "whatever").await?;
54 nested.start_array("ar").await?.end().await?;
55 nested.end().await?;
56
57 Ok(())
58 }
59
60 async fn do_test_combined<F: JsonFormatter>(mut writer: JsonWriter<'_, Vec<u8>, F, DefaultFloatFormat>, expected: &str) -> io::Result<()> {
61 let mut root = JsonObject::new(&mut writer).await?;
62 do_write_json(&mut root).await?;
63 root.end().await?;
64
65 let s = writer.into_inner()?.to_vec();
66 let s = String::from_utf8(s).unwrap();
67
68 assert_eq!(s, expected);
69 Ok(())
70 }
71
72 #[tokio::test]
73 async fn test_write_combined_compact() -> io::Result<()> {
74 do_test_combined(JsonWriter::new_compact(&mut Vec::new()),
75 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":[]}}"#
76 ).await
77 }
78
79 #[tokio::test]
80 async fn test_write_combined_pretty() -> io::Result<()> {
81 do_test_combined(JsonWriter::new_pretty(&mut Vec::new()),
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 ).await
108 }
109}