Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataContractSerializer: How to serialize classes/members without DataContract/DataMember attributes

DataContractSerializer requires classes and members to be marked with the DataContract and DataMember attributes. However, in my case the classes are auto-generated with the EFPocoAdapater framework and these attributes are not present.

How can I force serialization of all members using the DataContractSerializer without these attributes being present?

From Alexdej:

This changed in 3.5SP1, hope you saw that: http://www.pluralsight.com/community/blogs/aaron/archive/2008/05/13/50934.aspx

like image 440
aleemb Avatar asked Apr 24 '09 04:04

aleemb


People also ask

Is DataMember attribute required?

DataMember attribute is not mandatory to add to serialize data. When DataMember attribute is not added, old XMLSerializer serializes the data. Adding a DataMember provides useful properties like order, name, isrequired which cannot be used otherwise.

Which attribute specifies that a public property of a class is capable of being serialized?

Applying the DataMemberAttribute to a field or property explicitly specifies that the member value will be serialized. In contrast, the BinaryFormatter serializes public and private fields of a type, and the XmlSerializer serializes only public fields and properties of a type.

What is DataContract serialization?

DataContractSerializer(Type, IEnumerable<Type>) Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type, and a collection of known types that may be present in the object graph.

What is DataContract and DataMember?

A datacontract is a formal agreement between a client and service that abstractly describes the data to be exchanged. In WCF, the most common way of serialization is to make the type with the datacontract attribute and each member as datamember. Creating a basic DataContract and DataMember.


1 Answers

You cannot - plain and simple. The attribute are needed for the DataContractSerializer to pick up which elements to serialize. In contract to the XmlSerializer, which basically just serializes everything (unless you explicitly tell it to ignore it), the DataContractSerializer is "opt-in" - you have to explicitly tell it (by means of the attributes) which fields and/or properties to serialize.

UPDATE: As several folks have pointed out, with .NET 3.5 SP1, Microsoft loosened those rules up a bit - any public read/write property will be serialized automatically by the DataContractSerializer. At the same time, your class also needs to have a parameterless default constructor - sounds like the exact requirements we had for XmlSerializer way back when....

Of course, this:

  • doesn't allow you to serialize something private - if you want to serialize it, you have to expose it as public read/write property
  • doesn't allow you to specify a defined chosen ordering of the parameters - it will just use them in the order they appear in the class
  • now requires you to have a parameterless constructor in your class again for deserialization

I still think these thing ought to be explicit and clear, making those no longer required opens up the path for lazy/sloppy programming - I don't like it. But if you want to, you can use it now without explicitly marking your properties with [DataMember].....

Marc

like image 168
marc_s Avatar answered Oct 06 '22 03:10

marc_s