When does a DataContractJsonSerializer include type information?

I noticed that with a DataContractJsonSerializer, a serialized JSON string sometimes includes type information in the form

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

according to my observations, it seems that this happens only when serialization of the base type is passed instead, but to the known sub type, which makes sense, but I did not find official documentation to confirm this, or even in any case, to force the serializer to consistently manifest this behavior whenever it encounters a custom type.

Can anyone confirm the correctness of my observations? Better yet, if you know how to instruct the serializer to always serialize type information for custom types, if at all possible?

Thanks,

+8
json serialization wcf
source share
1 answer

In fact, you MAY force the serializer to always generate __type information sequentially.

If you are using a standalone serializer, use constructor overload , which takes the alwaysEmitTypeInformation argument. Go to true.

If you are using DataContractJsonSerializerOperationBehavior, set this attribute to true for this attribute. I believe you can also do this through config.

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

Hope this helps!

+14
source share

All Articles