System.Text.Json.Serialization Does not appear to work for JSON with NESTED classes

Viewed 1481

How do I define a class that works using only System.Text.Json.Serialization?

Using Microsoft's new alternative to Newtonsoft deserialization does not currently work for nested classes, as all the properties are set to null when the JSON file is deserialized. Using Newtosonsoft's Json property attribute [JsonProperty("Property1")] maintains the properties' value.

Thanks!

public class Class1
{
    [JsonProperty("Property1")]
    public string Property1 { get; set; }
}
    

Pasting using visual studio's paste JSON to class to create a class:

public class Rootobject
{
    public string Database { get; set; }
    public Configuration Configuration { get; set; }
}

public class Configuration
{
    public Class1 Class1 { get; set; }
    public Class2 Class2 { get; set; }
    public Class3 Class3 { get; set; }
}

public class Class1
{
    public string Property1 { get; set; }
}

public class Class2
{
    public string Property2 { get; set; }
}

public class Class3
{
    public string Property3 { get; set; }
}

Issue is when using System.Text.Json.Serialization the nested class's properties are set to null.

{
  "property1": null
}

csharp2json

Using Newtonsoft deserializer works with [JsonProperty("Configuration")]

public class Class1
{
    [JsonProperty("Property1")]
    public string Property1 { get; set; }
}

public class Class2
{
    [JsonProperty("Property2")]
    public string Property2 { get; set; }
}

public class Class3
{
    [JsonProperty("Property3")]
    public string Property3 { get; set; }
}

public class Configuration
{
    [JsonProperty("Class1")]
    public Class1 Class1 { get; set; }

    [JsonProperty("Class2")]
    public Class2 Class2 { get; set; }

    [JsonProperty("Class3")]
    public Class3 Class3 { get; set; }
}

public class RootObject
{
    [JsonProperty("Database")]
    public string Database { get; set; }

    [JsonProperty("Configuration")]
    public Configuration Configuration { get; set; }
}
1 Answers

It looks like you're mixing the [JsonProperty] attribute from Newtonsoft.Json with System.Text.Json. That won't work.

The equivalent attribute in System.Text.Json is [JsonPropertyName].

You also need to match the case (for System.Text.Json, which is why the properties are evaluated as null). From the docs you referenced:

By default, property name matching is case-sensitive. You can specify case-insensitivity.

So if your json properties look like this:

{
    "property1": "abc"
}

then, your class should look like this:

public class Class1
{
    [JsonPropertyName("property1")]
    public string Property1 { get; set; }
}

Notice that "property1" is lowercase in the JsonPropertyName attribute.

Checkout this online demo.

To specify case-insensitivity you can use PropertyNameCaseInsensitive in the serializer options:

var options = new JsonSerializerOptions
{
    PropertyNameCaseInsensitive = true,
};
var data = JsonSerializer.Deserialize<Root>(json, options);
Related