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

Investigate pydantic model parsing in the json Serde class #3650

Open
fselmo opened this issue Mar 20, 2025 · 0 comments
Open

Investigate pydantic model parsing in the json Serde class #3650

fselmo opened this issue Mar 20, 2025 · 0 comments

Comments

@fselmo
Copy link
Collaborator

fselmo commented Mar 20, 2025

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 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant