Open dammer opened 1 year ago
i think good syntax for this would be:
abstract class Shape
include JSON::Serializable
use_json_discriminator "type", {point: Point, circle: Circle}
property type = "point"
end
@kostya That would be very difficult to implement because the default value of the ivar is only known once an object is instantiated, but discriminator parsing needs to happen before that in order to determine which type to instantiate.
i think macro can just extract default value of instance variable, before instantiate.
I think an explicit default
parameter is genuinely better. It is purely a matter of use_json_discriminator
and should be encapsulated in that macro call. Externalizing the default discriminator type to a type property is mixing the concerns of the data model and serialization logic.
use_json_discriminator
does not even require the existence of a property that maps to the discriminator field. So it shouldn't require a property to define a default value either.
My API proposal is use_json_discriminator "type", {point: Point, circle: Circle}, default: Point
At the moment, use_*_discriminator
does require the corresponding instance variable to exist in order for serialization to work correctly, though #11894 tries to lift that requirement
use_*_discriminator
is about deserialization and never accesses the field as property on the deserialized object.
For example:
wold be great to have this possibility