The class JsonSerializerOptions has the following property:
public IList<Serialization.JsonConverter> Converters { get; }
This code works fine, as expected:
using System.Text.Json.Serialization;
var options = new JsonSerializerOptions();
options.Converters.Add(new JsonStringEnumConverter());
This code fails, as expected:
var options = new JsonSerializerOptions
{
Converters = new List<JsonConverter> { new JsonStringEnumConverter() }
};
// `Property or indexer 'JsonSerializerOptions.Converters' cannot be assigned to -- it is read only`
So far, so good. But this code also works fine, to my surprise:
var options = new JsonSerializerOptions
{
Converters = { new JsonStringEnumConverter() }
};
What exactly is happening in that last example? I might be having a mental lapse, but I don't think I've encountered such syntax before. Can someone point out where it is documented? And I don't understand why it's able to circumvent the property's lack of set or init.
Side note: the code I'm asking about was based on this article.