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

Support BaseModel serialization to Protobuf Message #11

Open
so1n opened this issue Apr 16, 2023 · 2 comments
Open

Support BaseModel serialization to Protobuf Message #11

so1n opened this issue Apr 16, 2023 · 2 comments

Comments

@so1n
Copy link
Owner

so1n commented Apr 16, 2023

The current step of converting BaseModel to Protobuf Message is tedious and has poor performance. e.g:

from google.protobuf.json_format import ParseDict
from pydantic import BaseModel
from demo_pb2 import DemoMessage

class Demo(BaseModel):
    pass


ParseDict(Demo().dict(), DemoMessage())

Serialization can be optimized in the following ways:

  • 1.Convert directly to Message:

    from protobuf_to_pydantic.p2p_model import P2PBaseModel
    
    class Demo(P2PBaseModel):
        pass
    
    Demo().to_message()
  • 2.Serialized to dict, then converted to Message by the developer (JSON-like serialization on .dict() pydantic/pydantic#1409 (comment))

    from google.protobuf.json_format import ParseDict
    from pydantic import BaseModel
    from demo_pb2 import DemoMessage
    
    class Demo(BaseModel):
        pass
    
    
    DemoMessage(**Demo().dict())
@mikey0000
Copy link

So yeah came here to use this to convert from protobuf to model and am stumped as there isn't really strong documentation for more complex multi-level models or submessage types. Love that this generates all the models so easily so big thank you!

@so1n
Copy link
Owner Author

so1n commented May 9, 2024

@mikey0000 Protobuf's JsonFormat implementation is more complex there are many cases that can not be dealt with, you need to modify his code to achieve. So I will delay the implementation of this feature.

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

When branches are created from issues, their pull requests are automatically linked.

2 participants