什么时候DataContractJsonSerializer包括类型的信息? [英] when does DataContractJsonSerializer include the type information?

查看:208
本文介绍了什么时候DataContractJsonSerializer包括类型的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,与DataContractJsonSerializer序列化的JSON字符串有时也包括形式

I noticed that with the DataContractJsonSerializer the serialized JSON string sometimes include the type information in the form of

{"__type":"MyClass:#MyNamespace", ... }

根据我的观察,似乎只有做到这一点时,它的序列化基本类型,但一个子类型传递,而不是这是有道理的,但我还没有发现任何官方单证,确认此或者实际上反正强制串行一贯表现每当遇到一个自定义类型的这种行为。

based on my observations it seems to only do this when it's serializing a base type but a known sub type is passed instead which makes sense, but I haven't found any official documentations to confirm this or indeed anyway to force the serializer to consistently exhibit this behaviour whenever it encounters a custom type.

任何人都可以确认,如果我的观察是正确的?更重要的是,如果你知道的一种方式来指示串行总是序列化的类型信息自定义类型,如果这是在所有可能的?

Can anyone confirm if my observations are correct? Better yet, if you know of a way to instruct the serializer to always serialize the type information for custom types, if this is at all possible?

谢谢

推荐答案

其实,你可以强制序列化始终​​一贯地发出__type信息。

Actually, you CAN force the serializer to always consistently emit __type information.

如果您使用的是串行独立的,使用构造函数重载,是以alwaysEmitTypeInformation的说法。传入true。

If you are using the serializer standalone, use the constructor overload that takes the "alwaysEmitTypeInformation" argument. Pass in true.

如果您使用的是DataContractJsonSerializerOperationBehavior,该属性设置为true相同的属性。我相信你也可以通过配置做到这一点。

If you are using DataContractJsonSerializerOperationBehavior, set the same property on that attribute to true. I believe you can also do it via config.

最后,请参见当类型提示发射的在<一个href="http://msdn.microsoft.com/en-us/library/bb412170.aspx">http://msdn.microsoft.com/en-us/library/bb412170.aspx对于更详细的解释。

Finally, see the section "When Are Type Hints Emitted" at http://msdn.microsoft.com/en-us/library/bb412170.aspx for a more detailed explanation.

希望这有助于!

这篇关于什么时候DataContractJsonSerializer包括类型的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆