2

If I have [Serializable] attribute on a class then it causes the resulting serialised Json string to include backing members created by the framework.

For example I get below for my Id field:

<Id>k__BackingField=20001  

I could find many resources on SOF and elsewhere to get around this problem but I couldn't find why Json serializer is behaving differently when it sees [Serializable] attribute.

If the Jason serializer doesn't serialise members and only serialise properties then why does it behave differently when a class is decorated with [Serializable] attribute?

Please note I'm not looking for a way to fix this issue as I have already found that. I would like to know why Newtonsoft.Jsonserialiser behaves differently here.

Menol
  • 1,230
  • 21
  • 35
  • 3
    From [Serialization Guide: Objects](https://www.newtonsoft.com/json/help/html/SerializationGuide.htm#Objects): *Finally, types can be serialized using a fields mode. All fields, both public and private, are serialized and properties are ignored. This can be specified by setting MemberSerialization.Fields on a type with the JsonObjectAttribute or by using the .NET SerializableAttribute and setting IgnoreSerializableAttribute on DefaultContractResolver to false.* – dbc Mar 28 '19 at 16:58
  • 1
    So your code must be setting [`DefaultContractResolver.IgnoreSerializableAttribute = false`](https://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_Serialization_DefaultContractResolver_IgnoreSerializableAttribute.htm) somewhere; it's not the default. Does that answer your question? – dbc Mar 28 '19 at 16:59
  • 1
    Or wait: `k__BackingField=20001` looks like XML not JSON. Are you sure you are using [tag:json.net] at all? Can you share a [mcve]? If you are actually using `DataContractSerializer` you can get these backing fields, see [Is there a way to make DataContractSerializer output cleaner XML?](https://stackoverflow.com/q/1953984/3744182) for why. – dbc Mar 28 '19 at 17:05
  • @dbc the solution I'm working on uses WCF and Web API and like you said setting the IgnoreSerializableAttribute setting had fixed the problem. I asked this question as I couldn't find why Json.net was behaving this way. Thanks to the link you provided (Serialization guide:objects) I understand this now. Thanks. – Menol Mar 28 '19 at 18:17

1 Answers1

0

In case someone wants to find the reason in the future, following explains how objects are serialised by Json.Net:

Breakdown of Type Serialization > Objects

By default a type's properties are serialized in opt-out mode. What that means is that all public fields and properties with getters are automatically serialized to JSON, and fields and properties that shouldn't be serialized are opted-out by placing JsonIgnoreAttribute on them. To serialize private members, the JsonPropertyAttribute can be placed on private fields and properties.

Types can also be serialized using opt-in mode. Only properties and fields that have a JsonPropertyAttribute or DataMemberAttribute on them will be serialized. Opt-in mode for an object is specified by placing the JsonObjectAttribute or DataContractAttribute on the type.

Finally, types can be serialized using a fields mode. All fields, both public and private, are serialized and properties are ignored. This can be specified by setting MemberSerialization.Fields on a type with the JsonObjectAttribute or by using the .NET SerializableAttribute and setting IgnoreSerializableAttribute on DefaultContractResolver to false.

Menol
  • 1,230
  • 21
  • 35