Skip to content
Permalink
Browse files
let's do the serialization the right way
  • Loading branch information
steveklabnik committed Oct 23, 2014
1 parent f57f00e commit f0d25e5faefedd049edb47a7b52d4f8c4d339271
Showing 1 changed file with 14 additions and 0 deletions.
@@ -7,6 +7,8 @@ use nickel::{Nickel, Request, Response, HttpRouter, StaticFilesHandler};

use postgres::{PostgresConnection, NoSsl};

use std::collections::TreeMap;
use serialize::json::ToJson;
use serialize::json;

#[deriving(Decodable, Encodable)]
@@ -15,6 +17,18 @@ struct Todo {
is_completed: bool,
}

// Specify encoding method manually
impl ToJson for Vec<Todo> {
fn to_json(&self) -> json::Json {
let todos = self.iter().map({|todo| json::encode(todo) }).collect::<Vec<_>>();

let mut d = TreeMap::new();
d.insert("todos".to_string(), todos.to_json());

json::Object(d)
}
}

fn main() {
let mut server = Nickel::new();

0 comments on commit f0d25e5

Please sign in to comment.