Skip to content

Commit

Permalink
Implement ToJson for all tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
aochagavia committed Jun 30, 2014
1 parent 035914e commit c3cf3b3
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions src/libserialize/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2015,25 +2015,37 @@ impl ToJson for String {
fn to_json(&self) -> Json { String((*self).clone()) }
}

impl<A: ToJson, B: ToJson> ToJson for (A, B) {
fn to_json(&self) -> Json {
match *self {
(ref a, ref b) => {
List(vec![a.to_json(), b.to_json()])
macro_rules! tuple_impl {
// use variables to indicate the arity of the tuple
($($tyvar:ident),* ) => {
// the trailing commas are for the 1 tuple
impl<
$( $tyvar : ToJson ),*
> ToJson for ( $( $tyvar ),* , ) {

#[inline]
#[allow(uppercase_variables)]
fn to_json(&self) -> Json {
match *self {
($(ref $tyvar),*,) => List(vec![$($tyvar.to_json()),*])
}
}
}
}
}

impl<A: ToJson, B: ToJson, C: ToJson> ToJson for (A, B, C) {
fn to_json(&self) -> Json {
match *self {
(ref a, ref b, ref c) => {
List(vec![a.to_json(), b.to_json(), c.to_json()])
}
}
}
}
tuple_impl!{A}
tuple_impl!{A, B}
tuple_impl!{A, B, C}
tuple_impl!{A, B, C, D}
tuple_impl!{A, B, C, D, E}
tuple_impl!{A, B, C, D, E, F}
tuple_impl!{A, B, C, D, E, F, G}
tuple_impl!{A, B, C, D, E, F, G, H}
tuple_impl!{A, B, C, D, E, F, G, H, I}
tuple_impl!{A, B, C, D, E, F, G, H, I, J}
tuple_impl!{A, B, C, D, E, F, G, H, I, J, K}
tuple_impl!{A, B, C, D, E, F, G, H, I, J, K, L}

impl<'a, A: ToJson> ToJson for &'a [A] {
fn to_json(&self) -> Json { List(self.iter().map(|elt| elt.to_json()).collect()) }
Expand Down

0 comments on commit c3cf3b3

Please sign in to comment.