I'm wondering how to exclude/strip certain properties of given type(s) (or collections of those) from being serialized using Json.NET library? I tried to write my own contract resolver (inheriting from DefaultContractResolver) with no luck.
I know that I could be done using DataAnnotations, decorating the excluded properties with ScriptIgnoreAttribute, but it's not applicable in my scenario. The objects serialized can be virtually anything, so I don't know which properties to exclude at design-time. I know only the types of properties that should not be serialized.
It looks like a rather simple task, but unfortunately I couldn't find a decent solution anywhere...
BTW - I'm not bound to Json.NET library - if it can easily be done with default/other .NET JSON serializers it'd be an equally good solution for me.
UPDATE
The properties has to be excluded before trying to serialize them. Why?
Basically, the types of objects I'm receiving and serializing can have dynamic properties of type inheriting from IDynamicMetaObjectProvider. I'm not going to describe all the details, but the DynamicMetaObject returned from GetMetaObject method of these objects doesn't have DynamicMetaObject.GetDynamicMemberNames method implemented (throws NotImplementedException...). Summarizing - the problem is those objects (I need to exclude) doesn't allow to enumerate their properties, what Json.NET serializer tries to do behind the scenes. I always end up with NotImplementedException being thrown.
IDynamicMetaObjectProvider? I am very familiar with dynamic types, but I am unsure in your case what you are actually serializing. It sounds like you are serializing normal classes and want to exclude certain properties on these that reference dynamic instances. At the same time you also indicated that you do not know the types, or base types, of the items you are serializing--just that they occasionally reference properties that are dynamic and cannot be serialized. Please post a simple example.DynamicObjector some other known type implementingIDynamicMetaObjectProvideror are they also completely random classes from other libraries not known at design time?