Search code examples
c#wcfjsonserializationdatacontractserializer

How to serialize class type but not the namespace to a Json string using DataContractJsonSerializer


I'm trying to serialize a class hierarchy to a Json string using DataContractJsonSerializer, in a WCF service. the default behaviour for serializing a derived class is to add the following key value pair to the object:

"__type":"ClassName:#Namespace"

My problem is that namespaces are long and they bloat the Json string. I would like to somehow intervene with the serialization and output this instead:

"__type":"ClassName"

and on deserialization intervene again to point to the correct namespace (which i know in runtime).

Is there any way to do such a thing?


Solution

  • Adding the namespace parameter to the data contract does the trick. [DataContract(Namespace = "")]