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
The FriendlyJsonSerde has specific instructions for how to parse certain objects. More recently, I started tinkering with how we could standardize pydantic models across the libraries to serialize their "json" as JSON-RPC-spec-compatible objects.
I believe we can give the right instructions in the serde class to where if we define serialization options for all pydantic objects that expect to be serialized in some JSON-RPC call, we can directly use these objects in forming requests and have them be properly serialized, no matter their attributes or properties.
As an initial example, take the CustomPydanticModel that the SignedSetCodeAuthorization inherits from in eth-account in this PR. This defines a way to serialize the object as camel-case and defines _excludes so that it only serializes what the JSON-RPC object would expect. The SignedSetCodeAuthorization class needs only implement the excluded fields and whatever field serializers are appropriate and the util appropriately camelCases, excludes those fields, and serializes them as specified.
If we define some standard like this across our libraries and expect this standard when we serialize using the FriendlyJsonSerde in web3.py, we could directly use the pydantic model inside transactions and serialize appropriately. To be clear, we could use similar logic that the serializer util in eth-account implemented directly in the Serde class itself so that it is implicitly converted as expected.
This could be useful for new models to be introduced as pydantic models, or for converting old objects to pydantic models if / when appropriate.
Bonus: I think pydantic plays well with orjson so issue #3649 can also remain relevant here and both of these features could possibly play quite nicely together.
The text was updated successfully, but these errors were encountered:
What feature should we add?
The
FriendlyJsonSerde
has specific instructions for how to parse certain objects. More recently, I started tinkering with how we could standardize pydantic models across the libraries to serialize their "json" as JSON-RPC-spec-compatible objects.I believe we can give the right instructions in the serde class to where if we define serialization options for all pydantic objects that expect to be serialized in some JSON-RPC call, we can directly use these objects in forming requests and have them be properly serialized, no matter their attributes or properties.
As an initial example, take the
CustomPydanticModel
that theSignedSetCodeAuthorization
inherits from in eth-account in this PR. This defines a way to serialize the object as camel-case and defines_excludes
so that it only serializes what the JSON-RPC object would expect. The SignedSetCodeAuthorization class needs only implement the excluded fields and whatever field serializers are appropriate and the util appropriately camelCases, excludes those fields, and serializes them as specified.If we define some standard like this across our libraries and expect this standard when we serialize using the
FriendlyJsonSerde
in web3.py, we could directly use the pydantic model inside transactions and serialize appropriately. To be clear, we could use similar logic that the serializer util in eth-account implemented directly in the Serde class itself so that it is implicitly converted as expected.This could be useful for new models to be introduced as pydantic models, or for converting old objects to pydantic models if / when appropriate.
Bonus: I think pydantic plays well with
orjson
so issue #3649 can also remain relevant here and both of these features could possibly play quite nicely together.The text was updated successfully, but these errors were encountered: