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