issue while serializing graphs with protobuf.net

*爱你&永不变心* 提交于 2019-12-24 09:37:13

问题


while playing with the newly released protobuf.net, we are encountering the issue illustrated in the code below:

[ProtoContract]
class Node
{
    public Node()
    {
        ChildLinks = new List<Link>();
        ParentLinks = new List<Link>();
    }

    [ProtoMember(1, IsRequired = true)]
    public string Data { get; set; }

    [ProtoMember(2, IsRequired = true)]
    public List<Link> ChildLinks { get; set; }

    [ProtoMember(3, IsRequired = true)]
    public List<Link> ParentLinks { get; set; }

    public void AddChild(Node child)
    {
        Link link = new Link { Parent = this, Child = child };
        ChildLinks.Add(link);
        child.ParentLinks.Add(link);
    }
}

[ProtoContract]
class Link
{
    [ProtoMember(2, AsReference = true, IsRequired = true)]
    public Node Child { get; set; }

    [ProtoMember(3, AsReference = true, IsRequired = true)]
    public Node Parent { get; set; }
}

public static void Main()
{
    Node node = new Node { Data = "parent" };
    node.AddChild(new Node { Data = "child" });

    using (MemoryStream memStream = new MemoryStream())
    {
        Serializer.Serialize(memStream, node);
        memStream.Position = 0;
        Node deserialized = Serializer.Deserialize<Node>(memStream);

        Link childLink = deserialized.ChildLinks.Single();
        Debug.Assert(ReferenceEquals(childLink, childLink.Child.ParentLinks.Single()));
    }
}

The assert throws an exception... Our goal here is to have a unique instance of a Link object in the ChildLinks and ParentLinks properties. We tried the AsReference attribute but it didn't work...

Does anyone know how we could fix that ?


回答1:


I still need to assess the impact (not something to dry doing while my head feels like cheese), but there is a .... trouble-spot with the root object in the graph - meaning: because I usually handle the reference tracking while walking the association, and on the root object there is no association. I guess I can probably address this with some type-level attribute instead (i.e. always treat as a reference).

Anyway, for now you can side-step this by adding one more level to the graph (artificially adding an association), i.e.

static class Program
{
    public static void Main()
    {

        Node node = new Node { Data = "parent" };
        node.AddChild(new Node { Data = "child" });
        using (MemoryStream memStream = new MemoryStream())
        {
            Serializer.Serialize(memStream, new NodeWrapper { Root = node });
            memStream.Position = 0;
            Node deserialized = Serializer.Deserialize<NodeWrapper>(memStream).Root;

            Link childLink = deserialized.ChildLinks.Single();
            Debug.Assert(ReferenceEquals(childLink, childLink.Child.ParentLinks.Single()));
        }
    }
}
[ProtoContract]
class NodeWrapper
{
    [ProtoMember(1, AsReference = true, IsRequired = true)]
    public Node Root {get;set;}
}


来源:https://stackoverflow.com/questions/6100647/issue-while-serializing-graphs-with-protobuf-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!