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

What's the key difference between jsons and pydantic? #184

Open
Raven888888 opened this issue Oct 20, 2022 · 1 comment
Open

What's the key difference between jsons and pydantic? #184

Raven888888 opened this issue Oct 20, 2022 · 1 comment

Comments

@Raven888888
Copy link

For serialization, one can export an instance of pydantic model to json

from datetime import datetime
from pydantic import BaseModel

class BarModel(BaseModel):
    whatever: int

class FooBarModel(BaseModel):
    foo: datetime
    bar: BarModel


m = FooBarModel(foo=datetime(2032, 6, 1, 12, 13, 14), bar={'whatever': 123})
print(m.json())
#> {"foo": "2032-06-01T12:13:14", "bar": {"whatever": 123}}

For deserialization, one can directly parse string into an instance of pydantic model

from datetime import datetime
from pydantic import BaseModel

class User(BaseModel):
    id: int
    name = 'John Doe'
    signup_ts: datetime = None

m = User.parse_raw('{"id": 123, "name": "James"}')
print(m)
#> id=123 signup_ts=None name='James'

So, why jsons over pydantic?

I can see that jsons being more lightweight for obvious reasons, but I wish to hear from the authors as well!

@Raven888888 Raven888888 changed the title What's the key differences between jsons and pydantic? What's the key difference between jsons and pydantic? Oct 20, 2022
@JobaDiniz
Copy link

Does pydantic support nested classes?
Does it support Generics?

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

2 participants