1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
pub use nop_json_derive::*;
use crate::debug_to_json::DebugToJson;
use crate::value::Value;
use crate::nop_json::{escape};

use std::{char, fmt, f32, f64, io};
use std::collections::{HashMap, HashSet, BTreeMap, BTreeSet, LinkedList, VecDeque};
use std::sync::{RwLock, Mutex, Arc};
use std::rc::Rc;


/// Trait that can be automatically derived for structs and enums.
///
/// Example:
///
/// ```
/// use nop_json::WriteToJson;
///
/// #[derive(WriteToJson)]
/// struct Point {x: i32, y: i32}
///
/// let point = Point {x: 1, y: 2};
/// point.write_to_json(&mut std::io::stdout()).unwrap();
/// ```
pub trait WriteToJson<W: io::Write>
{	fn write_to_json(&self, out: &mut W) -> io::Result<()>;
}

fn write_debug_to_json<W, T>(out: &mut W, value: &T) -> io::Result<()> where T: DebugToJson, W: io::Write
{	struct Wrapper<'a, T: DebugToJson>
	{	value: &'a T
	}
	impl<'a, T: DebugToJson> fmt::Display for Wrapper<'a, T>
	{	fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result
		{	DebugToJson::fmt(self.value, out)
		}
	}
	let w = Wrapper {value};
	write!(out, "{}", w)
}

impl<W: io::Write> WriteToJson<W> for ()     {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
impl<W: io::Write> WriteToJson<W> for isize  {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
impl<W: io::Write> WriteToJson<W> for i128   {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
impl<W: io::Write> WriteToJson<W> for i64    {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
impl<W: io::Write> WriteToJson<W> for i32    {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
impl<W: io::Write> WriteToJson<W> for i16    {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
impl<W: io::Write> WriteToJson<W> for i8     {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
impl<W: io::Write> WriteToJson<W> for usize  {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
impl<W: io::Write> WriteToJson<W> for u128   {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
impl<W: io::Write> WriteToJson<W> for u64    {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
impl<W: io::Write> WriteToJson<W> for u32    {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
impl<W: io::Write> WriteToJson<W> for u16    {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
impl<W: io::Write> WriteToJson<W> for u8     {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
impl<W: io::Write> WriteToJson<W> for f64    {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
impl<W: io::Write> WriteToJson<W> for f32    {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
impl<W: io::Write> WriteToJson<W> for bool   {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
impl<W: io::Write> WriteToJson<W> for char   {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
impl<W: io::Write> WriteToJson<W> for String {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}
impl<W: io::Write> WriteToJson<W> for Value  {fn write_to_json(&self, out: &mut W) -> io::Result<()> {write_debug_to_json(out, self)}}

impl<W: io::Write, T> WriteToJson<W> for Box<T> where T: WriteToJson<W>
{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
	{	let v: &T = &*self;
		v.write_to_json(out)
	}
}

impl<W: io::Write, T> WriteToJson<W> for RwLock<T> where T: WriteToJson<W>
{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
	{	match self.read()
		{	Ok(v) => v.write_to_json(out),
			Err(e) => Err(io::Error::new(io::ErrorKind::Other, e.to_string())),
		}
	}
}

impl<W: io::Write, T> WriteToJson<W> for Mutex<T> where T: WriteToJson<W>
{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
	{	match self.lock()
		{	Ok(v) => v.write_to_json(out),
			Err(e) => Err(io::Error::new(io::ErrorKind::Other, e.to_string())),
		}
	}
}

impl<W: io::Write, T> WriteToJson<W> for Rc<T> where T: WriteToJson<W>
{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
	{	let v: &T = &*self;
		v.write_to_json(out)
	}
}

impl<W: io::Write, T> WriteToJson<W> for Arc<T> where T: WriteToJson<W>
{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
	{	let v: &T = &*self;
		v.write_to_json(out)
	}
}

impl<W: io::Write, T> WriteToJson<W> for Option<T> where T: WriteToJson<W>
{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
	{	match *self
		{	Some(ref v) => v.write_to_json(out),
			None => write!(out, "null"),
		}
	}
}

impl<W: io::Write, T> WriteToJson<W> for Vec<T> where T: WriteToJson<W>
{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
	{	let mut c = '[';
		for item in self
		{	write!(out, "{}", c)?;
			item.write_to_json(out)?;
			c = ',';
		}
		if c == '['
		{	write!(out, "[]")
		}
		else
		{	write!(out, "]")
		}
	}
}

impl<W: io::Write, T> WriteToJson<W> for HashSet<T> where T: WriteToJson<W>
{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
	{	let mut c = '[';
		for item in self
		{	write!(out, "{}", c)?;
			item.write_to_json(out)?;
			c = ',';
		}
		if c == '['
		{	write!(out, "[]")
		}
		else
		{	write!(out, "]")
		}
	}
}

impl<W: io::Write, T> WriteToJson<W> for LinkedList<T> where T: WriteToJson<W>
{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
	{	let mut c = '[';
		for item in self
		{	write!(out, "{}", c)?;
			item.write_to_json(out)?;
			c = ',';
		}
		if c == '['
		{	write!(out, "[]")
		}
		else
		{	write!(out, "]")
		}
	}
}

impl<W: io::Write, T> WriteToJson<W> for VecDeque<T> where T: WriteToJson<W>
{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
	{	let mut c = '[';
		for item in self
		{	write!(out, "{}", c)?;
			item.write_to_json(out)?;
			c = ',';
		}
		if c == '['
		{	write!(out, "[]")
		}
		else
		{	write!(out, "]")
		}
	}
}

impl<W: io::Write, T> WriteToJson<W> for BTreeSet<T> where T: WriteToJson<W>
{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
	{	let mut c = '[';
		for item in self
		{	write!(out, "{}", c)?;
			item.write_to_json(out)?;
			c = ',';
		}
		if c == '['
		{	write!(out, "[]")
		}
		else
		{	write!(out, "]")
		}
	}
}

impl<W: io::Write, T> WriteToJson<W> for HashMap<String, T> where T: WriteToJson<W>
{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
	{	let mut c = '{';
		for (key, item) in self
		{	write!(out, "{}\"{}\":", c, escape(key))?;
			item.write_to_json(out)?;
			c = ',';
		}
		if c == '{'
		{	write!(out, "{{}}")
		}
		else
		{	write!(out, "}}")
		}
	}
}

impl<W: io::Write, T> WriteToJson<W> for BTreeMap<String, T> where T: WriteToJson<W>
{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
	{	let mut c = '{';
		for (key, item) in self
		{	write!(out, "{}\"{}\":", c, escape(key))?;
			item.write_to_json(out)?;
			c = ',';
		}
		if c == '{'
		{	write!(out, "{{}}")
		}
		else
		{	write!(out, "}}")
		}
	}
}

impl<W: io::Write, A, B> WriteToJson<W> for (A, B) where A: WriteToJson<W>, B: WriteToJson<W>
{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
	{	write!(out, "[")?;
		self.0.write_to_json(out)?;
		write!(out, ",")?;
		self.1.write_to_json(out)?;
		write!(out, "]")
	}
}

impl<W: io::Write, A, B, C> WriteToJson<W> for (A, B, C) where A: WriteToJson<W>, B: WriteToJson<W>, C: WriteToJson<W>
{	fn write_to_json(&self, out: &mut W) -> io::Result<()>
	{	write!(out, "[")?;
		self.0.write_to_json(out)?;
		write!(out, ",")?;
		self.1.write_to_json(out)?;
		write!(out, ",")?;
		self.2.write_to_json(out)?;
		write!(out, "]")
	}
}