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.

2
  • 1
    Explained in the first web search hit on "json.net exclude type": Exclude property from serialization via custom attribute (json.net). Don't get confused by the title, the answers work without attributes. Commented Dec 5, 2013 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. Commented Dec 5, 2013 at 14:13

1 Answer 1

-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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm restricted to the Newtonsoft's Json.NET serializer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.