1

Closed

Add support to add $type only for particular types

description

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 ..hm.. 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 9:57 PM by JamesNK
TypeNameHandling specifies whether it will be present.

comments

estrizhok wrote Dec 25, 2012 at 11:30 PM

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.