Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JasperFx - MessagePack - Serialization
public class MessagePackMessageDeserializer : IMessageDeserializer
{
public string ContentType { get; private set; }
public Type DotNetType { get; private set; }
public string MessageType { get; private set; }
internal MessagePackMessageDeserializer(Type messageType) : this(messageType, "application/msgpack")
{
}
internal MessagePackMessageDeserializer(Type messageType, string contentType)
{
if (messageType == null)
{
throw new ArgumentNullException(nameof(messageType));
}
if (string.IsNullOrWhiteSpace(contentType))
{
throw new ArgumentNullException(nameof(contentType));
}
ContentType = contentType;
DotNetType = messageType;
MessageType = messageType.ToMessageAlias();
}
public object ReadFromData(byte[] data)
{
return MessagePackSerializer.NonGeneric.Deserialize(DotNetType, data);
}
public Task<T> ReadFromRequest<T>(HttpRequest request)
{
return MessagePackSerializer.DeserializeAsync<T>(request.Body);
}
}
public class MessagePackMessageSerializer : IMessageSerializer
{
public string ContentType { get; private set; }
public Type DotNetType { get; private set; }
internal MessagePackMessageSerializer(Type messageType) : this(messageType, "application/msgpack")
{
}
internal MessagePackMessageSerializer(Type messageType, string contentType)
{
if (messageType == null)
{
throw new ArgumentNullException(nameof(messageType));
}
if (string.IsNullOrWhiteSpace(contentType))
{
throw new ArgumentNullException(nameof(contentType));
}
ContentType = contentType;
DotNetType = messageType;
}
public byte[] Write(object model)
{
return MessagePackSerializer.Serialize(model);
}
public Task WriteToStream(object model, HttpResponse response)
{
return MessagePackSerializer.SerializeAsync(response.Body, model);
}
}
public class MessagePackSerializerFactory : ISerializerFactory
{
public string ContentType => "application/msgpack";
static MessagePackSerializerFactory()
{
MessagePackSerializer.SetDefaultResolver(MessagePack.Resolvers.ContractlessStandardResolver.Instance);
}
public object Deserialize(Stream message)
{
return MessagePackSerializer.Typeless.Deserialize(message);
}
public IMessageDeserializer[] ReadersFor(Type messageType, MediaSelectionMode mode)
{
var deserializers = new IMessageDeserializer[1];
if (mode == MediaSelectionMode.All)
{
deserializers[0] = new MessagePackMessageDeserializer(messageType);
}
else
{
deserializers[0] = VersionedReaderFor(messageType);
}
return deserializers;
}
public IMessageDeserializer VersionedReaderFor(Type incomingType)
{
return new MessagePackMessageDeserializer(incomingType, incomingType.ToContentType("msgpack"));
}
public IMessageSerializer[] WritersFor(Type messageType, MediaSelectionMode mode)
{
var serializers = new IMessageSerializer[1];
if (mode == MediaSelectionMode.All)
{
serializers[0] = new MessagePackMessageSerializer(messageType);
}
else
{
serializers[0] = new MessagePackMessageSerializer(messageType, messageType.ToContentType("msgpack"));
}
return serializers;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.