问题
I've recently upgraded my project to use JSON.Net version 7.0.1 (was previously using version 6.0.3) and I've run into an issue where my models are no longer being populated with the values from the camel cased JSON source in the http response.
Here's an example of the model I'm using:
public class FooModel
{
public string Bar { get; set; }
public int Id { get; set; }
}
In this app, I'm creating my http request on the server side and requesting content from a built-in web api controller. My web api method looks like:
public class TestApiController : ApiController
{
// GET api/<controller>
public HttpResponseMessage Get()
{
return Request.CreateResponse(HttpStatusCode.OK, new FooModel
{
Bar = "Lorem ipsum",
Id = 123456
});
}
}
And my server side code making the call to this web api is set up as follows:
var client = new HttpClient();
client.BaseAddress = new Uri(string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~/api/")));
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var responseMessage = await client.GetAsync("TestApi").ConfigureAwait(continueOnCapturedContext: false);
if (responseMessage.IsSuccessStatusCode)
return await responseMessage.Content.ReadAsAsync<FooModel>();
Looking at the raw response string in both the old and new versions, the JSON string returned is correct:
{"bar":"Lorem ipsum","id":123456}
The issue I see is that when the response comes back using JSON.Net 6, my model is fully populated with the JSON data:
Bar = "Lorem ipsum"
Id = 123456
When the same code is executed using JSON.Net 7, however, the properties on my model are not populated:
Bar = null
Id = 0
I seem to have narrowed down the issue to something related to the camel casing of the JSON serialization. I have configured my web app to use the built in camel casing option by specifying:
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
What I've noticed is that if I don't include this camel casing designation, the code works as expected. Much of my code is set up to expect this camel casing standard, so it's not an option for me to no longer use this. I could write a custom camel casing formatter, but I'd rather not have to if I don't have to as this works as expected in the previous version of JSON.Net. I've also noticed that if I explicitly use the JSONConverter class to deserialize the raw response, it parses correctly.
var model = Newtonsoft.Json.JsonConvert.DeserializeObject<FooModel>(responseMessage.Content.ReadAsStringAsync().Result)
It seems that it's an issue only with the 'ReadAsAsync' method or something related to that. Again, I use this method throughout my code so I'd rather not have to change everywhere it's being used. Is there a known issue with JSON.Net version 7 related to this camel casing deserialization? Or is there something else that might be causing this issue in JSON.Net v7?
Here is a sample project where this behavior can be observed: https://www.dropbox.com/s/er55ftalfax6y6u/Json.Net%20Test.zip?dl=0
来源:https://stackoverflow.com/questions/34364562/camel-cased-json-net-deserialization-from-web-api-request