Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Support struct field renaming #106

Closed
aloucks opened this issue May 3, 2015 · 11 comments
Closed

Support struct field renaming #106

aloucks opened this issue May 3, 2015 · 11 comments

Comments

@aloucks
Copy link

aloucks commented May 3, 2015

There needs to be a way to rename struct fields for use with #[derive(RustcDecodable, RustcEncodable)].

For example:

#[derive(RustcDecodable, RustcEncodable)]
struct Thing {
    #[serialize(decode_name="type", encode_name="type")]
    type_ : String
}
@fuchsnj
Copy link

fuchsnj commented Jul 20, 2015

https://github.com/serde-rs/serde already has something like this

struct Thing {
    #[serde(rename="type")]
    type_ : String
}

or if you want different names for different serializations...

struct Thing {
    #[serde(rename(json="type", xml="type2"))]
    type_ : String
}

@redlolgeerf
Copy link

but serde is not a proper replacement for rustc-serialize, as it's missing some drammatic features like deserealising only part of json fields serde-rs/serde#60

@fuchsnj
Copy link

fuchsnj commented Oct 25, 2015

@redlolgeerf I agree serde is not a proper replacement. I was showing that as an example of how it could be implemented.

@redlolgeerf
Copy link

@fenhl , my point is that requested feature is a needed one, despite presence of libraries, that do have it.

@fenhl
Copy link
Contributor

fenhl commented Oct 26, 2015

cc @fuchsnj (assuming @redlolgeerf's comment was supposed to be directed at them)

@aidanhs
Copy link

aidanhs commented Nov 23, 2015

I'm interested in this.

@erickt
Copy link
Contributor

erickt commented Jan 18, 2016

@redlolgeerf: The soon to be released serde v0.7.0 will by default automatically ignore unknown fields, with an attribute that restores the old behavior.

@sjmackenzie
Copy link

any movement on this?

@norru
Copy link

norru commented Sep 20, 2016

Has this been implemented yet?

@dtolnay
Copy link
Contributor

dtolnay commented Feb 2, 2017

Now that Serde is a proper replacement for rustc-serialize (and more), I think the idea is you should use Serde if you need to customize the serialized representation in any way, including renaming fields.

#[derive(Serialize, Deserialize)]
struct Thing {
    #[serde(rename = "type")]
    ty: String
}

@alexcrichton
Copy link
Contributor

I'm going to close this now that this crate is deprecated in favor of serde. We're discontinuing feature development in rustc-serialize but will still continue to merge bug fixes if they arise.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants