I'm trying to deserialize a part of a json file that represents this class.
public class Command
{
[JsonRequired]
public string Name { get; set; }
[DefaultValue("Json!")]
public string Text { get; set; }
//[DefaultValue(typeof(Dictionary<string, string>))]
public Dictionary<string, string> Parameters { get; set; } = new Dictionary<string, string>();
}
where two properties are optional: Text and Parameters. I'd like them to be populated with default values.
The problem is that I cannot figure out how to make it work for both of them.
- If I use the
DefaultValueHandling.Populateoption thenTextwill be populated butParametersremainsnull. - If I use
DefaultValueHandling.Ignorethen it'll be the other way around. - If I set
[DefaultValue(typeof(Dictionary<string, string>))]on theParametersproperty it'll crash.
Quesiton: Is there a way to make it work for all properties?
I'd like to have it not-null so that I don't have to check it in other part of the code.
Demo of what I have tried:
void Main()
{
var json = @"
[
{
""Name"": ""Hallo"",
""Text"": ""Json!""
},
{
""Name"": ""Hallo"",
}
]
";
var result = JsonConvert.DeserializeObject<Command[]>(json, new JsonSerializerSettings
{
DefaultValueHandling = DefaultValueHandling.Populate,
ObjectCreationHandling = ObjectCreationHandling.Reuse
});
result.Dump(); // LINQPad
}