Reputation: 4042
I have a couple of classes that I serialize to XML with DataContract
/ DataMember
attributes and the DataContractSerializer
.
Now I would like to use the same classes to serialize to JSON for ajax calls from the client.
However the members that I want to serialize to JSON are slightly different from the ones that I want to serialize to XML. Is there a way (with attributes) to differentiate between these two?
BTW I'm using DataContractSerializer
as opposed to XmlSerializer
because I need to serialize private members, so switching to XmlSerializer
is not an option.
Upvotes: 2
Views: 1425
Reputation: 7076
For serializing json, I use:
System.Web.Script.Serialization.JavaScriptSerializer
You could also use it for your situation using the ignore attributes to manipulate the output.
http://msdn.microsoft.com/en-us/library/system.web.script.serialization.scriptignoreattribute.aspx
Edit:
Just noticed there was another option which you might be more familiar with in dealing with datacontracts:
System.Runtime.Serialization.Json.DataContractJsonSerializer
Refersnces:
http://msdn.microsoft.com/en-us/library/bb412179.aspx
Upvotes: 1