1

Given some JSON:

{
    "name": "Dude",
    "foo": {
      "bar": "baz"
    }
}

Assuming I want to map it to a struct like this:

struct Chump {
    Name: string,
    Bar: string,
}

which means I'd like to map Chump.Bar to foo.bar, is there any way I can easily achieve this with serde? Or any other form of JSON (de)serializer?

The only way I found so far is to use

#[serde(flatten)]

to get a Map of whatever gets left after parsing. Is there a better, more efficient way, maybe?

hellow
  • 12,430
  • 7
  • 56
  • 79
chris
  • 280
  • 1
  • 9
  • Your Rust code won't compile. Rust is case sensitive, which means it's not `string`, but [`String`](https://doc.rust-lang.org/std/string/struct.String.html). Also fields should be written in camelCase, which means the first character is lowercase (e.g. `name` and `bar). But this is a convention, and not an enforcement. Please fix that and provide a [mcve] best on [the playground](https://play.rust-lang.org/) if possible. – hellow Apr 26 '19 at 11:28

0 Answers0