4
我正在使用NewtonSoft JSON.NET庫來序列化DTOBase可以容納派生實例的以下類。使用JSON.NET序列化/反序列化派生對象而不使用JsonProperty?
public class Command
{
public DTOBase CommandDTO { get; set; }
}
每this article您需要包括JsonProperty屬性,這樣得到的情況下得到適當的反序列化
public class Command
{
[JsonProperty(TypeNameHandling = TypeNameHandling.All)]
public DTOBase CommandDTO { get; set; }
}
的問題是,是否有除了使用屬性來獲得相同的結果任何其他方式?我寧願不加入NewtonSoft庫和json序列化,特別是在課堂級別。有沒有一種方法可以在庫的Serialize/Deserialize方法中指定一些設置以獲得相同的結果?
嗨,詹姆斯,我試過這個,但它沒有工作,序列化的字符串不包含屬性的類型信息,就像它使用JsonProperty屬性時一樣。 v var settings = new JsonSerializerSettings {TypeNameHandling = TypeNameHandling.All}; string serializedInstance = JsonConvert.SerializeObject(myobject,Formatting.None,settings); – 2011-06-07 20:25:23
看起來我有一個老版本的te庫。我升級到最新版本,並且帶有TypeNameHandling.Auto的JsonSerializerSettings按預期工作。 – 2011-06-15 01:05:43