是否有序列化/反序列化场景 DataContractSerializer 可以处理,而DataContractJsonSerializer 不能?
特别是,我正在考虑循环引用:this MSDN page解释了 DataContractSerializer
如何通过在 DataContractAttribute
构造函数中使用 IsReference = true
来管理循环引用。另一方面,DataContractAttribute.IsReference文档没有明确声明其适用性仅限于 DataContractSerializer
。
DataContractJsonSerializer
是否也支持 IsReference
属性?
最佳答案
没有什么比下午的动手测试更好的了...
在要序列化的类上应用 DataContractAttribute.IsReference = true
时,
[DataContract(IsReference = true)]
public class SerializableClass {
...
}
并尝试使用 DataContractJsonSerializer
对其进行序列化,
var serializer = new DataContractJsonSerializer(typeof(SerializableClass));
serializer.WriteObject(stream, obj);
WriteObject
方法会抛出异常:
System.Runtime.Serialization.SerializationException : The type 'SerializableClass' cannot be serialized to JSON because its IsReference setting is 'True'. The JSON format does not support references because there is no standardized format for representing references. To enable serialization, disable the IsReference setting on the type or an appropriate parent class of the type.
另一方面,如果我使用 DataContractSerializer
序列化同一个对象,序列化(和反序列化)就像一个魅力。
现在,如果有人知道 DataContractJsonSerializer
与 DataContractSerializer
相比有更多限制,我洗耳恭听...
关于c# - DataContractJsonSerializer 可以处理循环引用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19975600/