Closed ThierryPfeiffer closed 5 years ago
Hi Titounnosaure.
In your second example, you've used a module instead of a class as a type hint.
import datetime
@dataclass
class ChatUser:
uid: datetime # <-- this should be datetime.datetime OR import datetime from datetime
name: str
The error is somewhat cryptic I confess. I'll see if that can be improved.
Your third example uses a NewType
for a type hint. That is not supported by jsons
(yet).
For the time being, you could instead do:
from dataclasses import dataclass
import jsons
class Uid(str):
pass
@dataclass
class ChatUser:
uid: Uid
name: str
dumped = jsons.dumps(ChatUser(Uid('012451'), 'Casimir'), strip_privates=True, strip_microseconds=True, verbose=True)
print(dumped)
instance = jsons.loads(dumped, ChatUser)
print(instance)
Thank you very much Ramon for this clear and quick answer. Everything works well now. :thumbsup: I didn't noticed the "subtlety" with datetime. :flushed:
I'm a beginner with Python. So I'm not sure if I use jsons wrong.
The first exemple below works well.
Both following examples fail with DeserializationError. Can someone help?