3
votes

The rustc-serialize package for Rust allows a struct to be serialized or deserialized to JSON automatically in some cases by deriving from RustcEncodable and RustcDecodable, respectively. For example, the following struct:

#[derive(RustcEncodable, RustcDecodable)]
struct Person {
    first_name: String,
    last_name: String,
    age: u8,
}

would be represented as this JSON:

{
  "first_name": "Joe",
  "last_name": "Shmoe",
  "age": 30
}

I need to deserialize some JSON that uses camel-cased keys. This seems to require that the struct fields be named similarly, which emits a compiler warning that struct fields should be snake-cased. I can silence this warning with #[allow(non_snake_case)], but I'd rather have the struct field snake-cased. Is there a way to do this without manually implementing JSON serialization/deserialization with the ToJson/Encodable/Decodable traits?

1
You can also use serde, which allows to rename fields natively. - Byron
Ah ha! I looked at serde but didn't know it could do that! The docs probably need some love. - Jimmy

1 Answers

4
votes

No, the #[derive] infrastructure provides no opportunities for customisation at all.

You can, however, know what the #[derive] stuff expands to with rustc -Z unstable-options --pretty expanded:

#![feature(no_std)]
#![no_std]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std as std;
struct Person {
    first_name: String,
    last_name: String,
    age: u8,
}
#[automatically_derived]
impl ::rustc_serialize::Decodable for Person {
    fn decode<__D: ::rustc_serialize::Decoder>(__arg_0: &mut __D)
     -> ::std::result::Result<Person, __D::Error> {
        __arg_0.read_struct("Person", 3usize, |_d| -> _ {
                            ::std::result::Result::Ok(Person{first_name:
                                                                 match _d.read_struct_field("first_name",
                                                                                            0usize,
                                                                                            ::rustc_serialize::Decodable::decode)
                                                                     {
                                                                     ::std::result::Result::Ok(__try_var)
                                                                     =>
                                                                     __try_var,
                                                                     ::std::result::Result::Err(__try_var)
                                                                     =>
                                                                     return ::std::result::Result::Err(__try_var),
                                                                 },
                                                             last_name:
                                                                 match _d.read_struct_field("last_name",
                                                                                            1usize,
                                                                                            ::rustc_serialize::Decodable::decode)
                                                                     {
                                                                     ::std::result::Result::Ok(__try_var)
                                                                     =>
                                                                     __try_var,
                                                                     ::std::result::Result::Err(__try_var)
                                                                     =>
                                                                     return ::std::result::Result::Err(__try_var),
                                                                 },
                                                             age:
                                                                 match _d.read_struct_field("age",
                                                                                            2usize,
                                                                                            ::rustc_serialize::Decodable::decode)
                                                                     {
                                                                     ::std::result::Result::Ok(__try_var)
                                                                     =>
                                                                     __try_var,
                                                                     ::std::result::Result::Err(__try_var)
                                                                     =>
                                                                     return ::std::result::Result::Err(__try_var),
                                                                 },}) })
    }
}
#[automatically_derived]
impl ::rustc_serialize::Encodable for Person {
    fn encode<__S: ::rustc_serialize::Encoder>(&self, __arg_0: &mut __S)
     -> ::std::result::Result<(), __S::Error> {
        match *self {
            Person {
            first_name: ref __self_0_0,
            last_name: ref __self_0_1,
            age: ref __self_0_2 } =>
            __arg_0.emit_struct("Person", 3usize, |_e| -> _ {
                                match _e.emit_struct_field("first_name",
                                                           0usize, |_e| -> _ {
                                                           (*__self_0_0).encode(_e)
                                                       }) {
                                    ::std::result::Result::Ok(__try_var) =>
                                    __try_var,
                                    ::std::result::Result::Err(__try_var) =>
                                    return ::std::result::Result::Err(__try_var),
                                };
                                match _e.emit_struct_field("last_name",
                                                           1usize, |_e| -> _ {
                                                           (*__self_0_1).encode(_e)
                                                       }) {
                                    ::std::result::Result::Ok(__try_var) =>
                                    __try_var,
                                    ::std::result::Result::Err(__try_var) =>
                                    return ::std::result::Result::Err(__try_var),
                                };
                                return _e.emit_struct_field("age", 2usize,
                                                            |_e| -> _ {
                                                            (*__self_0_2).encode(_e)
                                                        }); }),
        }
    }
}

Yeah, it’s pretty clumsy, all expanded like that, but it can be collapsed quite easily and altered to suit:

impl Decodable for Person {
    fn decode<D: Decoder>(decoder: &mut D) -> Result<Person, D::Error> {
        decoder.read_struct("Person", 3, |d| Ok(Person {
            first_name: try!(d.read_struct_field("firstName", 0, Decodable::decode)),
            last_name: try!(d.read_struct_field("lastName", 1, Decodable::decode)),
            age: try!(d.read_struct_field("age", 2, Decodable::decode)),
        }))
    }
}

impl Encodable for Person {
    fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
        s.emit_struct("Person", 3, |e| {
            try!(e.emit_struct_field("firstName", 0, |e| self.first_name.encode(e)));
            try!(e.emit_struct_field("lastName", 1, |e| self.last_name.encode(e)));
            e.emit_struct_field("age", 2, |e| self.age.encode(e))
        })
    }
}