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