The protobuf specification doesn't have the notion of object identity.
protobuf-net *does* (as an optionally enabled feature), but it doesn't
currently work for list items directly, although I suspect it probably
should. Since it would break the format, though, it would need explicit
enabling if I fixed this.

But the following code works today - note that what I have done here is to
wrap the top-level list items in a wrapper that just encapsulates the
ProtoItem, but in doing so enables reference-tracking. Not ideal, but: it
works.

using ProtoBuf;
using System.Collections.Generic;
using System.IO;

[ProtoContract(AsReferenceDefault=true)]
public class ProtoItem
{
    [ProtoMember(1)]
    public int Value { get; set; }

    [ProtoMember(2)]
    public ProtoItem BaseItem { get; set; }
}
[ProtoContract]
public class Wrapper
{
    [ProtoMember(1, DataFormat = DataFormat.Group)]
    public ProtoItem Item { get;set; }

    public static implicit operator ProtoItem(Wrapper value)
    {
        return value == null ? null : value.Item;
    }
    public static implicit operator Wrapper(ProtoItem value)
    {
        return value == null ? null : new Wrapper { Item = value };
    }
}



static class Program
{

    static void Main()
    {
        var itemParent = new ProtoItem { Value = 1 };
        var item2 = new ProtoItem { Value = 2, BaseItem = itemParent };
        var item3 = new ProtoItem { Value = 3, BaseItem = itemParent };

        var parentListToWrite = new List<Wrapper> { itemParent, item2,
item3 };

        const string file = "protofile.txt";
        try
        { File.Delete(file); }
        catch
        { };

        using (var fs = File.OpenWrite(file))
        {
            Serializer.Serialize(fs,
    parentListToWrite);
        }

        List<Wrapper> readList;
        using (var fs = File.OpenRead(file))
        {
            readList = Serializer.Deserialize<List<Wrapper>>(fs);
        }

        if (readList[0].Item == readList[2].Item.BaseItem)
        {
            //how to make it equal?
            System.Console.WriteLine("eq");
        }
        if (readList[0].Item == readList[1].Item.BaseItem)
        {
            //how to make it equal?
            System.Console.WriteLine("eq");
        }
    }
}


On 9 June 2014 18:12, Jan Kowalski <tramwaj...@gmail.com> wrote:

> I have a simple class with reference to parent object. All objects are in
> one list (even parent objects). Is it possible to keep references
> references after deserialization?
>
> In my code I have something like this:
>
>     [ProtoContract]
>     public class ProtoItem
>     {
>         [ProtoMember(1)]
>         public int Value { get; set; }
>
>         [ProtoMember(2, AsReference = true)]
>         public ProtoItem BaseItem { get; set; }
>     }
>
> And I use it like that:
>
>
>  static void Main()
>         {
>             var itemParent = new ProtoItem { Value = 1 };
>             var item2 = new ProtoItem { Value = 2, BaseItem = itemParent };
>             var item3 = new ProtoItem { Value = 3, BaseItem = itemParent };
>
>             var parentListToWrite = new List<ProtoItem> {itemParent, item2, 
> item3};
>
>             const string file = "protofile.txt";
>             try { File.Delete(file); }
>             catch { };
>
>             using (var fs = File.OpenWrite(file)) { Serializer.Serialize(fs,
>                  parentListToWrite); }
>
>         List<ProtoItem> readList;
>         using (var fs = File.OpenRead(file)) { readList =
>             Serializer.Deserialize<List<ProtoItem>>(fs); }
>
>         if (readList[0] == readList[2].BaseItem)
>         {
>             //how to make it equal?
>         }
>         if (readList[0] == readList[1].BaseItem)
>         {
>             //how to make it equal?
>         }
>     }
>
>
> Is that possible to deserialize it that last two equations work?
> jan.
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to protobuf+unsubscr...@googlegroups.com.
> To post to this group, send email to protobuf@googlegroups.com.
> Visit this group at http://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Regards,

Marc

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to