This project has moved. For the latest updates, please go here.


Add support to add $type only for particular types


I was looking for a way to add type information to serialized object tree, but only to objects of particular types, which will be treated specially on a client side. I don't want to add $type to every object, because it will significantly increase json size.

I was expecting that it would be possible to achieve by decorating the default IContractResolver and setting some special property indicating that $type property is required. Turned out that a contract cannot tell that a $type property must be added, it can only tell that such property should be added to serialized values of its properties. Since JsonSerializerInternalWriter is internal, I cannot modify the decision making process on whether or not $type property should be written.

Of course, one can always implement a custom JsonConverter for types he\she wants, and write $type property to the result manually, but that's somewhat hacky since (a) '$type' string is not exposed as public, (b) it seems almost impossible to just add a property for a type without overriding whole serialization of that type.

So, could please add a support that would make this scenario possible?
Closed Dec 23, 2012 at 10:57 PM by JamesNK
TypeNameHandling specifies whether it will be present.


estrizhok wrote Dec 26, 2012 at 12:30 AM

Not true. TypeNameHandling specifies whether it will be present for ALL objects of ANY type (when specified on settings object), or for ALL properties of a CERTAIN type (if specified in JsonObject attribute decorating that type). There is no way to add $type property only to certain types.

wrote Feb 22, 2013 at 2:47 AM

wrote May 16, 2013 at 1:36 PM