diff --git a/src/lib.rs b/src/lib.rs index b60fa77..c630f50 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,11 +17,12 @@ fn expand_derive_tojson(ct: &mut ExtCtxt, span: Span, _: &ast::MetaItem, item: &ast::Item, push: &mut FnMut(P)) { if let ast::ItemStruct(ref struct_def, _) = item.node { let struct_name = item.ident; - let conv_body: Vec> = struct_def.fields.iter().map(|field| { + let conv_body: Vec> = struct_def.fields.iter().map(|field| { if let ast::NamedField(name, _) = field.node.kind { let name_str = name.as_str(); - quote_stmt!(ct, - __container.insert($name_str.to_string(), self.$name.to_json())).unwrap() + quote_expr!(ct, { + __container.insert($name_str.to_string(), self.$name.to_json()); + }) } else { ct.span_fatal(span, "#[derive(ToJson)] doesn't support simple struct for now"); } @@ -31,7 +32,7 @@ fn expand_derive_tojson(ct: &mut ExtCtxt, span: Span, _: &ast::MetaItem, impl ::rustc_serialize::json::ToJson for $struct_name { fn to_json(&self) -> ::rustc_serialize::json::Json { let mut __container = ::std::collections::BTreeMap::new(); - $conv_body + $conv_body; ::rustc_serialize::json::Json::Object(__container) } }).unwrap(); diff --git a/tests/simple.rs b/tests/simple.rs index 3d18b88..fcce614 100644 --- a/tests/simple.rs +++ b/tests/simple.rs @@ -21,4 +21,7 @@ fn test_gen () { println!("{}", pj); assert_eq!(Json::String("Vidar Kjartansson".to_string()), *pj.find("name").unwrap()); + assert_eq!(Json::U64(25), *pj.find("age").unwrap()); + assert_eq!(Json::String("Iceland".to_string()), + *pj.find("nationality").unwrap()); }