Serde internally tagged enum with common fields
Asked Answered
S

1

8

I have the JSON roughly like this:

[
  {
     "commonA": 1,
     "commonB": 2,
     "type": "Foo",
     "fooSpecificA": 3,
     "fooSpecificB": 4
  },
  {
     "commonA": 5,
     "commonB": 6,
     "type": "Bar",
     "barSpecificA": 7,
     "barSpecificB": 8
  },
  ...

In other words I have internally tagged objects, but some of the fields are common to every type. I'd like to deserialise it to something like this:

struct Entry {
  commonA: i64,
  commonB: i64,
  variant: EntryVariant,
}

enum EntryVariant {
  Foo(FooSpecific),
  Bar(BarSpecific),
}

struct FooSpecific {
  fooSpecificA: i64,
  fooSpecificB: i64,
}

struct BarSpecific {
  barSpecificA: i64,
  barSpecificB: i64,
}

Is that possible with Serde?

Sporulate answered 14/4, 2020 at 9:59 Comment(4)
By the way, idiomatic Rust uses snake_case for variables, methods, macros, fields and modules; UpperCamelCase for types and enum variants; and SCREAMING_SNAKE_CASE for statics and constants.Stock
Yeah I know but a load of #[rename_all()] attributes would have made the example less clear.Sporulate
You could rename the corresponding JSON, considering that you made it up to start with.Stock
True. Also not very important here!Sporulate
L
15

Combine internally tagged unions with struct flattening.

use serde::{Serialize, Deserialize};

#[derive(Debug, Serialize, Deserialize)]
struct Entry {
    #[serde(rename = "commonA")]
    common_a: i64,
    #[serde(rename = "commonB")]
    common_b: i64,
    #[serde(flatten)]
    variant: EntryVariant,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type")]
enum EntryVariant {
    Foo(FooSpecific),
    Bar(BarSpecific),
}

#[derive(Debug, Serialize, Deserialize)]
struct FooSpecific {
    #[serde(rename = "fooSpecificA")]
    foo_specific_a: i64,
    #[serde(rename = "fooSpecificB")]
    foo_specific_b: i64,
}

#[derive(Debug, Serialize, Deserialize)]
struct BarSpecific {
    #[serde(rename = "barSpecificA")]
    bar_specific_a: i64,
    #[serde(rename = "barSpecificB")]
    bar_specific_b: i64,
}

Playground

Luci answered 14/4, 2020 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.