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

Implement #[serde(implied(key = "key", value = "value")] for adding static values #2908

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

frectonz
Copy link

@frectonz frectonz commented Mar 10, 2025

The implied Attribute

It would be useful to introduce an implied attribute in serde, which automatically adds a specific key-value pair during serialization and ensures that the key is present during deserialization. This would allow us to automatically include predefined keys (like method in the following example) in the serialized and deserialized data without manually specifying them in thestruct.

Example:

#[derive(Debug, Deserialize, Serialize)]
#[serde(implied(key = "method", value = "Target.setDiscoverTargets"))]
struct TargetSetDiscoverTargets {
    id: String,
    params: TargetSetDiscoverTargetsParams,
}

Expected Behavior

1. Serialization

When serializing an instance of TargetSetDiscoverTargets, serde should automatically include the "method": "Target.setDiscoverTargets" key-value pair in the resulting JSON:

{
    "method": "Target.setDiscoverTargets",
    "id": "some-id",
    "params": {  }
}

2. Deserialization

During deserialization, serde should check if the "method" key exists in the input and match it against the predefined value ("Target.setDiscoverTargets"). If it does not match or is missing, serde should raise an error.

{
    "method": "Target.setDiscoverTargets",
    "id": "some-id",
    "params": {  }
}

Alternatives Considered

Manually adding the method field during serialization and validating its existence during deserialization. This approach introduces repetitive code that could be automated with the implied attribute. This also means that the user will have to implement Serialize and Deserialize themselves.

This PR implements this attribute.

@Mingun
Copy link
Contributor

Mingun commented Mar 10, 2025

#1448 introduces #[serde(tag="...")] on structs. Your needs can be addressed with that code:

#[derive(Debug, Deserialize, Serialize)]
#[serde(tag = "method", rename = "Target.setDiscoverTargets")]
struct TargetSetDiscoverTargets {
    id: String,
    params: TargetSetDiscoverTargetsParams,
}

Is that would enough for you?

@frectonz
Copy link
Author

Oh nice, #1448 handles the serialization part of the problem. It adds the tag key and value as a field when it serializes the struct, but it doesn't check for the tag when it deserializes.

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