Polymorphic TagSerializable
See original GitHub issueThe current pattern for polymorphic serialization using TagSerializable
requires a public new static Func<TagCompound, T> DESERIALIZER
field on every subtype. This is an unnecessary and counter-intuitive pattern for subclasses that don’t change the serialization contract. The current static DESERIALIZER
pattern was created as an attempt at “class level polymorphism” to allow classes to define their deserialisation method (normally call the appropriate constructor).
There is also a lack of documentation on correct use of the serialization system.
TagSerializable
should be used for your own classesTagSerializer
should only be used for classes you don’t own (eg,Point
in another library) or structs
There are several possible solutions to the inheritance inadequacy
- Let a
DESERIALIZER
field on a parent class function for retrieving instances of the children if not ‘overriden’- This requires that the modder use
TagSerializer.GetType(tag.GetString("<type>"))
and call the appropriate constructor via reflection. - Should the
TagSerializableSerializer
enforce that the returned type is the same as<type>
and how would that interact with refactoring (whereGet<T>
is called andT
is not a parent of the type stored in<type>
so<type>
is ignored so theDESERIALIZER
forT
is called and allowed to re-interpret the data to match the requested type. (My intuition is that refactoring and inheritance don’t mix under the current system unless the modder uses reflection and some kind of old -> new type mapping in theirDESERIALIZER
- This requires that the modder use
- Allow a constructor with a single
TagCompound
parameter to take the place of theDESERIALIZER
field.- This should probably be implemented anyway. It has the downside that the constructor must be explicitly overriden in all subclasses, but this is much more intuitive.
- The reason this option wasn’t chosen in the first place is it doesn’t allow for as much flexibility deserializer implementation, but it should probably be the default for most modders and documented as such.
- Create an extension of
TagSerializable
likeTagSerializableAuto
(can’t think of a good name at the time) which defines an additionalvoid Deserialize(TagCompound)
method, and calls the default constructor
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:8 (6 by maintainers)
Top Results From Across the Web
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 >polymorphism.md
The most straightforward way to use serialization with a polymorphic hierarchy is to mark the base class sealed . All subclasses of a...
Read more >Serialization of polymorphic objects - java
According to java doc of Serializable interface, all the subtypes of a serializable class are themselves serializable.
Read more >Polymorphic Serialization with .NET System.Text.Json
So you've decided you needed to use inheritance within your object model but are struggling to serialize all the data present on the ......
Read more >Tag: Polymorphism - Erik the <Coder>
This embeds type names into JSON text as properties named “$type”. When the JSON text is de-serialized, […] Posted in ArchitectureTagged C#, Contravariance, ......
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
Everything discussed here stands, but given the lack of discussion I assume it’s low priority for the amount of work it would require. Polymorphism is only used by the few experienced devs in the community and they’re also probably quite capable of working around it for now. If this message gets 15 upvotes, I’ll change it to high priority.
What about NonSerializedAttribute ?