YamlDotNet
YamlDotNet copied to clipboard
Serializing c# anonymous object
Describe the bug I have a YAML schema that has an array of items that can have the same key, e.g. choices:
- correct: answer1
- wrong: answer2
- wrong: answer3
- correct: answer4
In order to make this I used the following setup:
public List<object> choices { get; set; }
and the following usage of this property:
choices.Add( isCorrect ? new { correct = answer } : new { wrong = answer });
When serializing the object with this collection of anonymous objects, I have errors in the output (in bold):
choices:
- correct: 30
- wrong: 20
- wrong: 40
- &o0 wrong: 10
or:
choices:
- &o3 wrong: false
- correct: Java
- wrong: true
- wrong: Scala
Describe the solution you'd like I would like to have a setup that enables proper serialization of anonymous objects.
Describe alternatives you've considered Building the YAML object in the c# code instead of serialization.
Can you paste in a full code example that will reproduce this issue? I suspect it’s adding an anchor, just need to verify that I’d what is happening.
You can also try adding this on your serializer builder DisableAliases()
Did disablealiases work?