1

I have an existing class in an external assembly which I can't change. I would like to serialize an object from this class with Newtonsoft JSON.Net, but not all the properties.

Normally I can do this with the JsonIgnoreAttribute attribute like this:

public class TestJsonClass
{
    public string PropA { get; set; }

    [JsonIgnoreAttribute]
    public string PropB { get; set; }
}

But since I can't change the class, is there a way to ignore a property without attributes?

3
  • You might want to do it at runtime, but it a bit expensive as you'll have to use reflection. Take a look at this thread: social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/… Commented Apr 16, 2012 at 19:06
  • I was hoping for a non reflection way (if possible) Commented Apr 16, 2012 at 19:15
  • 1
    Why don't you deserialize to TestJsonClass and then copy the values to original object. Commented Apr 16, 2012 at 19:29

1 Answer 1

1

Try inherit class and override property with appropriate annotations or copy property values in a completely new class.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.