xeraphim
xeraphim

Reputation: 4645

Nullpointer when deserializing json to a list

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
        });
    }
}

Upvotes: 1

Views: 176

Answers (1)

haldo
haldo

Reputation: 16711

It looks like you're mixing Newtonsoft.Json and System.Text.Json.

JsonProperty is used in Newtonsoft.

JsonPropertyName is the equivalent in System.Text.Json.

These attributes are not interchangeable between Newtonsoft.Json and System.Text.Json. Try updating your class to the below:

public class ReportVersandLogDto
{
    [JsonPropertyName("anzahlArtikel")]
    public long AnzahlArtikel { get; set; }

    [JsonPropertyName("betreff")]
    public string Betreff { get; set; }

    [JsonPropertyName("hasBeenRead")]
    public bool HasBeenRead { get; set; }

    [JsonPropertyName("reportId")]
    public long ReportId { get; set; }

    [JsonPropertyName("reportVersandLogId")]
    public long ReportVersandLogId { get; set; }

    [JsonPropertyName("versendetAm")]
    public string VersendetAm { get; set; }

    [JsonPropertyName("link")]
    public string Link { get; set; }
}

Also, there's no need to declare the strings as nullable reference (string?) types unless you're using them in a nullable context.

Please see this fiddle.

Upvotes: 2

Related Questions