Created
December 9, 2010 10:57
-
-
Save scottlittlewood/734602 to your computer and use it in GitHub Desktop.
OpenRasta Json Codec using the Newtonsoft.Json library
This file contains hidden or 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
[MediaType("application/json;q=0.5", "json")] | |
public class NewtonsoftJsonCodec : IMediaTypeReader, IMediaTypeWriter | |
{ | |
public object Configuration { get; set; } | |
public object ReadFrom(IHttpEntity request, IType destinationType, string destinationName) | |
{ | |
using(var streamReader = new StreamReader(request.Stream)) | |
{ | |
var ser = new JsonSerializer(); | |
return ser.Deserialize(streamReader, destinationType.StaticType); | |
} | |
} | |
public void WriteTo(object entity, IHttpEntity response, string[] parameters) | |
{ | |
if (entity == null) | |
return; | |
using (var textWriter = new StreamWriter(response.Stream)) | |
{ | |
var serializer = new JsonSerializer(); | |
serializer.Serialize(textWriter, entity); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment