-
-
Notifications
You must be signed in to change notification settings - Fork 44
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
deserialize based on value? #75
Comments
Hi @amirotin I read your message several times but I couldn't figure what problem you're facing. Can you give an example of what you did, what you got and what you expected? |
Ok :) These are json examples
I have main class
I have request type classes
i want to get such result when i call
I've function which can return needed Req class for each value of c. deserialize options only gives me key d, but not the entire request dict |
I also tried to make it working in this way:
but looks like _deserialize works only for class params, not for class itself. Or i made something wrong? |
If the choice of the ReqX class depends on another field of GameRequest, you can use the post_deserialize hook: @dataclass
class GameRequest(DataClassJSONMixin):
c: int
s: int
d: Any
o: str = None
t: int = None
@classmethod
def __post_deserialize__(cls, obj):
if obj.c == 1:
obj.d = Req1.from_dict(obj.d)
elif obj.c == 2:
obj.d = Req2.from_dict(obj.d)
elif obj.c == 3:
obj.d = Req3.from_dict(obj.d)
return obj |
post_deserialize would be last thing on my mind |
Hi! I'm still playing with your amazing library. In continue of another question:
I've main dataclass:
I've many other classes for different
c
value of main class and want to get them intod
value. I tried to usefield(metadata={"deserialize": ... })
and define custom_deserialize
in SerializableType. But main problem is that i always get only dict, passed tod
and i don't have any references to main class request.using
{'c': 1, 's': 2, 'o': 3, 't': 12345, 'd': {'zzz': 'aaa'}}
i always get only{'zzz': 'aaa'}
The text was updated successfully, but these errors were encountered: