Formatting a structure field differently than its JSON key

The rustc-serialize package for Rust allows you to serialize or deserialize a structure to JSON automatically in some cases based on RustcEncodable

and RustcDecodable

, respectively. For example, the following structure:

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

      

will be represented as this JSON:

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

      

I need to deserialize some JSON that uses keys with stone. This seems to require the structure fields to be named similarly, which gives a warning to the compiler that the structure fields should be snaked around. I can turn off this warning with #[allow(non_snake_case)]

, but I'd rather have a structure field with a snake. Is there a way to do this without manually injecting JSON serialization / deserialization using ToJson

/ Encodable

/ traits Decodable

?

+3


source to share


1 answer


No, the infrastructure #[derive]

does not provide any customization options.

You may, however, be aware that the material is #[derive]

expanded 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)
                                                        }); }),
        }
    }
}

      



Yes, its quite clumsy, it all expanded, but it can be easily destroyed and changed:

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))
        })
    }
}

      

+4


source







All Articles