Skip to content

Instantly share code, notes, and snippets.

@kppullin
Created November 5, 2011 22:31
Show Gist options
  • Save kppullin/1342121 to your computer and use it in GitHub Desktop.
Save kppullin/1342121 to your computer and use it in GitHub Desktop.
Json.NET Serializer for Nancy
public class JsonNetSerializer : ISerializer
{
private Nancy.Responses.DefaultJsonSerializer _defaultSerializer = new Nancy.Responses.DefaultJsonSerializer();
public bool CanSerialize(string contentType)
{
return _defaultSerializer.CanSerialize(contentType);
}
public void Serialize<TModel>(string contentType, TModel model, Stream outputStream)
{
var serializer = JsonSerializer.Create(new JsonSerializerSettings());
using (var writer = new JsonTextWriter(new StreamWriter(new UnclosableStreamWrapper(outputStream))))
{
serializer.Serialize(writer, model);
writer.Flush();
}
}
}
public class JsonNetStartup : IStartup
{
public IEnumerable<TypeRegistration> TypeRegistrations
{
get { return null; }
}
public IEnumerable<CollectionTypeRegistration> CollectionTypeRegistrations
{
get { yield return new CollectionTypeRegistration(typeof(ISerializer), new List<Type> { typeof(JsonNetSerializer) }); }
}
public IEnumerable<InstanceRegistration> InstanceRegistrations
{
get { return null; }
}
public void Initialize(IPipelines pipelines)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment