Json.Net converted empty strings in ints/floats to 0 if there was an empty string. I'm using type numeric in my inputs, but empty fields will be an empty string in a form post call. Is there a config to convert empty strings to a Null or 0 like Json.net ?
fiddle below. https://dotnetfiddle.net/CDNicW
CodePudding user response:
Just make value nullable
public class Model
{
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
public int? Value { get; set; }
}
the result will be
{ value: null }
CodePudding user response:
add nullable ?
in your class model
public class Model
{
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
public int? Value { get; set; }
}
and change for Newtonsoft.Json.JsonConvert.DeserializeObject in your deserializeObject
Model d = Newtonsoft.Json.JsonConvert.DeserializeObject<Model>(myJson);
Console.WriteLine("Value: " d?.Value);
finally your result is :
Value:
json: {"Value":null}