我正在尝试使用Yamldotnet库来序列化具有循环引用的模型。我应该使用哪些序列化程序设置或数据类属性来实现这一点?我希望能够序列化和反序列化模型,保留引用。
样品型号:
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; }
}
目前我可以将模型正确地序列化为yaml:
&o0 !Author
Name: name1
Books:
- !Book
Title: title1
Authors:
- *o0
,但反序列化引发异常:
YamlDotNet.Core.AnchorNotFoundException: (Line: 7, Col: 5, Idx: 70) - (Line: 7, Col: 8, Idx: 73): Alias $o0 cannot precede anchor declaration
我的代码示例位于。NET小提琴
这似乎是YamlDotNet库中的一个错误,应该在9.1.4之后的下一个版本中修复。