How to handle cycles in object graph with yamldotnet serializers

Viewed 188

I'm trying to use Yamldotnet library to serialize a model with circular references. Which serializer settings or data class attributes should I use to achieve this? I want to be able to serialize and deserialize the models back, keeping references.

Sample model:

public class Book
{
    public string Title { get; set; }
    public List<Author> Authors { get; set; }
}

public class Author
{
    public string Name { get; set; }
    public List<Book> Books { get; set; }
}

Currently I can serialize the model correctly to yaml:

&o0 !Author
Name: name1
Books:
- !Book
  Title: title1
  Authors:
  - *o0

, but deserialization throws an exception:

YamlDotNet.Core.AnchorNotFoundException: (Line: 7, Col: 5, Idx: 70) - (Line: 7, Col: 8, Idx: 73): Alias $o0 cannot precede anchor declaration

My code sample on .NET fiddle

1 Answers

It seems it was a bug in YamlDotNet library, it should be fixed in the next release following 9.1.4.

Related