1

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
        });
    }
}
5
  • 1
    Please provide minimal reproducible example that we can copy and paste into console app. Commented Nov 6, 2019 at 8:41
  • JsonSerializer.Deserialize() does not throw a NullReferenceException. Create a minimal reproducible example. Commented Nov 6, 2019 at 8:44
  • 1
    Not valid JSON. dotnetfiddle.net/ZkNwnS jsonlint.com Commented Nov 6, 2019 at 9:09
  • Have you tried dotnetfiddle.net/XHZnD2 instead? Commented Nov 6, 2019 at 9:12
  • In your example, you have made your JSON a JSON string. You have too many quotes. Commented Nov 6, 2019 at 9:32

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.