Alternate matching of struct members for JSON serialization


#1

I’m attempting to learn Rust by porting across a Go library. This library relies heavily on JSON serialization for configuration data. One on the fields within their schema collides with a Rust keyword (type). This prevents me from easily decoding/encoding a struct.

// Go code 
type Network struct {
  Type: string `json:"type"`
  // other fields here 
}

To Rust

pub struct Network {
   pub type: String,   // this won't compile as type is a keyword!
   // other fields here
}

Go allows a form of annotations to indicate the name of the field. AFAICT this isn’t possible in Rust so I’m guessing I have to write some custom serialization here? The documentation does a pretty good job of explaining the ToJSON trait but there isn’t really a great example going the other way (JSON -> Struct). Do I need to implement the Decodable trait here?

Any help greatly appreciated!


#2

Serde supports field renaming: https://github.com/serde-rs/serde#annotations