ShockwaveFlash
Serialization

Polymorphism

AVM1-safe polymorphism: dispatch on an existing discriminator field, no injected $type, byte-faithful round-trips.

A base type marked [Avm1Polymorphic] with [Avm1DerivedType(typeof(D), "disc")] entries dispatches to a derived type by a discriminator. This is deliberately AVM1-shaped, not JSON:

[Avm1Polymorphic(TypeDiscriminatorPropertyName = "t")]   // "t" is a REAL field in the data
[Avm1DerivedType(typeof(Dog), "dog")]
[Avm1DerivedType(typeof(Cat), "cat")]
public abstract record Animal;

public record Dog([property: Avm1Property("n")] string Name) : Animal;
public record Cat([property: Avm1Property("l")] int Lives) : Animal;
  • On read the discriminator is taken from the existing member named by TypeDiscriminatorPropertyName (coerced with AVM1 string semantics, so a numeric or string field both work) — there is no injected $type convention.
  • On write the discriminator is only added when the serialized object does not already contain that key, so AVM1 data that already carries the field round-trips byte-faithfully.
  • Register the base on the context ([Avm1Serializable(typeof(Animal))]); the generator registers each derived type automatically. Reflection mode needs no context.

For unions distinguished by key/position rather than a discriminator field (e.g. g1, g2, …), model each as its own member, or capture the lot with [Avm1ExtensionData].