Why does serialization fail using DataContractSerializer or DataContractJsonSerializer on classes with Serializable attribute?
Exception:
“System.Runtime.Serialization.Json.DataContractJsonSerializer’ cannot
be serialized. Consider marking it with the DataContractAttribute
attribute, and marking all of its members you want serialized with the
DataMemberAttribute attribute. If the type is a collection, consider
marking it with the CollectionDataContractAttribute”
DataContractSerializers works well with DataContract/DataMember attributes, but it also works with classes without those attributes except when there is a Serializable attribute in the class it fails, generating the exception above.
I solved the issue, I have a private property Serializer in my class which returns System.Runtime.Serialization.Json.DataContractJsonSerializer
even though it is private, DataContractJsonSerializer incldue it in serialization. Not just that property but so as private fields. This is how it behaves when you don’t specify the DataContract and DataMember attributes.