Deserialization based on __type

Jan 30, 2010 at 2: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 3:10 AM

Just found the solution: TypeNameHandling

Jan 30, 2010 at 4: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.

 

 

Coordinator
Jan 31, 2010 at 4:59 AM

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

Jan 31, 2010 at 4:12 PM

James,

Here's the link to the docs: http://msdn.microsoft.com/en-us/library/bb412170.aspx?ppud=4.  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.

Coordinator
Feb 2, 2010 at 9: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 3: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?