when does DataContractJsonSerializer include the type information?
I noticed that with the DataContractJsonSerializer the serialized JSON string sometimes include 开发者_C百科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?
Thanks,
Actually, you CAN force the serializer to always consistently emit __type information.
If you are using the serializer standalone, use the constructor overload
that takes the "alwaysEmitTypeInformation" argument. Pass in true.
If you are using DataContractJsonSerializerOperationBehavior, set the same property on that attribute to true. I believe you can also do it via config.
Finally, see the section "When Are Type Hints Emitted" at http://msdn.microsoft.com/en-us/library/bb412170.aspx for a more detailed explanation.
Hope this helps!
精彩评论