Git Product home page Git Product logo

Comments (2)

KamilaBorowska avatar KamilaBorowska commented on July 17, 2024 1

It's possible to manually implement deserialization function that provides a default using serde(deserialize_with) functionality. Hopefully it will help, I may later consider adding functionality to use default values for enum maps to serde_with crate.

use enum_map::{EnumArray, EnumMap};
use serde::de::{Deserializer, MapAccess, Visitor};
use serde::Deserialize;
use std::fmt;
use std::marker::PhantomData;

#[derive(Deserialize, Debug)]
struct DefaultEnumMap(#[serde(deserialize_with = "default_values")] EnumMap<u8, String>);

fn default_values<'de, D, K, V>(deserializer: D) -> Result<EnumMap<K, V>, D::Error>
where
    D: Deserializer<'de>,
    K: Deserialize<'de> + EnumArray<V>,
    V: Deserialize<'de> + Default,
{
    struct EnumMapVisitor<K, V>(PhantomData<(K, V)>);
    impl<'de, K, V> Visitor<'de> for EnumMapVisitor<K, V>
    where
        K: Deserialize<'de> + EnumArray<V>,
        V: Deserialize<'de> + Default,
    {
        type Value = EnumMap<K, V>;
        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            write!(formatter, "a map")
        }
        fn visit_map<M: MapAccess<'de>>(self, mut access: M) -> Result<EnumMap<K, V>, M::Error> {
            let mut entries = EnumMap::default();
            while let Some((key, value)) = access.next_entry()? {
                entries[key] = value;
            }
            Ok(entries)
        }
    }
    deserializer.deserialize_map(EnumMapVisitor(PhantomData))
}

fn main() {
    println!(
        "{:?}",
        serde_json::from_str::<DefaultEnumMap>(r#"{"2": "abc", "5": "def"}"#),
    );
}

from enum-map.

zesterer avatar zesterer commented on July 17, 2024

Thanks. I've ended up implementing something similar myself, but I guess there's not really a good way to have this included upstream without breaking the current semantics. Thanks!

from enum-map.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.