VP.
VP.

Reputation: 16685

How to deserialize a subfield of a struct from the original struct's JSON with Serde?

I want to have the Test::team_size attribute be deserialized from the data of Test object itself:

#[derive(Debug, Serialize, Deserialize)]
struct TeamSize {
    pub min: i64,
    pub max: i64,
}

#[derive(Debug, Serialize, Deserialize)]
struct Test {
    pub i: i64,
    pub team_size: TeamSize,
}

fn main() {
    let t: Test = serde_json::from_str(r#"{"i": -2, "min": 2, "max": 5}"#).unwrap();
    assert_eq!(t.i, -2);
    assert_eq!(t.team_size.min, 2);
    assert_eq!(t.team_size.max, 5);
}

This code does not compile and I don't know how to make Serde do what I want. Is there a way to deserialize team_size in this example from the JSON of the original structure where it is a subfield?

It seems that I want something like #[serde(untagged)] but for a struct and for a field and not the whole struct.

Upvotes: 5

Views: 2836

Answers (2)

dtolnay
dtolnay

Reputation: 10982

As of Serde 1.0.34, you can now use #[serde(flatten)]:

#[derive(Debug, Serialize, Deserialize)]
struct Test {
    pub i: i64,
    #[serde(flatten)]
    pub team_size: TeamSize,
}

Before this, the easiest workaround would be to deserialize into a private helper type and restructure it as needed:

#[macro_use]
extern crate serde_derive;

extern crate serde;
extern crate serde_json;

use serde::{Serialize, Serializer, Deserialize, Deserializer};

#[derive(Debug)]
pub struct TeamSize {
    pub min: i64,
    pub max: i64,
}

#[derive(Debug)]
pub struct Test {
    pub i: i64,
    pub team_size: TeamSize,
}

// Private helper
#[derive(Serialize, Deserialize)]
struct Helper {
    pub i: i64,
    pub min: i64,
    pub max: i64,
}

impl Serialize for Test {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where S: Serializer
    {
        let helper = Helper {
            i: self.i,
            min: self.team_size.min,
            max: self.team_size.max,
        };
        helper.serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for Test {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: Deserializer<'de>
    {
        let helper = Helper::deserialize(deserializer)?;
        Ok(Test {
            i: helper.i,
            team_size: TeamSize {
                min: helper.min,
                max: helper.max,
            },
        })
    }
}

fn main() {
    let j = r#" { "i": -2, "min": 2, "max": 5 } "#;

    let de: Test = serde_json::from_str(j).unwrap();
    println!("{:#?}", de);

    let ser = serde_json::to_string_pretty(&de).unwrap();
    println!("{}", ser);
}

Upvotes: 8

Shepmaster
Shepmaster

Reputation: 430310

The Serde documentation has a chapter on how to implement custom deserialization when the autogenerated attributes don't fully do what you want. Starting from there, it's not too complicated, just tedious:

extern crate serde;
extern crate serde_json;

use std::fmt;

use serde::de::{self, Deserialize, Deserializer, Visitor, MapAccess};

#[derive(Debug)]
struct TeamSize {
    pub min: i64,
    pub max: i64,
}

#[derive(Debug)]
struct Test {
    pub i: i64,
    pub team_size: TeamSize,
}

impl<'de> Deserialize<'de> for Test {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: Deserializer<'de>
    {
        struct TestVisitor;

        impl<'de> Visitor<'de> for TestVisitor {
            type Value = Test;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("struct Test")
            }

            fn visit_map<V>(self, mut map: V) -> Result<Test, V::Error>
                where V: MapAccess<'de>
            {
                let mut min = None;
                let mut max = None;
                let mut i = None;

                while let Some(key) = map.next_key()? {
                    match key {
                        "min" => {
                            if min.is_some() {
                                return Err(de::Error::duplicate_field("min"));
                            }
                            min = Some(map.next_value()?);
                        }
                        "max" => {
                            if max.is_some() {
                                return Err(de::Error::duplicate_field("max"));
                            }
                            max = Some(map.next_value()?);
                        }
                        "i" => {
                            if i.is_some() {
                                return Err(de::Error::duplicate_field("i"));
                            }
                            i = Some(map.next_value()?);
                        }
                        _ => {
                            /* Ignore extra fields */
                        }
                    }
                }

                let min = min.ok_or_else(|| de::Error::missing_field("min"))?;
                let max = max.ok_or_else(|| de::Error::missing_field("max"))?;
                let i = i.ok_or_else(|| de::Error::missing_field("i"))?;

                Ok(Test { i, team_size: TeamSize { min, max }})
            }
        }

        const FIELDS: &'static [&'static str] = &["min", "max", "i"];
        deserializer.deserialize_struct("Test", FIELDS, TestVisitor)
    }
}

fn main() {
    let t: Test = serde_json::from_str(r#"{"i": -2, "min": 2, "max": 5}"#).unwrap();
    assert_eq!(t.i, -2);
    assert_eq!(t.team_size.min, 2);
    assert_eq!(t.team_size.max, 5);
}

Upvotes: 2

Related Questions