nop_json/
write_to_json.rs

1pub use nop_json_derive::*;
2use crate::debug_to_json::DebugToJson;
3use crate::value::Value;
4use crate::escape::escape;
5
6use std::{char, fmt, f32, f64, io};
7use std::collections::{HashMap, HashSet, BTreeMap, BTreeSet, LinkedList, VecDeque};
8use std::sync::{RwLock, Mutex, Arc};
9use std::rc::Rc;
10
11
12/// Trait that can be automatically derived for structs and enums.
13///
14/// Example:
15///
16/// ```
17/// use nop_json::WriteToJson;
18///
19/// #[derive(WriteToJson)]
20/// struct Point {x: i32, y: i32}
21///
22/// let point = Point {x: 1, y: 2};
23/// point.write_to_json(&mut std::io::stdout()).unwrap();
24/// ```
25pub trait WriteToJson<W: io::Write>
26{	fn write_to_json(&self, out: &mut W) -> io::Result<()>;
27}
28
29fn write_debug_to_json<W, T>(out: &mut W, value: &T) -> io::Result<()> where T: DebugToJson, W: io::Write
30{	struct Wrapper<'a, T: DebugToJson>
31	{	value: &'a T
32	}
33	impl<'a, T: DebugToJson> fmt::Display for Wrapper<'a, T>
34	{	fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result
35		{	DebugToJson::fmt(self.value, out)
36		}
37	}
38	let w = Wrapper {value};
39	write!(out, "{}", w)
40}
41
42impl<W: io::Write> WriteToJson<W> for ()     {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
43impl<W: io::Write> WriteToJson<W> for isize  {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
44impl<W: io::Write> WriteToJson<W> for i128   {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
45impl<W: io::Write> WriteToJson<W> for i64    {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
46impl<W: io::Write> WriteToJson<W> for i32    {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
47impl<W: io::Write> WriteToJson<W> for i16    {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
48impl<W: io::Write> WriteToJson<W> for i8     {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
49impl<W: io::Write> WriteToJson<W> for usize  {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
50impl<W: io::Write> WriteToJson<W> for u128   {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
51impl<W: io::Write> WriteToJson<W> for u64    {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
52impl<W: io::Write> WriteToJson<W> for u32    {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
53impl<W: io::Write> WriteToJson<W> for u16    {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
54impl<W: io::Write> WriteToJson<W> for u8     {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
55impl<W: io::Write> WriteToJson<W> for f64    {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
56impl<W: io::Write> WriteToJson<W> for f32    {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
57impl<W: io::Write> WriteToJson<W> for bool   {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
58impl<W: io::Write> WriteToJson<W> for char   {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
59impl<W: io::Write> WriteToJson<W> for String {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
60impl<W: io::Write> WriteToJson<W> for Value  {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
61
62impl<W: io::Write, T> WriteToJson<W> for Box<T> where T: WriteToJson<W>
63{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
64	{	let v: &T = &*self;
65		v.write_to_json(out)
66	}
67}
68
69impl<W: io::Write, T> WriteToJson<W> for RwLock<T> where T: WriteToJson<W>
70{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
71	{	match self.read()
72		{	Ok(v) => v.write_to_json(out),
73			Err(e) => Err(io::Error::new(io::ErrorKind::Other, e.to_string())),
74		}
75	}
76}
77
78impl<W: io::Write, T> WriteToJson<W> for Mutex<T> where T: WriteToJson<W>
79{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
80	{	match self.lock()
81		{	Ok(v) => v.write_to_json(out),
82			Err(e) => Err(io::Error::new(io::ErrorKind::Other, e.to_string())),
83		}
84	}
85}
86
87impl<W: io::Write, T> WriteToJson<W> for Rc<T> where T: WriteToJson<W>
88{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
89	{	let v: &T = &*self;
90		v.write_to_json(out)
91	}
92}
93
94impl<W: io::Write, T> WriteToJson<W> for Arc<T> where T: WriteToJson<W>
95{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
96	{	let v: &T = &*self;
97		v.write_to_json(out)
98	}
99}
100
101impl<W: io::Write, T> WriteToJson<W> for Option<T> where T: WriteToJson<W>
102{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
103	{	match *self
104		{	Some(ref v) => v.write_to_json(out),
105			None => write!(out, "null"),
106		}
107	}
108}
109
110impl<W: io::Write, T> WriteToJson<W> for Vec<T> where T: WriteToJson<W>
111{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
112	{	let mut c = '[';
113		for item in self
114		{	write!(out, "{}", c)?;
115			item.write_to_json(out)?;
116			c = ',';
117		}
118		if c == '['
119		{	write!(out, "[]")
120		}
121		else
122		{	write!(out, "]")
123		}
124	}
125}
126
127impl<W: io::Write, T> WriteToJson<W> for HashSet<T> where T: WriteToJson<W>
128{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
129	{	let mut c = '[';
130		for item in self
131		{	write!(out, "{}", c)?;
132			item.write_to_json(out)?;
133			c = ',';
134		}
135		if c == '['
136		{	write!(out, "[]")
137		}
138		else
139		{	write!(out, "]")
140		}
141	}
142}
143
144impl<W: io::Write, T> WriteToJson<W> for LinkedList<T> where T: WriteToJson<W>
145{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
146	{	let mut c = '[';
147		for item in self
148		{	write!(out, "{}", c)?;
149			item.write_to_json(out)?;
150			c = ',';
151		}
152		if c == '['
153		{	write!(out, "[]")
154		}
155		else
156		{	write!(out, "]")
157		}
158	}
159}
160
161impl<W: io::Write, T> WriteToJson<W> for VecDeque<T> where T: WriteToJson<W>
162{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
163	{	let mut c = '[';
164		for item in self
165		{	write!(out, "{}", c)?;
166			item.write_to_json(out)?;
167			c = ',';
168		}
169		if c == '['
170		{	write!(out, "[]")
171		}
172		else
173		{	write!(out, "]")
174		}
175	}
176}
177
178impl<W: io::Write, T> WriteToJson<W> for BTreeSet<T> where T: WriteToJson<W>
179{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
180	{	let mut c = '[';
181		for item in self
182		{	write!(out, "{}", c)?;
183			item.write_to_json(out)?;
184			c = ',';
185		}
186		if c == '['
187		{	write!(out, "[]")
188		}
189		else
190		{	write!(out, "]")
191		}
192	}
193}
194
195impl<W: io::Write, T> WriteToJson<W> for HashMap<String, T> where T: WriteToJson<W>
196{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
197	{	let mut c = '{';
198		for (key, item) in self
199		{	write!(out, "{}\"{}\":", c, escape(key))?;
200			item.write_to_json(out)?;
201			c = ',';
202		}
203		if c == '{'
204		{	write!(out, "{{}}")
205		}
206		else
207		{	write!(out, "}}")
208		}
209	}
210}
211
212impl<W: io::Write, T> WriteToJson<W> for BTreeMap<String, T> where T: WriteToJson<W>
213{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
214	{	let mut c = '{';
215		for (key, item) in self
216		{	write!(out, "{}\"{}\":", c, escape(key))?;
217			item.write_to_json(out)?;
218			c = ',';
219		}
220		if c == '{'
221		{	write!(out, "{{}}")
222		}
223		else
224		{	write!(out, "}}")
225		}
226	}
227}
228
229impl<W: io::Write, A, B> WriteToJson<W> for (A, B) where A: WriteToJson<W>, B: WriteToJson<W>
230{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
231	{	write!(out, "[")?;
232		self.0.write_to_json(out)?;
233		write!(out, ",")?;
234		self.1.write_to_json(out)?;
235		write!(out, "]")
236	}
237}
238
239impl<W: io::Write, A, B, C> WriteToJson<W> for (A, B, C) where A: WriteToJson<W>, B: WriteToJson<W>, C: WriteToJson<W>
240{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
241	{	write!(out, "[")?;
242		self.0.write_to_json(out)?;
243		write!(out, ",")?;
244		self.1.write_to_json(out)?;
245		write!(out, ",")?;
246		self.2.write_to_json(out)?;
247		write!(out, "]")
248	}
249}