Using (JsonTextReader jsonTextReader = new JsonTextReader(streamReader)) Using (StreamReader streamReader = new StreamReader(stream, Encoding)) JsonSerializer serializer = JsonSerializer.Create(_jsonSerializerSettings) Fill out the mediatype and encoding we support _jsonSerializerSettings = jsonSerializerSettings ? new JsonSerializerSettings() Public JsonNetFormatter(JsonSerializerSettings jsonSerializerSettings) Private JsonSerializerSettings _jsonSerializerSettings Public class JsonNetFormatter : MediaTypeFormatter Return new HttpResponseMessage(test) //echoing data var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK) Using (var jsonReader = new JsonTextReader(sr) ).Result Here's my new OnReadFromStreamAsync code. However, this has still not fixed the problem. I changed the OnReadFromStreamAsync method to construct the JsonTextReader with the CloseInput property set to false, similar to the OnWrite method code that you have highlighted probably not a good idea for MediaTypeFormatters to close the input or output The indentation makes no difference to the compiled IL, my OnReadFromStreamAsync is semantically identical (unless Agreed that the OnWriteToStreamAsync method is different, but OnWrite isn't being used in this case OnRead is. Using (var jsonWriter = new JsonTextWriter(new StreamWriter(stream))) Protected override Task OnWriteToStreamAsync(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, FormatterContext formatterContext, TransportContext transportContext) Var result = ser.Deserialize(jsonReader, type) Using (var jsonReader = new JsonTextReader(sr)) Using (var sr = new StreamReader(stream, Encoding)) Var ser = JsonSerializer.Create(_settings) Protected override Task OnReadFromStreamAsync(Type type, Stream stream, HttpContentHeaders contentHeaders, FormatterContext formatterContext) Protected override bool CanWriteType(Type type) Protected override bool CanReadType(Type type) ![]() SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json")) Įncoding = new UTF8Encoding(false, true) _settings = settings ? new JsonSerializerSettings() Public JsonNetFormatter(JsonSerializerSettings settings) Private readonly JsonSerializerSettings _settings Public class JsonNetFormatter: MediaTypeFormatter Has a dependency on Nuget package Newtonsoft.Json 4.0.8 ReadAsAsync() doesn't use my Json formatter if I add it to the Controller's Configuration.Formatters earlier in the request.Įdit: versions! I am using AspNetWebApi.6.16343, AspNetWebApi.Core.6.16343, .6.16343, .6.16343, .6.16343 and System.Json.6.16343įor sure, here it is (pretty much the same code as suggested by by Henrik Neilsen at ![]() So it seems that () does not use the formatters in ? I can also confirm that ![]() With the stack trace suggesting that DataContractJsonSerializer has been used.Īlso, breakpoints at CanReadType() and OnReadFromStreamAsync() in my MyJsonNetFormatter do not fire. In my ApiController action, when I call (Type) with a Type that has DateTime properties which the client has formatted in the JSON.Net way, I get an error along the line of: Message=DateTime content 'T00:00:00' does not start with '/Date(' and end with ')/' as required for JSON. (Incidentally, in most examples I have seen where JSON.Net is used to provide an alternative Json MediaTypeFormatter, this bit of code to ensure it is always used is omitted - in my experience it is required however) Var myJsonNetFormatter = new MyJsonNetFormatter() I have made my JsonNetFormatter the default in through: (This is all within an ASP.NET hosted environment as well.) I am also using an alternative implementation of MediaTypeFormatter for JSON via JSON.Net as most people appear to be in order to have satisfactory date handling. I have a Post() handler that is doing some dynamic deserialization of the request body therefore I am trying to use HttpContent.ReadAsAsync(Type) - where I determine the Type to be deserialized on the basis of other attributes of the request.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |