Last active
April 16, 2018 12:05
-
-
Save mikerdean/6f359aba19ecdaabac0db41a8db78f0a to your computer and use it in GitHub Desktop.
JasperFx - MessagePack - Serialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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