想知道是否可以拥有私有构造函数并使用新的 System.Text.Json 序列化器。
public class MyModel
{
public string Name { get; set; }
public string Data { get; set; }
private MyModel()
{
// use me for when deserializing
}
public MyModel(string name, string data)
{
Name = name;
Data = data;
}
}
简单的往返。
var model = new MyModel("doo", "doo");
var json = JsonSerializer.Serialize(model, new JsonSerializerOptions
{
WriteIndented = true
});
// no to go because of there is no parameterless constructor defined for this object.
var rehydrated = JsonSerializer.Deserialize<MyModel>(json);
互换的青春
慕容3067478
海绵宝宝撒