开发者

Understanding ProtoBuf-Net AsReference with recursive referencing

开发者 https://www.devze.com 2023-03-31 03:17 出处:网络
I am trying out the AsReference option in ProtoMember for recursive referencing. If I create AnOwner with the public constructor and then serialize/deserialize, AnOwner.Data becomes null. Can someone

I am trying out the AsReference option in ProtoMember for recursive referencing. If I create AnOwner with the public constructor and then serialize/deserialize, AnOwner.Data becomes null. Can someone explain what happens internally and whether recursive referencing is supported? Thanks!

[ProtoContract()]
public class SomeData
{
    [ProtoMember(1, AsReference = true)]
    public AnOwner Owner;

    [ProtoMember(2)]
    public string Value;

    /// <summary>
    /// ProtoBuf deserialization constructor. In fact, Serializer did not complain when this is missing
    /// </summary>
    private SomeData()
    {

    }

    public SomeData(string value)
    {
        Value = value;
    }
}

[ProtoContract()]
public class AnOwner
{
    [ProtoMember(1)]
    public SomeData Data;

    /// <summary>
    /// ProtoBuf deserialization constructor
    /// </summary>
    private AnOwner()
    {

    }

    public AnOwner(SomeData data)
    {
        Data = data;
        Data.Owner = this;
    }
}

EDIT: After much deliberation, I manage to understand it in the form of this small demo which I will share here. With the current implementation (v2 beta) it matters if AsReference=true is specified for both, neither or either with respect to which object is passed into Serializer.Serialize().

public class Program
{
    using System.IO;
    using ProtoBuf;
    using System;

    public static void main();
    {
        AnOwner owner1, owner2;
        AnOwner owner = new AnOwner();
        SomeData data = new SomeData();
        owner.Data = data;
        data.Owner = owner;
        string file = "sandbox.txt";

        try { File.Delete(file); } catch {}; // Just in case, cos' it felt like some caching was in place.

        using (var fs = File.OpenWrite(file)) { Serializer.Serialize(fs, owner); }
        using (var fs = File.OpenRead(file)) { owner1 = Serializer.Deserialize<AnOwner>(fs); }
        using (var fs = File.OpenRead(file)) { owner2 = Serializer.Deserialize<AnOwner>(fs); }

        Console.WriteLine("SomeData.i: {0}, {1}, {2}, {3}", owner1.Data.i, owner1.Data.Owner.Data.i, owner2.Data.i, owner2.Data.Owner.Data.i);
        Console.WriteLine("AnOwner.i: {0}, {1}, {2}, {3}", owner1.i, owner1.Data.Owner.i, owner2.i, owner2.Data.Owner.i);

        System.Diagnostics.Debug.Assert(owner1 == owner1.Data.Owner, "1. Expect reference same, but not the case.");
        System.Diagnostics.Debug.Assert(owner2 == owner2.Data.Owner, "2. Expect reference same, but not the case.");
        System.Diagnostics.Debug.As开发者_如何学Csert(owner1 != owner2, "3. Expect reference different, but not the case.");
    }
}

[ProtoContract()]
public class SomeData
{
    public static readonly Random RAND = new Random(2);

    [ProtoMember(1, AsReference = true)]
    public AnOwner Owner;

    // Prove that SomeData is only instantiated once per deserialise
    public int i = RAND.Next(100);

    public SomeData() { }
}

[ProtoContract()]
public class AnOwner
{
    public static readonly Random RAND = new Random(3);

    [ProtoMember(1, AsReference=true)]
    public SomeData Data;

    // Prove that AnOwner is only instantiated once per deserialise
    public int i = RAND.Next(100);

    /// <summary>
    /// ProtoBuf deserialization constructor
    /// </summary>
    public AnOwner() { }
}


Basically, instead of serializing AnOwner directly, it serializes a fake (doesn't really exist) object with one or more of the following:

  • the existing key (integer) to an object that has already been seen
  • a new key
  • the object itself
  • the type information (if DynamicType is enabled)

When serializing a tracked object, an internal list is checked; if the object is there (has been seen before), then the old key (only) is written. Otherwise a new key is generated and stored against that object, and the new key and the object are written. When deserializing, if a "new key" is found, the object data is deserialized and the new object is stored against that key (actually the order here is a bit complex, to handle recursion). If an "old key" is found, the internal list is used to fetch the old old object.

For almost all objects, comparison is on a reference equality basis (even if equality is overriden). Note that this works slightly differently for strings, which are compared for string equality - so two different instances of the string "Fred" will still share a key.

I believe most recursion scenarios are supported, but if you get an issue please let me know.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号