Skip to content

Investigate pydantic model parsing in the json Serde class #3650

Open
@fselmo

Description

@fselmo

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions