Home > .net > Protobuf-Net: Couple of gotchas

Protobuf-Net: Couple of gotchas

Here’re a couple of issues with Protobuf-net that I ran into recently.

  1. C# classes generated with protogen /p:detectMissing flag creates string getters as
    get { return _myStringField ?? ""; }
    This means that string fields that were supposed to be nulls still get sent to the server as an empty string; not an ideal situation.
    The fix is to manually edit the generated class to remove the coalesce from all string getters.
     get { return _myStringField; }
  2. If you try to serialize a class where you do not populate an optional enum with no enum value defined for zero (0), you’ll get an exception “The default enum value is not defined for the optional property”. This happens even though Google spec says that in such cases, the 1st value should be used; although, in my opinion, Protobuf-Net should check whether to serialize the enum at all before attempting to get to the valid enum values.
    For example, this enum will throw the aforementioned exception:
    public enum MyEnum {
       MyEnumValueA = 10,
       MyEnumValueB = 100

    Unfortunately, there’re no workarounds for this problem at the moment except adding a “MyEnumValueUnknown=0” to the enum.

The author, Marc Gravell, is aware of these issues. Here’s hoping he’ll get a chance to fix these problems quickly.

Categories: .net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: