YamlDotNet icon indicating copy to clipboard operation
YamlDotNet copied to clipboard

Serializing c# anonymous object

Open igor-dragutinovic opened this issue 2 years ago • 3 comments

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.

igor-dragutinovic avatar Feb 07 '23 13:02 igor-dragutinovic

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.

EdwardCooke avatar Feb 10 '23 15:02 EdwardCooke

You can also try adding this on your serializer builder DisableAliases()

EdwardCooke avatar Feb 10 '23 15:02 EdwardCooke

Did disablealiases work?

EdwardCooke avatar Sep 01 '23 19:09 EdwardCooke