I'm using WCF for JSON services using this format:
[OperationContract]
[ServiceKnownType(typeof(ComplexResult))]
[WebInvoke(
Method = "GET",
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Json)]
MyClass MyFunction(string myParams);
This works great, however it has a limitation. I can't ignore properties of my class that I'm serializing to JSON. If I use the JavaScriptSerializer class then I can put [ScriptIgnore] attributes on the properties I want to ignore and they won't be serialized in the JSON, however this does not work with the method above.
Is there a way to exclude properties of the classes getting serialized to JSON using the ResponseFormat Json method?
WCF by default uses the DataContractJsonSerializer
to serialize objects. Depending on how MyClass
is defined, you can use different attributes to prevent members from being serialized:
[IgnoreDataMember]
attribute in the members you don't want serialized[Serializable]
, you can use the [NotSerialized]
attribute in those members[DataContract]
, then you just need to not add [DataMember]
to the members you don't want serialized.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With