Created
November 5, 2011 22:31
-
-
Save kppullin/1342121 to your computer and use it in GitHub Desktop.
Json.NET Serializer for Nancy
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 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