-
-
Notifications
You must be signed in to change notification settings - Fork 67
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
How to parse json string back to AvroModel #638
Comments
Hi, Ideally we want to have the original object. The problem is that import json
import dataclasses
from datetime import datetime
from dataclasses_avroschema.schema_generator import AvroModel
def parse_datetime(value):
return datetime.strptime(value, "%Y-%m-%dT%H:%M:%S")
@dataclasses.dataclass
class MyModel(AvroModel):
a: str
b: datetime
class Meta:
namespace = "my.namespace"
dacite_config = {"type_hooks": {datetime: parse_datetime}}
m = MyModel(a="123", b=datetime.now())
print(MyModel.parse_obj(json.loads(m.to_json())))
Out[33]: MyModel(a='123', b=datetime.datetime(2024, 5, 24, 16, 29, 3)) I will add the hook in the library so it will be easier for end users. You need to take into account that if the |
Thank you for the quick reply @marcosschroh. |
@RuiLoureiro You're welcome. I have added the missing |
Hi! I'm trying to initialize an
AvroModel
from a json string.I tried using
parse_obj
, but the strings corresponding todatetime
are not parsed into adatetime
object.Example
Is this expected behavior? And is there any alternative way to achieve this?
The text was updated successfully, but these errors were encountered: