Comments (2)
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.
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)
- Don't explicitly require importing enum_map_derive. HOT 1
- Stabilize enum-map HOT 2
- Derive Copy for EnumMap HOT 2
- Missing Ord and PartialOrd impls HOT 2
- enum_map! is unusable as a const initializer, and the array field being private makes manual initialization impossible too HOT 1
- Add EnumMap impl for Option<T: Enum> HOT 1
- Add a method for converting to an array (not a slice) HOT 1
- Clippy fails for single variant enum with Derive HOT 1
- Add Enum for tuples HOT 1
- Strange code generated in enum_map_derive? HOT 2
- Expose the `array` field (or add `as_array(&self) -> &K::Array`) HOT 8
- Derive Eq, Ord, PartialEq and PartialOrd on EnumMap<K,V> when K and V impl those traits HOT 2
- `from_usize` gives a generic, misleading panic message on out-of-bounds HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from enum-map.