Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic key-value support for Map #1228

Closed
wants to merge 1 commit into from

Conversation

HoKim98
Copy link

@HoKim98 HoKim98 commented Jan 17, 2025

Hello, this PR supports Map<K, V>.

Generic maps are useful for deserializing maps (i.e. impl<'de> Deserializer<'de, K, V> for Map<K, V>) into other types.

Side-effects

The map Map<K, V> implements other map-like operations and traits.
It may ask for users to specify the types K, V.

let mut map = serde_json::Map::new();  // error[E0282]: type annotations needed for `serde_json::Map<_, _>`
assert_eq!(map.entry("serde").key(), &"serde");

Instead, I set the default type of Map to Map<String, Value>.

let mut map: serde_json::Map = serde_json::Map::new();
assert_eq!(map.entry("serde").key(), &"serde");

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. I would prefer to keep the less generic implementation so that users are not forced to write type annotations so often when using serde_json::Map. This type is only intended for representing a map deserialized from JSON. For any other map, please use a different map type.

@dtolnay dtolnay closed this Jan 17, 2025
@HoKim98
Copy link
Author

HoKim98 commented Jan 17, 2025

Then what about renaming Map to other one (e.g. GenericMap) and make an alias for Map (i.e. type Map = GenericMap<String, Value>;)? It minimizes the side effect because there is few usage of generic Map<K, V> for now (nothing implemented at all).

@dtolnay
Copy link
Member

dtolnay commented Jan 17, 2025

I prefer not to support that in this crate. There are enough other map types that are already usable for that purpose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants