2

I have a class which looks like this:

public class Item
{
    public Guid Id { get; set; }
    public Guid Parent { get; set; }
    public string Name { get; set; }
}

If I serialize it:

        var parentId = Guid.NewGuid();
        var items = new List<Item>
        {
            new Item {Id = parentId, Parent = Guid.Empty, Name = "Parent"},
            new Item {Id = Guid.NewGuid(), Parent = parentId, Name = "Child"}
        };

        var builder = new StringBuilder();

        var serializer = new YamlDotNet.Serialization.Serializer();

        using (var writer = new StringWriter(builder))
        {
            serializer.Serialize(writer, items);
        }

        Console.WriteLine(builder.ToString());

The output is:

- Id: 1c49bc54-d0e3-494e-8fd7-b9ffd8c1892a
  Name: Parent
- Id: dd54864a-61f7-4d73-b89d-9bd4e4c2c8f4
  Parent: *o0
  Name: Child

There is a reference *o0 created which is the ID of the other class which is it's parent. But YamlDotnet does not define this reference, therefore when deserializing I get an error saying it's missing an anchor.

Is this a bug in the framework or is there anything I can do about it?

Anthon
  • 69,918
  • 32
  • 186
  • 246
Zebi
  • 8,682
  • 1
  • 36
  • 42
  • 1
    The example given in the YamlDotnet properly creates an anchor for a referenced mapping. But you have to write the serializer/emitter in such a way that any object can be an anchor and it looks like YamlDotnet doesn't support that (which is a bug). – Anthon Aug 07 '16 at 15:49
  • I thought so, I already created an issue on the github page as well https://github.com/aaubry/YamlDotNet/issues/198 . I think we can work around this problem. – Zebi Aug 08 '16 at 09:33
  • I had seen your bugreport. The anchor/alias implementation looks non-standard. It also supports [aliases before anchors](https://github.com/aaubry/YamlDotNet/blob/master/YamlDotNet.Test/Serialization/SerializationTests.cs#L213) where the YAML 1.2 standard explicitly states the first node use should have the anchor (thus disallowing forward referencing). – Anthon Aug 08 '16 at 09:42

0 Answers0