diff --git a/src/mako/lib.rs.mako b/src/mako/lib.rs.mako index b951bb2507..f4ce73dc59 100644 --- a/src/mako/lib.rs.mako +++ b/src/mako/lib.rs.mako @@ -29,7 +29,7 @@ ${lib.docs(c)} // unused imports in fully featured APIs. Same with unused_mut ... . #![allow(unused_imports, unused_mut)] // Required for serde annotations -#![feature(custom_derive, plugin)] +#![feature(custom_derive, custom_attribute, plugin)] #![plugin(serde_macros)] extern crate hyper; diff --git a/src/mako/lib/schema.mako b/src/mako/lib/schema.mako index d7c669dc5e..7efe2e8158 100644 --- a/src/mako/lib/schema.mako +++ b/src/mako/lib/schema.mako @@ -14,6 +14,9 @@ ${struct} { % for pn, p in properties.iteritems(): ${p.get('description', 'no description provided') | rust_doc_comment, indent_all_but_first_by(1)} + % if pn != mangle_ident(pn): + #[serde(alias="${pn}")] + % endif pub ${mangle_ident(pn)}: ${to_rust_type(schemas, s.id, pn, p)}, % endfor } diff --git a/src/rust/lib.rs b/src/rust/lib.rs index 12fff2ad32..a4c89b2ccd 100644 --- a/src/rust/lib.rs +++ b/src/rust/lib.rs @@ -1,5 +1,5 @@ -#![feature(core,io,old_path, custom_derive, plugin)] -#![allow(dead_code, deprecated, unused_features)] +#![feature(core,io,old_path, custom_derive, custom_attribute, plugin)] +#![allow(dead_code, deprecated, unused_features, unused_variables, unused_imports)] //! library with code shared by all generated implementations #![plugin(serde_macros)] extern crate hyper; @@ -20,6 +20,8 @@ mod tests { use std::default::Default; use std::old_path::BytesContainer; + use serde::json; + const EXPECTED: &'static str = "\r\n--MDuXWGyeE33QFXGchb2VFWc4Z7945d\r\n\ Content-Length: 50\r\n\ @@ -77,4 +79,37 @@ bar\r\n\ // See above: headers are unordered // assert_eq!(v.container_as_str().unwrap(), EXPECTED); } + + #[test] + fn serde() { + #[derive(Default, Serialize, Deserialize)] + struct Foo { + opt: Option, + req: u32, + opt_vec: Option>, + vec: Vec, + } + + let f: Foo = Default::default(); + json::to_string(&f).unwrap(); // should work + + let j = "{\"opt\":null,\"req\":0,\"vec\":[]}"; + let f: Foo = json::from_str(j).unwrap(); + + // This fails, unless 'vec' is optional + // let j = "{\"opt\":null,\"req\":0}"; + // let f: Foo = json::from_str(j).unwrap(); + + #[derive(Default, Serialize, Deserialize)] + struct Bar { + #[serde(alias="snooSnoo")] + snoo_snoo: String + } + json::to_string(&::default()).unwrap(); + + + let j = "{\"snooSnoo\":\"foo\"}"; + let b: Bar = json::from_str(&j).unwrap(); + assert_eq!(b.snoo_snoo, "foo"); + } } \ No newline at end of file