Serde - JSON deserialization

Hello.
I have a Json:

     {
        "variables":[
           {
              "id":123245,
              "name":"MB_VAR_1",
              "def":{
                 "custom_param1":123456,
                 "custom_param2":"buzz"
              }
           }
        ]
     }

I would like deserialize field def as String, because the def field is replaceable (generic).
How to do it?

Rust Playground

Take a look at the deserialize_with field attribute.

You can use RawValue to accept JSON on a field.

1 Like

I have a problem.

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'de` due to conflicting requirements
 --> src/main.rs:9:5
  |
9 |     def: &'a RawValue,
  |     ^^^

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=38f6679298b01310ffdbeaa8561d418c

I'm solved this problem.

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Variable<'a>  {
    pub id: u32,
    pub name: String,
    #[serde(borrow)]
    def: &'a RawValue,
 }
 
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Config<'a> {
    #[serde(borrow)]
     variables: Vec<Variable<'a>>,
 }

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=54ae74b11acfe2c4457c8f5d759863e5