-
Notifications
You must be signed in to change notification settings - Fork 128
Closed
Labels
Description
For (de)serialization cases, like in System.Text.Json
, we need a way to tell the linker to keep the properties of type T
, and also the properties of any types referenced by T
's properties recursively.
Take the example in https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to:
public class WeatherForecast
{
public DateTimeOffset Date { get; set; }
public int TemperatureCelsius { get; set; }
public string Summary { get; set; }
public string SummaryField;
public IList<DateTimeOffset> DatesAvailable { get; set; }
public Dictionary<string, HighLowTemps> TemperatureRanges { get; set; }
public string[] SummaryWords { get; set; }
}
public class HighLowTemps
{
public int High { get; set; }
public int Low { get; set; }
}
...
jsonString = File.ReadAllText(fileName);
weatherForecast = JsonSerializer.Deserialize<WeatherForecast>(jsonString);
Note that the properties of HighLowTemps
need to be preserved even though I didn't directly pass HighLowTemps
into JsonSerializer.Deserialize
. Also note that when walking the properties recursively, we need to handle generic collections: Dictionary<string, HighLowTemps>
.