How to achieve polymorphic behavior?
See original GitHub issueI have a slightly complex structure where my Entity class has members property which may contains members of either Container or Action type. The $type information is also coming along in the json to identify if it is a Container or Action member with every object but how to map it to corresponding classes using this $type information?
@jsonObject
export class Entity {
@jsonArrayMember(Member)
public members: Member[];
}
@jsonObject
export abstract class Member {
@jsonMember
public id: string;
@jsonMember
public name: string;
}
@jsonObject
export abstract class ContainerMember extends Member {
@jsonArrayMember(Member)
public children: Member[];
}
@jsonObject
export class SomeConcreteContainerMember1 extends ContainerMember {
// with additional properties
}
@jsonObject
export abstract class ActionMember extends Member {
}
@jsonObject
export class SomeConcreteActionMember1 extends ActionMember {
// with additional properties
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Polymorphic Behavior - IBM
Polymorphism is achieved in C++ by using inheritance and virtual functions. Consider the scenario where we have three forms (ExpenseForm, LoanForm, ...
Read more >OOP Concepts for Beginners: What is Polymorphism - Stackify
Polymorphism is one of the core concepts in OOP languages and describes the concept wherein you can use different classes with the same ......
Read more >Polymorphism in Java with Examples in 2023 - Great Learning
Polymorphism in Java can be defined as the ability of an object to take many forms. This helps us perform the same action...
Read more >10.3 Demonstrating Polymorphic Behavior | Object-Oriented ...
Harvey M. Deitel and Paul J. Deitel explain and demonstrate the concept of polymorphism with inheritance hierarchies.
Read more >What Is Polymorphism in Java and How to Implement It?
Characteristics/Features of Polymorphism · The functionality of a method behaves differently in different scenarios. · The behavior of a method ...
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 Free
Top 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

Hope, it will be helpful for someone who will search, I made some util function and new decorator to achieve polymorphic behaviour, which worked quite well for me. it is added knownTypes to root type automatically, searching for root element: https://gist.github.com/krizka/c83fb1966dd57997a1fc02625719387d
I assume this is coming from Json.NET (which uses the
$typeproperty with a fully-qualified name by default). TypedJSON is very similar, only it uses__typeby default, which can be configured by using a custom type-resolver. However, one big difference is that TypedJSON does not use a fully-qualified name, but only the class-name, so you’ll either need to configure Json.NET to only emit the class name, or write a type-resolver that only takes the last part of the fully qualified name.The signature of a type-resolver is the following:
Where
sourceObjectis a raw, untyped Javascript object (you inspect this object to look for a type-hint), andknownTypesis a map of references to any additional known classes used during the deserialization process. The return value is the class reference itself. These known types must be set in advance, more on that later.This is the default type-resolver, which uses the
__typeproperty:You can set a custom type-resolver in the configuration:
If Json.NET is emitting fully-qualified names, you should be able to do this:
Now, to recognize sub-classes during deserialization, you need to set them as known types (which is basically a listof classes). You can set this in the configuration, or in the
@jsonObjectdecorator, this must be done on the class which you expect to contain polymorphic objects:Note: you can also specify a static method by its key for
knownTypes, in that case your method should return the array of known-types.