Skip to content

Instantly share code, notes, and snippets.

@theburningmonk
Created September 1, 2011 14:41
Show Gist options
  • Save theburningmonk/1186303 to your computer and use it in GitHub Desktop.
Save theburningmonk/1186303 to your computer and use it in GitHub Desktop.
An implementation of protobuf-net body deserialize to use with Nancy
public sealed class ProtobufNetBodyDeserializer : IBodyDeserializer
{
public bool CanDeserialize(string contentType)
{
return IsProtoBufType(contentType);
}
public object Deserialize(string contentType, Stream bodyStream, BindingContext context)
{
// deserialize the body stream into the destination type
return RuntimeTypeModel.Default.Deserialize(bodyStream, null, context.DestinationType);
}
private static bool IsProtoBufType(string contentType)
{
if (string.IsNullOrWhiteSpace(contentType))
{
return false;
}
var contentMimeType = contentType.Split(';').First();
return contentMimeType.Equals("application/x-protobuf", StringComparison.InvariantCultureIgnoreCase);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment