Union type deserialize to dict from json
See original GitHub issueThe the Union type on a dataclass cannot be deserialized correctly from json. Example:
from dataclasses import dataclass
from dataclasses_json import dataclass_json
from typing import *
@dataclass_json
@dataclass(unsafe_hash=True)
class Gofy:
p1: str
@dataclass_json
@dataclass(unsafe_hash=True)
class Foo:
p2: str
@dataclass_json
@dataclass(unsafe_hash=True)
class Agg:
inst: Union[Gofy, Foo]
if __name__ == '__main__':
a = Agg(inst=Foo(p2='p2'))
json_str = a.to_json()
a_restored = Agg.from_json(json_str)
print(type(a_restored.inst) ) # this will print the dict type
assert a == a_restored # this check fails
assert isinstance(a_restored.inst, Foo) # this check fails
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
How can I deserialize JSON to a simple Dictionary<string ...
I have found the best way to deserialize into a dictionary is to use dynamic as the type for the values: JsonConvert.DeserializeObject<Dictionary<string ...
Read more >How to deserialize a union type from JSON : r/dartlang - Reddit
I have the following union type that I need to deserialize from JSON. It can either be a list of some type that...
Read more >Frequently Asked Questions — jsons documentation
The following example shows a class that jsons can serialize and ... first is to use a Union and define all possible types...
Read more >Home · JSON3.jl
For a JSON Object, it will return a JSON3.Object type, which acts like an immutable Dict , but has a more efficient view...
Read more >dataclasses-serialization - PyPI
Then we may serialize/deserialize it to/from JSON by using JSONSerializer ... Deserialize a Union type_ , by trying each type in turn, and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@lidatong hi! I experience the same issue on 0.4.1. Hack suggested by @FaraSeer solves the problem, but I guess it should be solved generally 😅
@fanchunke1991, @obi1kenobi, please use the following way:
instead of
I’m sorry for the inconvenience.