2 use std::collections::BTreeMap;
4 pub use serde_json::Value as Json;
5 use serde_json::{Map, Number};
8 fn to_json(&self) -> Json;
11 impl ToJson for Json {
12 fn to_json(&self) -> Json {
17 macro_rules! to_json_impl_num {
19 $(impl ToJson for $t {
20 fn to_json(&self) -> Json {
21 Json::Number(Number::from(*self))
27 to_json_impl_num! { isize, i8, i16, i32, i64, usize, u8, u16, u32, u64 }
29 impl ToJson for bool {
30 fn to_json(&self) -> Json {
36 fn to_json(&self) -> Json {
37 Json::String(self.to_owned())
41 impl ToJson for String {
42 fn to_json(&self) -> Json {
43 Json::String(self.to_owned())
47 impl<'a> ToJson for Cow<'a, str> {
48 fn to_json(&self) -> Json {
49 Json::String(self.to_string())
53 impl<A: ToJson> ToJson for [A] {
54 fn to_json(&self) -> Json {
55 Json::Array(self.iter().map(|elt| elt.to_json()).collect())
59 impl<A: ToJson> ToJson for Vec<A> {
60 fn to_json(&self) -> Json {
61 Json::Array(self.iter().map(|elt| elt.to_json()).collect())
65 impl<'a, A: ToJson> ToJson for Cow<'a, [A]>
69 fn to_json(&self) -> Json {
70 Json::Array(self.iter().map(|elt| elt.to_json()).collect())
74 impl<T: ToString, A: ToJson> ToJson for BTreeMap<T, A> {
75 fn to_json(&self) -> Json {
76 let mut d = Map::new();
77 for (key, value) in self {
78 d.insert(key.to_string(), value.to_json());
84 impl<A: ToJson> ToJson for Option<A> {
85 fn to_json(&self) -> Json {
88 Some(ref value) => value.to_json(),