Protobuf-net preserves object references?

408 views
Skip to first unread message

jae

unread,
Oct 5, 2009, 8:42:29 PM10/5/09
to Protocol Buffers
Hi all,

I just wonder if protobuf-net preserves object references. From my
experience, when the same object is referenced in two different
places, Protobuf.Serializer writes it twice. I found it's possible to
preserve object references in DataContractSerializer from its
constructor parameter, but I couldn't find any property or parameter
of Protobuf.Serializer that takes care of it. Am I missing something?
or it's not there yet?

Thank you!

Jae

Kenton Varda

unread,
Oct 5, 2009, 9:08:53 PM10/5/09
to jae, Protocol Buffers
The protobuf encoding does not provide any built-in way to represent object references -- all protobuf messages are trees, not graphs.  You can implement references at a higher level, e.g. using integer indexes to refer to other objects in a repeated field, but this is not built-in.

jae

unread,
Oct 5, 2009, 9:11:38 PM10/5/09
to Protocol Buffers
Thank you for the quick response, Kenton!

Marc Gravell

unread,
Oct 6, 2009, 1:31:45 AM10/6/09
to Protocol Buffers
Just to confirm; in common with similar core .NET serializers
(XmlSerializer, DataContractSerializer (ion the default mode, at
least), etc), it serializes as a *tree*, not a full graph. In theory I
*could* do something with identifiers, but it would be very hard to
consume for standard clients - i.e. instead of serializing an object,
I serialize an identifier/object pair (only sending the object if it
has not been seen before); issues:

- would need to keep a lookup of identifiers (could be expensive)
- may need to do fixups during deserialization (or demand that the
identifier cannot be used before it is defined)
- again; not very nice for other clients to consume

That said - I would *consider* testing something, but it would
essentially be an implementation-specific hack. I'd rather stay as
close to the compatible sub-set of features as possible. But (ahem)
since I already hack around inheritance...

Marc Gravell (protobuf-net)
Reply all
Reply to author
Forward
0 new messages