Last active
August 29, 2015 14:08
-
-
Save scottlittlewood/11558b6096ab4fd69d74 to your computer and use it in GitHub Desktop.
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
[MediaType("application/json")] | |
public class JsonFxCodec : IMediaTypeWriter, IMediaTypeReader | |
{ | |
private readonly JsonWriter _writer; | |
private readonly JsonReader _reader; | |
public object Configuration { get; set; } | |
public JsonFxCodec() | |
{ | |
var resolver = new CombinedResolverStrategy( | |
new JsonResolverStrategy(), // simple JSON attributes | |
new DataContractResolverStrategy(), // DataContract attributes | |
new XmlResolverStrategy(), // XmlSerializer attributes | |
new ConventionResolverStrategy(ConventionResolverStrategy.WordCasing.PascalCase), // DotNetStyle | |
new ConventionResolverStrategy(ConventionResolverStrategy.WordCasing.CamelCase), // jsonStyle | |
new ConventionResolverStrategy(ConventionResolverStrategy.WordCasing.Lowercase, "-"), // xml-style | |
new ConventionResolverStrategy(ConventionResolverStrategy.WordCasing.Uppercase, "_")); // CONST_STYLE | |
// pass the combined resolver strategy into the settings object | |
_reader = new JsonReader(new DataReaderSettings(resolver)); | |
// link the settings objects to share resolver strategies and name lookup cache | |
_writer = new JsonWriter(new DataWriterSettings(resolver) {PrettyPrint = true}); | |
} | |
public void WriteTo(object entity, IHttpEntity response, string[] codecParameters) | |
{ | |
using (TextWriter w = new StreamWriter(response.Stream, Encoding.UTF8)) | |
{ | |
_writer.Write(entity, w); | |
} | |
} | |
public object ReadFrom(IHttpEntity request, global::OpenRasta.TypeSystem.IType destinationType, string destinationName) | |
{ | |
using (TextReader r = new StreamReader(request.Stream, Encoding.UTF8)) | |
{ | |
return _reader.Read(r, destinationType.StaticType); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment