When running the below example, I get the error
System.InvalidOperationException: JsonPropertyInfo 'test' defined in type 'Test.Config' is marked required but does not specify a setter.
If I don't include the JsonRequired
attribute on the parameter, then it does not produce the error but then I get a default instantiation of my config object instead of one deserialized from my file
This happens only when running on Linux. On Windows it deserializes fine.
public class Config
{
[JsonRequired]
public int test { get; set; } = 1;
}
JsonSerializerOptions options = new JsonSerializerOptions();
using (StreamReader reader = new StreamReader(filePath)){
config = JsonSerializer.Deserialize<Config>(reader.BaseStream, options);
}
JSON file:
{
"test": 2
}