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?
If you can live with a single attribute, then the trick is:
there are 2 options here;
AllPublicworks likeXmlSerializer– public properties and fields are serialized (using the alphabetic order to choose tag numbers);AllFieldsworks a bit likeBinaryFormatter– 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).