Is it possible to adjust JsonSerializerSettings that way?
(I guess not) but still have some hope, cause I'm not very experienced with their API.
By default and with all settings I've tried missing int either deserialized to 0 or causing exception.
Example
{
"defaultCaptionLineNum": 6,
"worksheets": {
"0": { "name": "Ws1", "caption_ln": 6 },
"1": { "name": "Ws2", "caption_ln": null },
"2": { "name": "Ws3" },
"3": { "name": "Ws4", "caption_ln": 5 },
}
}
Currently by default caption line (caption_ln) of Ws3 evaluated to 0, I want it to be evaluated to null (as in Ws2).
I'm using
jObject.ToObject<MyClass>(JsonSerializer.Create(settings));
to get object with worksheet(Ws) information from JSON (tried it without serializer as well)
and any variations of Include
and Ignore
here don't make jObject deserialize missing ints in json differently (with the exception of throwing error right away for missing values)
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Include,
MissingMemberHandling = MissingMemberHandling.Ignore
};
Other serializer settings seem to be irrelevant.
UPD: Listing of MyObject MyClass.
public class MyClass
{
public class WorkSheet
{
[JsonProperty("name")]
string wsName;
[JsonProperty("caption_ln")]
int? captionLineNumber;
public WorkSheet(string name, int captionln)
{
wsName = name;
captionLineNumber = captionln;
}
}
private int _defaultCaptionLineNum;
[JsonProperty("defaultCaptionLineNum")]
public int DefaultCaptionLineNum { get => _defaultCaptionLineNum; }
private Dictionary<int, WorkSheet> _worksheets = new Dictionary<int, WorkSheet>();
[JsonProperty("worksheets")]
public Dictionary<int, WorkSheet> Worksheets { get => _worksheets; set => _worksheets = value; }
public MyClass()
{
Console.WriteLine("New MyClass object created!");
}
}