This project has moved and is read-only. For the latest updates, please go here.

Ignoring Parent Objects

Mar 23, 2008 at 10:47 PM
Hey there, I posted this on USENET re: the DataContractJSONSerializer, but was wondering if maybe Json.NET gives me a way to handle this specific scenario. (Or maybe a serialization wizard will be able to answer my original question.)

I have an object that has a DataContract attribute on it and several DataMembers. This object inherits from another object that does NOT have any data contract attributes, but whose properties trickle down into the object I'm trying to serialize. This makes the serializer give me errors telling me I need to add the DataContract to my parent object.

Is there any way around this? Is there a way that I can tell it to serialize my main object into JSON and just ignore those properties that come from the parent object? I don't particularly need the properties, and I can't add the attributes to the parent class.

Mar 25, 2008 at 3:56 PM
Hey - it looks like patch 934 that was submitted will do what I need to do. I'm going to go about trying to apply this patch myself to a copy of the code, but if it works, I STRONGLY suggest that this patch go in.
Mar 29, 2008 at 10:44 AM
I have added the ability to make properties opt-in rather than opt-out.
Apr 2, 2008 at 4:46 PM
Excellent. Thank you!

For anyone else reading this thread, and who doesn't want to search through the patch notes, simply add this attribute to your class:


Then, it will only pick up the properties with the JsonProperty attribute.

Thanks again - this saves me a load of work.