我正在使用 Json.net 4.5。我已经使用 Json 对 PatientV1 进行了序列化,并尝试将其除菌到 PatientV2 中。序列化发生得很好。
但是当我引入了验证检查AppUtility.CreatePatientNr
(如果传递了任何空值或空值,则会引发异常),反序列化失败。
我尝试使用派生的 JsonConverter 创建 PatientV2 对象,在该对象中我可以控制将“Proper”值传递给构造函数,但是在创建对象后我将无法设置正确的 PatientNr,因为它是只读的。
我不想使用反射。Json.net 是否提供任何方法来设置patientV2.PatientNr
JsonConverter 中的只读属性?
有什么方法可以忽略这个异常或任何其他方法来创建 PatientV2 对象?
//version 1.0. Legacy code with no Json tag
public class PatientV1
{
public int PatId { get; set; }
}
//Version 2.0
public class PatientV2
{
public PatientV2( string Id, string s1, string s2 )
{
PatientNr = AppUtility.CreatePatientNr( Id, s1, s2);
}
//PatId is renamed to a string type. It now has private set
[JsonProperty( "PatId" )]
public string PatientNr { get; private set; }
}
Edit1: Json 是否有任何特定的反序列化构造函数,就像二进制序列化一样?