DataContractJsonSerializer:使用接口属性序列化类
我正在尝试序列化这样的类:
[DataContract]
public class GenericFlow
{
[DataMember]
public ISource Source { get; set; }
[DataMember]
public IList<IFilter> Filters { get; set; }
}
当我序列化该对象的实例时,一切正常,但如果我尝试反序列化,则会出现错误。我使用 Mono 2.6 进行测试,这是 mono 平台上的错误:
Stacktrace:
Native stacktrace:
/usr/bin/mono() [0x48563b]
/usr/bin/mono() [0x4d275f]
/lib/libpthread.so.0(+0xfb40) [0x7fd5f8d6eb40]
/usr/bin/mono(mono_object_get_virtual_method+0x174) [0x4f5744]
/usr/bin/mono() [0x555524]
[0x41632228]
Debug info from gdb:
如果我从源属性中删除 DataMember 属性,反序列化就会起作用。
查看序列化过程中生成的 json 字符串,我可以看到 Filters 列表中的每个项目都包含一个“__type”属性,该属性指定了实现 IFilter 的具体类。但对于 ISource 类型的属性“Source”,其中缺少属性“__type”,情况并非如此,因此反序列化过程不知道使用哪个具体类来重新创建该属性。
由于我手边没有 Windows 盒子,我不知道这是否是单声道特定问题,或者也许我在这里遗漏了一些东西。
我应该如何告诉 DataContractJsonSerializer 在源属性中包含“__type”属性?
提前致谢,
更新:
再调查一下,现在我可以强制 DataContractJsonSerializer 始终发出类型信息,并调用alwaysEmitTypeInformation=true 的构造函数。 但问题仍然存在。有什么想法吗?
I am trying to serialize a Class like this:
[DataContract]
public class GenericFlow
{
[DataMember]
public ISource Source { get; set; }
[DataMember]
public IList<IFilter> Filters { get; set; }
}
When i seralize an instance of this object, everything goes right but if i try to deserialize i get an error. I am using Mono 2.6 for my tests, this is the error on the mono platform :
Stacktrace:
Native stacktrace:
/usr/bin/mono() [0x48563b]
/usr/bin/mono() [0x4d275f]
/lib/libpthread.so.0(+0xfb40) [0x7fd5f8d6eb40]
/usr/bin/mono(mono_object_get_virtual_method+0x174) [0x4f5744]
/usr/bin/mono() [0x555524]
[0x41632228]
Debug info from gdb:
If i remove the DataMember Attribute from the Source Property, deserialization works.
Taking a look at the json string produced during serialization, i can see that each item in the Filters list include a "__type" attribute especifing the concrete class that implements IFilter. But this is not true with the property "Source" of type ISource where the property "__type" is missing, so the desarialization process doesnt know which concrete class use to recreate the property.
Since i dont have a windows box handy, i dont know if this is mono specific problem or maybe i am missing something here.
How should i tell DataContractJsonSerializer to include the "__type" attribute in the Source Property?
Thanks in advance,
UPDATE:
Investigating a bit more, now i can force DataContractJsonSerializer to always emit type information calling the constructor with alwaysEmitTypeInformation=true.
But the problem persists. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据我使用 DataContractJsonSerializer 和反序列化复杂对象(在 Windows 上)的经验,您需要包含 __type 提示才能使反序列化正常工作。
我的 DataContractJsonSerializer 经验是将 JSON 发送到 WCF 服务
In my expereince with DataContractJsonSerializer and de-serializing complex objects (on windows) you need to include the __type hints in order for the de-serialization to work properly.
My DataContractJsonSerializer experience has been sending JSON to a WCF service