Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing with ProtoBuf.NET without tagging members

I've read somewhere a comment by the author of ProtoBuf.NET that:

There are options to automatically infer the numbers, but that is brittle and not recommended. Only use this if you know you never need to add more members (it orders them alphabetically, so adding a new AardvarkCount will break everything).

This is exactly that sort of situation I am interested in :)

I have something that is akin to a map-reduce scenario where I want to serialize results generated on remote machines using protocol buffers (e.g. the "map" side of map-reduce) and later read them and combine those results for further processing (e.g. the "reduce" side).

I don't want to start an attribute decoration marathon over every possible class I have that might get serialized during this process, and I do find the protocol buffers to be very alluring as I can create result with Mono and consume them effortlessly on MS.NET and vice-versa...

The apparent downsides of not pre-tagging the members doesn't bother me as exactly the same software revision does generation/consumptionn, so I don't need do worry about new members popping up in the code and messing my whole scheme...

So in short, my question is:

  • How do I do it (Serialize with ProtoBuf.NET without tagging/building Meta classes on my own)?
  • Is there any hole in my scheme that I've glaringly missed?
like image 365
damageboy Avatar asked Sep 30 '11 11:09

damageboy


People also ask

How do you serialize an object in Protobuf?

Protobuf is create for serialize purpose so I recommend you no need to serialize it again to a Serializable or Parcelable object. Instead of, serialize it to byte array and put byte array to to bundle.

Can you compress Protobuf?

No it does not; there is no "compression" as such specified in the protobuf spec; however, it does (by default) use "varint encoding" - a variable-length encoding for integer data that means small values use less space; so 0-127 take 1 byte plus the header.

What is Protobuf net in C#?

Introduction. Protobuf-net is a faster . NET library for serialization and deserialization based on Google's Protocol Buffers. It is designed to be a language neutral, platform neutral, extensible way of serializing structured data for use in communications protocols and efficient data storage (far smaller than xml).


1 Answers

If you can live with a single attribute, then the trick is:

    [ProtoContract(ImplicitFields = ImplicitFields.AllPublic)]
    public class WithImplicitFields
    {
        public int X { get; set; }
        public string Y { get; set; }
    }

there are 2 options here; AllPublic works like XmlSerializer - public properties and fields are serialized (using the alphabetic order to choose tag numbers); AllFields works a bit like BinaryFormatter - the fields are serialized (again, alphabetic).

I can't remember if this is yet available on the v2 API; I know it is on my list of things to ensure work! But if you want it in v2 without any attributes, I'm sure I can add an Add(ImplicitFields) overload.

As long as the 2 ends are never out of step, this is fine. If you store the data, or don't version the two ends "in step", then there could be problems. See also the intellisense comments on the enum (which pretty much repeats the warning that you are already aware of).

like image 123
Marc Gravell Avatar answered Oct 14 '22 19:10

Marc Gravell