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

Custom serde_json::Value deserialization? #2085

Closed
lquerel opened this issue Sep 2, 2021 · 1 comment
Closed

Custom serde_json::Value deserialization? #2085

lquerel opened this issue Sep 2, 2021 · 1 comment
Labels

Comments

@lquerel
Copy link

lquerel commented Sep 2, 2021

I'd like to apply a normalization function on the field names when deserializing to serde_json::Value.

let field_normalizer = |name: &str| {
            return name.to_lowercase().replace("#", "_");
};

let value: Value = serde_json::from_str_with_custom_normalizer(r#"{"Field_1": 1, "Field#2": 2}"#, field_normalizer)?;
dbg(&value);

The value displayed by the dbg macro should be:

[main.rs:10] &value = Object({
    "field_2": Number(
        2,
    ),
    "field_1": Number(
        1,
    ),
})

I'm well aware of the possible name collision after normalization but I have a mechanism to deal with that.

How can I implement a such custom generic deserialization without creating a modified copy of a Value?

@dtolnay
Copy link
Member

dtolnay commented Jan 23, 2022

Please take this question to one of the resources listed in https://github.com/serde-rs/serde/tree/v1.0.135#getting-help. Sorry that no one was able to provide guidance here.

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

No branches or pull requests

2 participants