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

Inquiry: does this make deserializing HashMap<Unicase, ...> possible? #1

Closed
isosphere opened this issue Jul 14, 2020 · 3 comments
Closed
Labels

Comments

@isosphere
Copy link

(note: I'm new to Rust)

I think this is related to serde-rs/serde#723 which seems to not be resolved yet.

I have a JSON response I want to deserialize, but I want the keys of a particular hashmap to be case-insensitive.

The following throws an error (mismatched types, expected vec etc), and I think I understand why:

    #[serde(default, with = "unicase_serde::unicase")]
    results: Vec<HashMap<UniCase<String>, Option<String>>>

I don't think I can use that "with" line unless the following type is a straight-up UniCase, which it is not, though it contains one.

Is what I'm attempting possible right now? My fallback is to do some slow upper/lower casing over the whole structure after the initial deserialization to String. Not ideal, but I'll survive.

@lawliet89
Copy link
Owner

lawliet89 commented Jul 14, 2020

Welcome to Rust!

No, you can't do this at the moment unfortunately. You are right that it relates to serde-rs/serde#723.

The current workaround involves writing some painful wrapper types and then manually implementing Serialize and Deserialize for them.

struct WrapperString(pub UniCase<String>);

In this case, it will help if you impl Deref for this type so that you can call the methods of the wrapped type easier without having to resort to test_string.0.method().

If you are not sure how to proceed from here, I can cook up some example code for you.

@isosphere
Copy link
Author

Darn.

Thank you for your reply and offer. For the moment I will just make-do with case sensitivity, it's not entirely necessary for my use case, just very nice to have.

I'll revisit if progress is made with that serde issue!

@lawliet89
Copy link
Owner

To add on, FWIW, I have commented on seanmonstar/unicase#41 to try and get this functionality implemented directly in the unicase crate so that we don't have to workaround this.

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

No branches or pull requests

2 participants