1

I tried searching google and stackoverflow for this simple question but had no luck.

When using Json.NET I want to serialize an object with all of the fields except a specific type.

I don't want to mark every field/property with JsonIgnore to prevent serialization instead what I want to do is just mark the specific type as "NonSerialized". This of course won't work. Is there anyway to achieve it without marking properties and fields with attributes?

I tried resolving this using DefaultContractResolver but didn't manage to make it work.

Sergey Rotbart
  • 319
  • 3
  • 12
  • 1
    Explained in the first web search hit on "json.net exclude type": [Exclude property from serialization via custom attribute (json.net)](http://stackoverflow.com/questions/13588022/exclude-property-from-serialization-via-custom-attribute-json-net). Don't get confused by the title, the answers work without attributes. – CodeCaster Dec 05 '13 at 13:55
  • 2
    Thanks, I did find the linked question on google but for some reason couldn't apply it to my code, after looking at it again and refactoring my code I noticed I made a mistake. The ShouldSerialize method always returned true. Changed some code and it works perfect. – Sergey Rotbart Dec 05 '13 at 14:13

1 Answers1

-1

You could try to use the JavaScriptSerializer class and implement a JavaScriptConverter which excludes the properties you don't need. You can then register that converter on the JavaScriptSerializer using its RegisterConverters method.

user247702
  • 23,641
  • 15
  • 110
  • 157
Sam Bauwens
  • 1,317
  • 11
  • 18