I'm trying to deserialize a JSON to a list of ReportVersandLogDtos with System.Text.Json.JsonSerializer
var reportVersandLogsAsync = JsonSerializer.Deserialize<List<ReportVersandLogDto>>(content, new JsonSerializerOptions {
PropertyNameCaseInsensitive = true,
IgnoreNullValues = true
});
Content looks like this:
[
{
"AnzahlArtikel": 6,
"Betreff": "Cupra Daily News",
"ReportId": 379717,
"ReportVersandLogId": 4244138,
"VersendetAm": "2019-11-02T06:30:15.997",
"Link": "foo"
}
]
The ReportVersandLogDto looks like this:
[JsonObject]
public class ReportVersandLogDto : IResource
{
[JsonProperty("anzahlArtikel")]
public long AnzahlArtikel { get; set; }
[JsonProperty("betreff")]
public string? Betreff { get; set; }
[JsonProperty("hasBeenRead")]
public bool HasBeenRead { get; set; }
[JsonProperty("reportId")]
public long ReportId { get; set; }
[JsonProperty("reportVersandLogId")]
public long ReportVersandLogId { get; set; }
[JsonProperty("versendetAm")]
public string? VersendetAm { get; set; }
//[JsonProperty("link")]
//public string? Link { get; set; }
}
Unfortunately, I get a NullPointerException when calling the JsonSerializer.Deserialize method.
Object reference not set to an instance of an object.
I'm not sure what I'm doing wrong... can you please point me in the right direction?
Minimal reproducable example:
(It's a .net core 3.0 console app)
I've already posted the whole ReportVersandLogDto above (it uses attributes from Newtonsoft.Json)
class Program
{
static void Main(string[] args)
{
var content = "\"[{\\\"AnzahlArtikel\\\":6,\\\"Betreff\\\":\\\"Cupra Daily News\\\",\\\"ReportId\\\":379717,\\\"ReportVersandLogId\\\":4244138,\\\"VersendetAm\\\":\\\"2019-11-02T06:30:15.997\\\",\\\"Link\\\":\\\"foo\\\"}]\"";
var reportVersandLogsAsync = JsonSerializer.Deserialize<List<ReportVersandLogDto>>(content, new JsonSerializerOptions {
PropertyNameCaseInsensitive = true,
IgnoreNullValues = true
});
}
}
JsonSerializer.Deserialize()does not throw a NullReferenceException. Create a minimal reproducible example.