2

we're using wpf, and would like to serialize a complex object -- a view model.

Using binary formatter, I can just add an attribute [Serializable], and it would automatically work for the entire class, recursively.

Do we have something similar in protobuf?

Also, where is the documentation?

I learned about protoinclude, and protomembers, but these are complex objects that may change.

We want to use protobuf because it is compact, fast, and portable. But I don't rule out other options, if it accomplishes the same goals, more or less, and is easy to use. Please answer or suggest options. Thank you

kobi7
  • 969
  • 1
  • 7
  • 15

1 Answers1

3

BinaryFormatter manages this by including the field name in the output, which is both verbose and brittle (for example, it won't withstand changing something from a field+property to an automatically implemented property).

If you want to do something similar in protobuf-net, you can use "ImplicitFields", however, note that this assigns an integer key to each member alphabetically, so is only suitable if your model is totally fixed as a contract and will not add/rename members as this will break the contract (meaning: you can't deserialize existing data correctly). For example:

[ProtoContract(ImplicitFields = ImplicitFields.AllPublic)]
public class Foo {...} // all public fields and properties are serialized,
                       // similar to XmlSerializer

[ProtoContract(ImplicitFields = ImplicitFields.AllFields)]
public class Bar {...} // all fields (not properties; public or private)
                       // are serialized, similar to BinaryFormatter

If your contract is not totally fixed, it would be preferable to explicitly assign a key to each serialized member, which can be done in a great many ways. The simplest being:

[ProtoContract]
public class Foo {
    [ProtoMember(1)]
    public int A {get;set;}

    ...
} 
Community
  • 1
  • 1
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Many thanks. we are passing the objects in memory, and expect only a single version. we want protobuf for the small size, and the speed. I will try this later today, will the attribute also "discover" subtypes inside, and be able to include them? – kobi7 Nov 21 '11 at 08:13
  • @better-than-soil no; subtypes currently need to be explicit, as there needs to be a robust way to resolve the potential subtypes (and assign a key). You can control this at runtime if preferred (and if *your code* can find the subtypes reliably) - it doesn't necessarily have to use attributes. – Marc Gravell Nov 21 '11 at 08:16
  • Marc, Any news on the other differences on protobuf-net-AsReference vs BinaryFormatter? I saw you made some work on null vs. empty collections (great!), have you had time to look at collections where items can be reference-equal and should remain so after cloning the collection? Otherwise I think @better-than-soil should be aware of that difference before using implicitfields to try use pbnet as a drop in replacement for binaryformatter. – Anders Forsgren Nov 21 '11 at 08:40
  • @Anders there is now optional support for nulls *inside* collections; I need to add more explicit null *outside* collections, then I'll blog on both. Re reference-equal; AsReference will work for the items *in* the collection, but doesn't currently work for the collection instance itself. – Marc Gravell Nov 21 '11 at 09:04
  • Thanks Marc, for the general case consider a list of lists of lists where everything is the same object reference. That does currently then create more than one object during deserialization while I expect the same structure of objects (a single list reference containing itself over and over). Is there an issue number for this in pb-net so I can follow it? – Anders Forsgren Nov 21 '11 at 10:10
  • @Anders have you set `AsReference=true` on the list member? note this is a contract change, so impacts existing data etc – Marc Gravell Nov 21 '11 at 16:19
  • I don't have a concrete example, I was just curious: can I still make protobuf-net stumble on any situation related to collections and references, or is it always possible to create a typemodel that will let an object graph clone identically to binaryfomatter without using shims/surrogates or changing the domain objects? Assume we try to mimic BF and serialize using alphabetical implicit fields and all reference type fields are AsReference=true. – Anders Forsgren Nov 21 '11 at 16:27
  • Marc, I made a new question with example code for a simpler case http://stackoverflow.com/questions/8218355/protobuf-net-null-empty-lists-and-reference-equality – Anders Forsgren Nov 21 '11 at 20:51