YamlDotNet
YamlDotNet copied to clipboard
Alias resolution causes issues with custom serializers and/or complex types
Describe the bug
I'm not exactly sure what the bug is, but it seems that YamlDotNet aliases different types withing the same document as long as their YAML representation is identical. This is perfectly fine, however such documents appears to be impossible to deserialize using the same YamlDotNet.
Basically, once aliases are enabled, YamlDotNet produces document that YamlDotNet can no longer read. Here are two documents, same data, one with aliases disabled and the same with aliases enabled.
Aliases disabled in the Serializer:
version: 1
playerName: Player
summitScoreboard:
entries: []
persistentStatistics:
startedTutorials: []
completedTutorials: []
usedItems: []
obtainedItems:
- Value: 1
- Value: 0
- Value: 95
- Value: 148
- Value: 86
- Value: 137
- Value: 159
- Value: 150
- Value: 5
- Value: 3
- Value: 151
usedLootTables: []
usedSaveMarkers: 0
quickSaves: 0
activateTutorials: false
Aliases enabled in the Serializer:
version: &o0 1
playerName: Player
summitScoreboard:
entries: []
persistentStatistics:
startedTutorials: []
completedTutorials: []
usedItems: []
obtainedItems:
- *o0
- &o1
Value: *o1
- &o2
Value: *o2
- &o3
Value: *o3
- &o4
Value: *o4
- &o5
Value: *o5
- &o6
Value: *o6
- &o7
Value: *o7
- &o8
Value: *o8
- &o9
Value: *o9
- &o10
Value: *o10
usedLootTables: []
usedSaveMarkers: *o1
quickSaves: *o1
activateTutorials: *o1
I'm not sure if any of this is normal or expected - the aliasing work on that list seems a bit insane to me.
In code, obtainedItems looks like this:
public HashSet<InventoryItemStringId> obtainedItems;
where InventoryItemStringId is a struct with nothing but a get; set; integer property named Value.
When reading the document, YamlDotNet throws a nested exception about converting types... no further detail
I have disabled Aliases completely in the Serializer for now, because they produce unusable documents and the error is not very clear about why. It would be very nice to get either a better error or more insight as to why this set up does not work - shouldn't the de-aliasing be done by the parser before any deserialization occurs? I'm surprised the aliased document produces type conversion errors but the non-aliased document doesn't
Kind regards