Created
February 24, 2017 00:26
-
-
Save ksysiekj/ecd8e9208ee0f9226feb55b22be57de2 to your computer and use it in GitHub Desktop.
JilMediaTypeFormatter
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 sealed class JilMediaTypeFormatter : MediaTypeFormatter | |
{ | |
private static readonly MediaTypeHeaderValue _applicationJsonMediaType = new MediaTypeHeaderValue("application/json"); | |
private static readonly MediaTypeHeaderValue _textJsonMediaType = new MediaTypeHeaderValue("text/json"); | |
private static readonly Task<bool> _done = Task.FromResult(true); | |
private static readonly Options _options; | |
static JilMediaTypeFormatter() | |
{ | |
_options = new Options(excludeNulls: true, serializationNameFormat: SerializationNameFormat.CamelCase, | |
unspecifiedDateTimeKindBehavior: UnspecifiedDateTimeKindBehavior.IsLocal, includeInherited: true, | |
dateFormat: DateTimeFormat.ISO8601); | |
} | |
public JilMediaTypeFormatter() | |
{ | |
SupportedMediaTypes.Add(_applicationJsonMediaType); | |
SupportedMediaTypes.Add(_textJsonMediaType); | |
SupportedEncodings.Add(new UTF8Encoding(false, true)); | |
SupportedEncodings.Add(new UnicodeEncoding(false, true, true)); | |
} | |
public override bool CanReadType(Type type) | |
{ | |
return true; | |
} | |
public override bool CanWriteType(Type type) | |
{ | |
return true; | |
} | |
public override Task<object> ReadFromStreamAsync(Type type, Stream input, HttpContent content, IFormatterLogger formatterLogger) | |
{ | |
var reader = new StreamReader(input); | |
var deserialize = TypedDeserializers.GetTyped(type); | |
var result = deserialize(reader, _options); | |
return Task.FromResult(result); | |
} | |
public override Task WriteToStreamAsync(Type type, object value, Stream output, HttpContent content, TransportContext transportContext) | |
{ | |
var writer = new StreamWriter(output); | |
JSON.Serialize(value, writer, _options); | |
writer.Flush(); | |
return _done; | |
} | |
} |
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
static class TypedDeserializers | |
{ | |
private const string DeserializeMethodName = "Deserialize"; | |
private static readonly ConcurrentDictionary<Type, Func<TextReader, Options, object>> _methods; | |
private static readonly MethodInfo _deserializeMethodInfo = typeof(JSON).GetMethod(DeserializeMethodName, new[] { typeof(TextReader), typeof(Options) }); | |
static TypedDeserializers() | |
{ | |
_methods = new ConcurrentDictionary<Type, Func<TextReader, Options, object>>(); | |
} | |
public static Func<TextReader, Options, object> GetTyped(Type type) | |
{ | |
return _methods.GetOrAdd(type, CreateDelegate); | |
} | |
private static Func<TextReader, Options, object> CreateDelegate(Type type) | |
{ | |
return (Func<TextReader, Options, object>)_deserializeMethodInfo | |
.MakeGenericMethod(type) | |
.CreateDelegate(typeof(Func<TextReader, Options, object>)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sure would be nice if you included your namespace references...