You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore } is provided field property is initialized with default value for int type
Actual behavior
Unhandled exception. Newtonsoft.Json.JsonSerializationException: Error converting value {null} to type 'System.Int32'. Path 'field'
Steps to reproduce
using Newtonsoft.Json;publicclassProgram{classA{publicA(intarg){}publicintfield;}publicstaticvoidMain(){varresult= JsonConvert.DeserializeObject<A>("{\"arg\": 1, \"field\": null}",new JsonSerializerSettings {NullValueHandling= NullValueHandling.Ignore });}}
The text was updated successfully, but these errors were encountered:
The NullValueHandling.Ignore only working for Serialize not Deserialize.
I would suggest to use a Nullable int: public int? field;
and judge the value before using: if(field.HasValue)
Source/destination types
Source/destination JSON
Expected behavior
When
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }
is providedfield
property is initialized with default value forint
typeActual behavior
Unhandled exception. Newtonsoft.Json.JsonSerializationException: Error converting value {null} to type 'System.Int32'. Path 'field'
Steps to reproduce
The text was updated successfully, but these errors were encountered: