You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm having trouble with Unions of classes.
If you have subclasses of data classes the serialisation and deserialization behaviour depends on the order of the Union. However, it is not symmetrical if you also have default values in your data classes ie if the union is one way around serialisation succeeds but then deserialisation fails and vice versa.
This is because it's using the default value for value2 in SubBase to create a valid SubBase instance using only the arguments for Base.
I'm not sure if there is any way around this.
The text was updated successfully, but these errors were encountered:
pnetherwood
changed the title
Unions of classes and sub classes with default values not serialzing and deserializing correctly
Unions of classes and sub classes with default values not serializing and deserializing correctly
Jun 3, 2024
I'm having trouble with Unions of classes.
If you have subclasses of data classes the serialisation and deserialization behaviour depends on the order of the Union. However, it is not symmetrical if you also have default values in your data classes ie if the union is one way around serialisation succeeds but then deserialisation fails and vice versa.
For example:
which incorrectly prints the following:
It recognises that SubBase is an instance of Base so uses the the Base serialiser and only serializes the fields in Base.
For deserialization:
you get the right values:
However, if you switch the Union around you get the correct serialisation:
but now the deserialization is wrong:
This is because it's using the default value for value2 in SubBase to create a valid SubBase instance using only the arguments for Base.
I'm not sure if there is any way around this.
The text was updated successfully, but these errors were encountered: