Search code examples
c#protobuf-net

Deserialize a protobuf serialized data after renaming the base ProtoBuf.Net class;


I had the below classes in the old version of my application.

FooBase.cs


[ProtoContract]
[ProtoInclude(1, typeof(Baz)]
[ProtoInclude(3, typeof(Qux)]
public abstract class FooBase
{
     [ProtoMember(5)]
     public int Bar { get; set;}
}

Baz.cs


[ProtoContract]
public class Baz : FooBase
{
    [ProtoMember(1)]
    public string Quux {get;set;}
}

Qux.cs


[ProtoContract]
public class Qux : FooBase
{
    [ProtoMember(1)]
    public string Corge {get;set;}
}

Now we have some data serialized using Protobuf.Net to Bar and Qux

Now, in the next version, we decided to make it possible to serialize to FooBase as well. So, FooBase is no longer an abstract class.

I really want to rename FooBase to Foo The question is, If I rename FooBase to Foo,

Foo.cs


[ProtoContract]
[ProtoInclude(1, typeof(Baz)]
[ProtoInclude(3, typeof(Qux)]
public class Foo
{
     [ProtoMember(5)]
     public int Bar { get; set;}
}

Is it still possible to deserialize my old data to Baz and Qux?

I could not find any answer from the available documentation.


Solution

  • At the binary protocol level, protobuf doesn't talk names; it exclusively talks numbers. If you're using code-first, you can change the names all day long - it won't matter. You can serialize from Foo and deserialize into NewFooFinal3 - fine. Just don't change the numbers.

    (caveat: unless you're using the "dynamic typing" features from v2, that are deprecated in v3)