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

[Feature] Optimize WithOtherFields deserialization #2065

Open
mattsse opened this issue Feb 15, 2025 · 2 comments · May be fixed by #2110
Open

[Feature] Optimize WithOtherFields deserialization #2065

mattsse opened this issue Feb 15, 2025 · 2 comments · May be fixed by #2110
Labels
c-serde enhancement New feature or request

Comments

@mattsse
Copy link
Member

mattsse commented Feb 15, 2025

Component

rpc, serde

Describe the feature you would like

we currently use this workaround:

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
struct WithOtherFieldsHelper<T> {
#[serde(flatten)]
inner: T,
#[serde(flatten)]
other: OtherFields,
}
let mut helper = WithOtherFieldsHelper::deserialize(deserializer)?;
// remove all fields present in the inner struct from the other fields, this is to avoid
// duplicate fields in the catch all other fields because serde flatten does not exclude
// already deserialized fields when deserializing the other fields.
if let Value::Object(map) =
serde_json::to_value(&helper.inner).map_err(serde::de::Error::custom)?
{
for key in map.keys() {
helper.other.remove(key);
}
}

however we can likely achieve the same thing if the have a manual deserialize impl + visitor and use serde's helper

see also expanded code

Additional context

No response

@mattsse mattsse added the enhancement New feature or request label Feb 15, 2025
@Ayushdubey86
Copy link

I'll look into this

@PanGan21
Copy link
Contributor

Hey I gave it a try in #2110 . Looking for reviews 🙂

@github-project-automation github-project-automation bot moved this to Todo in Alloy Feb 28, 2025
@yash-atreya yash-atreya moved this from Todo to Ready for Review in Alloy Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c-serde enhancement New feature or request
Projects
Status: Ready for Review
Development

Successfully merging a pull request may close this issue.

4 participants