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.

deserialize based on value?

See original GitHub issue

Hi! I’m still playing with your amazing library. In continue of another question:

I’ve main dataclass:

@dataclass(slots=True)
class GameRequest(DataClassJSONMixin):
    c: int
    s: int
    d: Any
    o: str = None
    t: int = None

I’ve many other classes for different c value of main class and want to get them into d value. I tried to use field(metadata={"deserialize": ... }) and define custom _deserialize in SerializableType. But main problem is that i always get only dict, passed to d 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'}

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Fatal1tycommented, Apr 3, 2022

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
0reactions
amirotincommented, Apr 3, 2022

post_deserialize would be last thing on my mind Thanks! Worked.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson deserialize based on type - Stack Overflow
} } } I am trying to avoid custom deserializer and attempting to deserialize the above JSON (called Wrapper. java) into Java POJOs....
Read more >
Getting Started with Custom Deserialization in Jackson
This quick tutorial will illustrate how to use Jackson 2 to deserialize JSON using a custom Deserializer.
Read more >
Deserialization of a polymorphic type based on presence of a ...
I'd like a feature on jackson for deserialization of polymorphic type ... based on the presence (or type or value) of an object's...
Read more >
Best way to deserialize json objects based in property value ...
[Solved]-Best way to deserialize json objects based in property value-kotlin ... So when we convert the JSON string into the Event object, the...
Read more >
Definitive Guide to Jackson ObjectMapper - Serialize and ...
In this detailed guide - learn everything you need to know about ObjectMapper. Convert JSON to and from Java POJOs, implement custom ...
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