views:

83

answers:

2

I am using the DataContractJsonSerializer class to serialise my objects to JSON.

Is there an attribute I can use on fields in my custom objects (C#) that will turn off serialisation for that one particular field?

+1  A: 

If you use the DataContractAttribute to decorate your class, you will then be required to use the DataMemberAttribute to mark the fields/properties that you want to have serialized. I don't think that there is any attribute that can turn off serialization of a public property/field on a class without the DataContractAttribute. MSDN has a tutorial on how to serialize data as JSON.

tvanfosson
+1  A: 

Actually, there is a way: IgnoreDataMemberAttribute

See http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ignoredatamemberattribute.aspx and http://msdn.microsoft.com/en-us/library/cc656732.aspx for full documentation.

Eugene Osovetsky
That second link was great: "All public fields, and properties with public get and set methods are serialized, unless you apply the IgnoreDataMemberAttribute attribute to that member."
dan