Suppose I have the json payload sent from the client side as following
{"Number": 2, "number": 4}
Over on the server side, I have this model class.
public class Arg
{
public int Number { get; set; }
}
The payload is deserialised in my controller like so:
Request.Content.ReadAsAsync<Arg>();
Why is Arg.Number
== 4? How can I make ReadAsAsync
case sensitive?
Deserialization done via Json.NET. I am digging into process and end up with next code:
public JsonProperty GetClosestMatchProperty(string propertyName)
{
JsonProperty property = GetProperty(propertyName, StringComparison.Ordinal);
if (property == null)
property = GetProperty(propertyName, StringComparison.OrdinalIgnoreCase);
return property;
}
So as you can see, if it fails to get property using String.Ordinal
comparer, it will try String.OrdinalIgnoreCase
, and thats why it overrides your value.
For name I see only one solution to add dummy property to catch this value:
public class Arg
{
[JsonProperty("Number")]
public int Number { get; set; }
[JsonProperty("number")]
public int SmallNumber { get; set; }
}
You need to add a new property in your custom deserializer
public class Arg
{
public int Number { get; set; }
public int number { get; set; }
}
Tested using newtonsoft
Trying to defining your class as wont work
public class Arg
{
[JsonProperty("Number")]
public int Number { get; set; }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.