Deserialize from Type
jovball opened this issue · comments
How can I deserialize from a Type? I am looking to integrate Wire into an existing library for Redis. The method I need to use has this signature.
object DeserializeCore(Type type, byte[] value)
I thought this might work but it doesn't.
using (var memStream = new MemoryStream(value))
{
var serializer = new Serializer(new SerializerOptions(true));
var d = serializer.GetDeserializerByType(type);
return d.ReadValue(memStream, new DeserializerSession(serializer));
}
You might want to try something like to the following, which worked for me in the past in a similar scenario:
using (var memStream = new MemoryStream(value))
{
var serializer = new Serializer(new SerializerOptions(true));
return serializer.Deserialize(memStream);
}
Wire doesn't need to know what type it is performing deserialization on typically, since the type information is encoded into the serialized bytes themselves.
A Generic Deserialize method can be finished up by changing the last line to
return serializer.Deserialize(memStream) as T;
@to11mtm is correct, Wire do not need any type information as that is embedded in the stream manifest.