From 2224e9afdffe886ffc5aa2a61e839e054b233df7 Mon Sep 17 00:00:00 2001 From: kvark Date: Sun, 7 Sep 2014 10:43:15 -0400 Subject: [PATCH] Fixed the struct map interaction with serial_name. Added a separarte JSON test for serial_name. --- benches/bench_log.rs | 1 - serde_macros/src/lib.rs | 35 ++++++++++++++++++++++++----------- tests/json_struct.rs | 31 +++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 12 deletions(-) create mode 100644 tests/json_struct.rs diff --git a/benches/bench_log.rs b/benches/bench_log.rs index d9c47251..67da760b 100644 --- a/benches/bench_log.rs +++ b/benches/bench_log.rs @@ -844,7 +844,6 @@ fn bench_manual_mem_writer_no_escape(b: &mut Bencher) { b.iter(|| { let mut wr = MemWriter::with_capacity(1024); manual_no_escape(wr.by_ref(), &log); - let _json = wr.unwrap(); //let _json = String::from_utf8(_json).unwrap(); diff --git a/serde_macros/src/lib.rs b/serde_macros/src/lib.rs index 322d9736..a61f298f 100644 --- a/serde_macros/src/lib.rs +++ b/serde_macros/src/lib.rs @@ -291,15 +291,16 @@ fn deserialize_struct( deserializer: Gc, token: Gc ) -> Gc { - let serial_names = definitions.iter().map(|def| - find_serial_name(def.node.attrs.iter()) - ).collect(); + let serial_names: Vec> = + definitions.iter().map(|def| + find_serial_name(def.node.attrs.iter()) + ).collect(); let struct_block = deserialize_struct_from_struct( cx, span, type_ident, - serial_names, + serial_names.as_slice(), fields, deserializer ); @@ -308,6 +309,7 @@ fn deserialize_struct( cx, span, type_ident, + serial_names.as_slice(), fields, deserializer ); @@ -332,7 +334,7 @@ fn deserialize_struct_from_struct( cx: &ExtCtxt, span: Span, type_ident: Ident, - serial_names: Vec>, + serial_names: &[Option], fields: &StaticFields, deserializer: Gc ) -> Gc { @@ -364,6 +366,7 @@ fn deserialize_struct_from_map( cx: &ExtCtxt, span: Span, type_ident: Ident, + serial_names: &[Option], fields: &StaticFields, deserializer: Gc ) -> Gc { @@ -380,9 +383,14 @@ fn deserialize_struct_from_map( .collect(); // Declare key arms. - let key_arms: Vec = fields.iter() - .map(|&(name, span)| { - let s = cx.expr_str(span, token::get_ident(name)); + let key_arms: Vec = serial_names.iter() + .zip(fields.iter()) + .map(|(serial, &(name, span))| { + let serial_name = match serial { + &Some(ref string) => string.clone(), + &None => token::get_ident(name), + }; + let s = cx.expr_str(span, serial_name); quote_arm!(cx, $s => { $name = Some( @@ -393,9 +401,14 @@ fn deserialize_struct_from_map( }) .collect(); - let extract_fields: Vec> = fields.iter() - .map(|&(name, span)| { - let name_str = cx.expr_str(span, token::get_ident(name)); + let extract_fields: Vec> = serial_names.iter() + .zip(fields.iter()) + .map(|(serial, &(name, span))| { + let serial_name = match serial { + &Some(ref string) => string.clone(), + &None => token::get_ident(name), + }; + let name_str = cx.expr_str(span, serial_name); quote_stmt!(cx, let $name = match $name { Some($name) => $name, diff --git a/tests/json_struct.rs b/tests/json_struct.rs new file mode 100644 index 00000000..ce53cedc --- /dev/null +++ b/tests/json_struct.rs @@ -0,0 +1,31 @@ +#![feature(phase)] + +extern crate serde; +#[phase(plugin)] +extern crate serde_macros; + +#[deriving(PartialEq, Show)] +#[deriving_serializable] +#[deriving_deserializable] +struct Test { + #[serial_name = "$schema"] + schema: String, + title: String, + #[serial_name = "type"] + ty: int +} + +#[test] +fn test_json_struct() { + let input = Test { + schema: "a".to_string(), + title: "b".to_string(), + ty: 3, + }; + + let s = serde::json::to_string(&input).unwrap(); + assert_eq!(s.as_slice(), r#"{"$schema":"a","title":"b","type":3}"#); + + let output: Test = serde::json::from_str(s.as_slice()).unwrap(); + assert_eq!(input, output); +}