What I'm doing has already been implemented in Json.net
by setting TypeNameHandling
to TypeNameHandling.Objects
. That way the object type will be serialized as well and the deserialized object will have the exact original type.
However using TypeNameHandling
exposes some security issues and requires us to use a custom SerializationBinder
to limit which types will be supported to avoid possible code injection. That's not the main reason for me trying to find another solution. Actually I find that by using TypeNameHandling.Objects
, an object will be serialized to a complex JSON including not just the object data itself and the object type but also some other properties which look redundant to me.
I think we just need one more property containing info about object type (such as the assembly qualified name of the object type) so I would like to create a custom JsonConverter
which will serialize any object to some JSON like this:
{
"Item" : "normal JSON string of object",
"ItemType" : "assembly qualified name of object type"
}
Isn't that just enough? As I said before, besides 2 properties similar to those (with different names), the Json.net
lib includes some other properties (signature ...) which really look like redundant to me.
I'm not asking for how to implement the custom JsonConverter
I mentioned above. I just wonder if that converter (with a simplified JSON structure) is fine or I should use the standard solution provided by Json.net
with TypeNameHandling
(which involves a more complex JSON structure)? My main concern is with possible performance issues with TypeNameHandling
set to Objects
due to more data to convert/serialize/transfer.
One more concern with the standard solution is performance issue, actually I just need to apply the custom converting logic to all objects of the exact type object
, not to all other strongly typed objects (which may still be unnecessarily applied by TypeNameHandling
?)