Reputation: 1739
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"
DataContractSerializer
s 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.
Upvotes: 1
Views: 1149
Reputation: 1739
I solved the issue, I have a private property Serializer in my class which returns System.Runtime.Serialization.Json.DataContractJsonSerializer
private XmlObjectSerializer Serializer
{
get
{
_serializer = _serializer ?? GetSerializer(GetType());
return _serializer;
}
}
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.
Upvotes: 1
Reputation: 563
I think you don't need to put [Serializable] in POCO class. It will automatically serialize your object even without putting them in your class.
Upvotes: 1