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.

Polymorphic TagSerializable

See original GitHub issue

The 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 classes
  • TagSerializer 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

  1. 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 (where Get<T> is called and T is not a parent of the type stored in <type> so <type> is ignored so the DESERIALIZER for T 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 their DESERIALIZER
  2. Allow a constructor with a single TagCompound parameter to take the place of the DESERIALIZER 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.
  3. Create an extension of TagSerializable like TagSerializableAuto (can’t think of a good name at the time) which defines an additional void Deserialize(TagCompound) method, and calls the default constructor

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
Chicken-Bonescommented, Oct 1, 2018

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.

2reactions
Jofairdencommented, Mar 28, 2018

I approve, [DontSerialize] or [NonSerialized] IAutoTagSerialized

What about NonSerializedAttribute ?

Read more comments on GitHub >

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

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