Dakito
Dakito

Reputation: 387

Can I deserialize proto to object type

It is known that protobuf-net is not created for dynamic procession of data, but if serialized object to proto file, can I deserialize it again to object and use it as a dynamic.

As an example:

object p = new
{
    Value = "Test"
};

//Saving object to file works perfectly
using (var file = File.Create("test.bin"))
{
    Serializer.Serialize(file, p);
}

//But this doesn't
using (var file = File.OpenRead("test.bin"))
{
    dynamic data = Serializer.Deserialize<object>(file);
    Console.WriteLine(data.Id);
}

Is there any way to deserialize proto to object type?

Upvotes: 2

Views: 476

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062855

Right now: no. Partly because it hasn't been necessary, and partly because .proto is ambiguous without additional information. That information is usually provided either by a .proto schema file, or (in the case of protobuf-net) via code attributes. Without that context there are a lot of values that cannot be correctly interpreted.

Adding true dynamic support is on the wish list of things that may warrant investigation, but as per the above: if you don't have a Type, you'll need to provide a message descriptor instead (this could be compiled as .proto, or as text, hypothetically - since protobuf-net has a fully managed schema parser).

If you do have a Type but just done have a <T>, then note that protobuf-net also has a full non-generic API that accepts Type as an input.

Upvotes: 2

Related Questions