Json: Map Rust's snake_case fields to/from Json's camelCase

Created on 25 Mar 2019  路  2Comments  路  Source: serde-rs/json

I'm not sure if this is possible in serde, but it would be nice if json fields in camel case could be mapped to snake case rust field names, either explicitly via some macros or automatically by the deserializer. This way one could adhere to conventions in naming both on the frontend and backend (camel in json/JS, snake in Rust).

My apologies if this feature exists and I simply missed it, I'd appreciate some pointers.

Most helpful comment

You can use serde(rename_all = "camelCase") on a struct to map the fields this way.

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Mandreyel {
    aaa_aaa: u8, // deserializes from json "aaaAaa"
    bbb_bbb: u8,
}

```

All 2 comments

You can use serde(rename_all = "camelCase") on a struct to map the fields this way.

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Mandreyel {
    aaa_aaa: u8, // deserializes from json "aaaAaa"
    bbb_bbb: u8,
}

```

That is exactly what I'm looking for, thanks! :relaxed:

Was this page helpful?
0 / 5 - 0 ratings