You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 10, 2023. It is now read-only.
Compared to the comparable library in Rust, this package lacks support for Transparent and Alias fields. Adding support for these two features would increase interoperability with serialized data from projects written in Rust.
I would be happy to implement these myself and submit a PR, but at the moment my free time is rather limited. Here's an untested stab at what an implementation of these two features may look like (but certainly needs validation):
fromserdeimportfieldsfromserde.exceptionsimportValidationErrorclassTransparent(fields.Field):
"""Serialize and deserialize a newtype struct or a braced struct with one field exactly the same as if its one field were serialized and deserialized by itself."""def_deserialize_with(self, model, d):
iflen(d) !=1:
raiseValidationError(
'Transparent Fields {!r} must be only field in class'.format(
self._serde_name)
)
setattr(model, self._attr_name, self._deserialize(d[next(iter(d))]))
returnmodel, ddef_serialize_with(self, model, d):
returnself._serialize(getattr(model, self._attr_name))
classAlias(fields.Field):
"""Allows the specification of an alternate deserialization field name for the contained field. Alias should not be the name of another field within the same model."""def__init__(self, aliases: str, *args, **kwargs):
self.aliases=aliasessuper(Alias, self).__init__(*args, **kwargs)
def_deserialize_with(self, model, d):
foraliasinself.aliases:
ifaliasinself._model_cls._fields:
raiseValidationError(
'Alias cannot contain existing field name {!r}'.format(
alias
))
ifself._attr_nameind:
value=d[self._attr_name]
setattr(model, self._attr_name, self._deserialize(value))
else:
foraliasinself.aliases:
ifaliasind:
value=d[alias]
d[self._attr_name] =d.pop(alias)
setattr(model, self._attr_name, self._deserialize(value))
breakreturnmodel, d
Compared to the comparable library in Rust, this package lacks support for Transparent and Alias fields. Adding support for these two features would increase interoperability with serialized data from projects written in Rust.
I would be happy to implement these myself and submit a PR, but at the moment my free time is rather limited. Here's an untested stab at what an implementation of these two features may look like (but certainly needs validation):