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.

Serializing Polymophic Hierarchy

See original GitHub issue

I have decision tree where the nodes are subclasses to support different types of decisions. Not surprisingly, if I dump using the base Node schema I only get the fields in that class which doesn’t even include the sub-nodes. Like I said, no real surprise there.

Is the idea of serializing a set of classes that use polymorphism totally out of scope or something we could work towards in a future release?

I’ll poke away at some solutions. Maybe we can find something to incorporate into dcj.

import copy
import json
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import MutableMapping

from dataclasses_json import dataclass_json

class Node(ABC):
    name: str

    def decide(self, state: MutableMapping) -> bool:

class DecisionNode(Node):
    true_node: Node
    false_node: Node

    def __init__(self, name: str, true_node: Node, false_node: Node, var_key: str, value: int):
        self.true_node = true_node
        self.false_node = false_node
        self.var_key = var_key
        self.value = value

    def decide(self, state: MutableMapping) -> bool:
        state = copy.deepcopy(state)
        if state[self.var_key] > self.value:
            return self.true_node.decide(state)
            return self.false_node.decide(state)

class LeafNode(Node):
    result: bool

    def __init__(self, name: str, result: bool):
        self.result = result

    def decide(self, state) -> bool:
        return self.result

def decide():
    true_leaf = LeafNode(name="true", result=True)
    false_leaf = LeafNode(name="false", result=False)
    decision_node = DecisionNode(
        name="int value", true_node=true_leaf, false_node=false_leaf, var_key="year", value=2000
    state = {"year": 2019}
    result = decision_node.decide(state)
    assert result

    print(json.dumps(Node.schema().dump(decision_node), indent=2))

if __name__ == "__main__":

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

NoamNolcommented, Sep 12, 2022

Any update? Is it possible without a Union?

nathan5280commented, Sep 12, 2020

@aronszanto My team has switched to FastAPI which uses Pydantic for the validation and mapping functionality that I so love in the lightweight DCJ package. It is unlikely that I’ll get back to this anytime soon. Feel free to ping me if you decide to move forward with this or want to bounce some ideas off me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Polymorphism - GitHub
The most straightforward way to use serialization with a polymorphic hierarchy is to mark the base class sealed . All subclasses of a...
Read more >
Polymorphic kotlinx serialization for complex hierarchy
With polymorphic serialization, I want to serialize classes A, B1, B2. I want to get the following line with class B2
Read more >
How to serialize properties of derived classes with System ...
Polymorphic serialization supports derived types that have been explicitly opted in via the JsonDerivedTypeAttribute. Undeclared types will ...
Read more >
(Not so) Gentle introduction to Polymorphic Serialization in ...
Since we already know all different kinds of sections, we can use a sealed class to create a strict hierarchy. Hierarchy of sections....
Read more >
Polymorphic serialization doesn't work for deeper sealed class ...
I have the following code: @Serializable sealed class Component ... serialization doesn't work for deeper sealed class hierarchies.
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 Post

No results found

github_iconTop Related Hashnode Post

No results found