Implement #[serde(implied(key = "key", value = "value")]
for adding static values
#2908
+259
−10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
implied
AttributeIt would be useful to introduce an
implied
attribute inserde
, 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 (likemethod
in the following example) in the serialized and deserialized data without manually specifying them in thestruct
.Example:
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: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.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
andDeserialize
themselves.This PR implements this attribute.