question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Union type deserialize to dict from json

See original GitHub issue

The 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:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
lainisourgodcommented, Mar 9, 2020

@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 😅

3reactions
FaraSeercommented, Feb 8, 2020

@fanchunke1991, @obi1kenobi, please use the following way:

Agg.schema().dump(a)
a_restored = Agg.schema().load(json_str)

instead of

json_str = a.to_json()
a_restored = Agg.from_json(json_str)

I’m sorry for the inconvenience.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found