How can I exclude type information from a DataContract?

I want to exclude the key and __type value from my serialization:

{"__type":"Entity:#Runtime.DataBus","Children":[],"Fields":[{"Key":"field1","Value":"10"},{"Key":"fieldString1","Value":"field1 init"},{"Key":"fieldString2","Value":"field2 init"}],"Name":"Entity1"} 

I am using System.Runtime.Serialization.Json.DataContractJsonSerializer .

I had to mark types as known types, and this seems to be what pulls __type into serialization.

I do not want this in my object, how can this be done?

Without using the KnownType attribute in a class with a DataContract, it will work, but without it in this class I get all kinds of exceptions for these classes of class classes.

+4
source share
2 answers

I can say definitively that there is no way to do this unless you serialize the type in polymorphic scripts.

One possible solution is to create some kind of shell operation that will not cause polymorphism and return the object through this operation, and not through the poly method.

The JSON serializer has a flag called alwaysEmitTypeInformation, but this is what you enable to always emit __type. Now it is possible to disable it, mainly in order to avoid unintentional user errors.

+2
source

In new versions you can use the following code

 DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings(); settings.EmitTypeInformation = System.Runtime.Serialization.EmitTypeInformation.Never; 
+3
source

All Articles