Deserialization based on __type

Jan 30, 2010 at 1:47 AM

It is possible to deserialize an object based on a "__type" property? something like Asp.Net Ajax  does, but more: all of object's properties that are complex objects to be deserialized based on a "__type" property.

My problem is that for some properties I don't know exactly what type will come from the client; for example:

class Data



Animal myAnimal; // here can be a Horse type, a Dog type, etc, all deriving from the base Animal type.



Jan 30, 2010 at 2:10 AM

Just found the solution: TypeNameHandling

Jan 30, 2010 at 3:57 PM

I had a question about the same functionality.  It looks like the type resolution is hard coded in the JsonSerializerInternalWriter and JsonSerializerInternalReader to use the fully qualified assembly name.  Without patching these two classes to use a strategy pattern for type name resolution, is there an existing extension point that can be used to change this behavior?  I'd like to use namespaces instead so I can match the behavior of the DataContractSerializer.



Jan 31, 2010 at 3:59 AM

What do you mean by namespaces like DataContractSerializer? How is that different/better?

Jan 31, 2010 at 3:12 PM


Here's the link to the docs:  It starts under the header "Polymorphism Preserving Type Information".  

IMO, it is better because 1) you don't have to expose the fully qualified assembly name to clients 2) less characters to represent the type informaton.

Feb 2, 2010 at 8:42 AM

That only works because types are registered with the serializer as "KnownTypes" before deserializing. I definitely don't want people have to do that to get type preserving to work.

Feb 8, 2010 at 2:24 PM

I certainly understand that aspect.  Is there any concern in exposing this as a pluggable strategy though with the defaults staying as is?