How to embed type information for de-/serialization purposes using protobuf-net?
I'd like to be able to serialize concrete instances of IMessage in such a way that the type information is retained/embedded (akin to what's available in e.g. Json.NET), so that upon deserialization that type information can be used to materialize those concrete instances. I'm well aware that the de-/serialization methods below don't work. Any guidance would be appreciated on how to change them so they do work.
public interface IMessage {}
public interface IEvent : IMessage {}
[ProtoContract]
public class DogBarkedEvent : IEvent {
[ProtoMember(0)]
public string NameOfDog { get; set; }
[ProtoMember(1)]
public int Times { get; set; }
}
//Somewhere in a class far, far away
public byte[] Serialize(IMessage message) {
using(var stream = new MemoryStream()) {
ProtoBuf.Serializer.Serialize<IMessage>(stream, message);
return stream.ToArray();
}
}
public IMessage Deserialize(byte[] data) {
using(var stream = new MemoryStream(data)) {
return ProtoBuf.Serializer.Deserialize<IMessage>(stream);
}
}
To shed a little light: The serialized events get written to persistence. When reading them, u开发者_开发知识库sage of a deserialization method with a generic argument is not a viable option (the best that can be done is specifying the type information as a regular parameter or using the common contract, IMessage in this case).
There are two ways of approaching this; my least preferred option is to use DynamicType=true
- this is more expensive and limits portability/versioning, but places no demands on knowing the data up-front. My preferred option is to declare a fixed identifier per interface, allowing it to recognise the data itself. This is shown below.
For info, DontAskWrapper
is because Serialize()
uses GetType()
; which means it won't spot the interface base. I suspect I can improve that, but this works for today on v2:
using System.Diagnostics;
using System.IO;
using NUnit.Framework;
using ProtoBuf;
using ProtoBuf.Meta;
namespace Examples.Issues
{
[TestFixture]
public class SO7078615
{
[ProtoContract] // treat the interface as a contract
// since protobuf-net *by default* doesn't know about type metadata, need to use some clue
[ProtoInclude(1, typeof(DogBarkedEvent))]
// other concrete messages here; note these can also be defined at runtime - nothing *needs*
// to use attributes
public interface IMessage { }
public interface IEvent : IMessage { }
[ProtoContract] // removed (InferTagFromName = true) - since you are already qualifying your tags
public class DogBarkedEvent : IEvent
{
[ProtoMember(1)] // .proto tags are 1-based; blame google ;p
public string NameOfDog { get; set; }
[ProtoMember(2)]
public int Times { get; set; }
}
[ProtoContract]
class DontAskWrapper
{
[ProtoMember(1)]
public IMessage Message { get; set; }
}
[Test]
public void RoundTripAnUnknownMessage()
{
IMessage msg = new DogBarkedEvent
{
NameOfDog = "Woofy", Times = 5
}, copy;
var model = TypeModel.Create(); // could also use the default model, but
using(var ms = new MemoryStream()) // separation makes life easy for my tests
{
var tmp = new DontAskWrapper {Message = msg};
model.Serialize(ms, tmp);
ms.Position = 0;
string hex = Program.GetByteString(ms.ToArray());
Debug.WriteLine(hex);
var wrapper = (DontAskWrapper)model.Deserialize(ms, null, typeof(DontAskWrapper));
copy = wrapper.Message;
}
// check the data is all there
Assert.IsInstanceOfType(typeof(DogBarkedEvent), copy);
var typed = (DogBarkedEvent)copy;
var orig = (DogBarkedEvent)msg;
Assert.AreEqual(orig.Times, typed.Times);
Assert.AreEqual(orig.NameOfDog, typed.NameOfDog);
}
}
}
And here's the same thing without attributes:
public interface IMessage { }
public interface IEvent : IMessage { }
public class DogBarkedEvent : IEvent
{
public string NameOfDog { get; set; }
public int Times { get; set; }
}
class DontAskWrapper
{
public IMessage Message { get; set; }
}
[Test]
public void RoundTripAnUnknownMessage()
{
IMessage msg = new DogBarkedEvent
{
NameOfDog = "Woofy",
Times = 5
}, copy;
var model = TypeModel.Create();
model.Add(typeof (DogBarkedEvent), false).Add("NameOfDog", "Times");
model.Add(typeof (IMessage), false).AddSubType(1, typeof (DogBarkedEvent));
model.Add(typeof (DontAskWrapper), false).Add("Message");
using (var ms = new MemoryStream())
{
var tmp = new DontAskWrapper { Message = msg };
model.Serialize(ms, tmp);
ms.Position = 0;
string hex = Program.GetByteString(ms.ToArray());
Debug.WriteLine(hex);
var wrapper = (DontAskWrapper)model.Deserialize(ms, null, typeof(DontAskWrapper));
copy = wrapper.Message;
}
// check the data is all there
Assert.IsInstanceOfType(typeof(DogBarkedEvent), copy);
var typed = (DogBarkedEvent)copy;
var orig = (DogBarkedEvent)msg;
Assert.AreEqual(orig.Times, typed.Times);
Assert.AreEqual(orig.NameOfDog, typed.NameOfDog);
}
Note that in both cases the TypeModel
should be cached and re-used; it is thread-safe, so can be aggressively used in parallel by different threads, etc.
精彩评论