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.
DefaultValueHandling.Populate
option then Text
will be populated but Parameters
remains null
. DefaultValueHandling.Ignore
then it'll be the other way around. [DefaultValue(typeof(Dictionary<string, string>))]
on the Parameters
property 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
}
Right-click the control that you want to change, and then click Properties or press F4. Click the All tab in the property sheet, locate the Default Value property, and then enter your default value. Press CTRL+S to save your changes.
The defaultValue property sets or returns the default value of a text field. Note: The default value is the value specified in the HTML value attribute.
To assign default values to a selection criterion, you use the following syntax: SELECT-OPTIONS seltab FOR f DEFAULT g [TO h ].... Default values g and h can be literals or field names. You can only use fields that contain a value when the program is started.
Instead of specifying a global DefaultValueHandling
via settings, use [JsonProperty]
attributes to set the DefaultValueHandling
as you need it for each individual property:
public class Command
{
[JsonRequired]
public string Name { get; set; }
[DefaultValue("Json!")]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
public string Text { get; set; }
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public Dictionary<string, string> Parameters { get; set; } = new Dictionary<string, string>();
}
Then, deserialize like this:
var result = JsonConvert.DeserializeObject<Command[]>(json);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With