4

As we know, ASP.NET web api (ApiController) gives JSON response automatically through NewtonSoft.JSON.

My three-level 1:MANY:1 EF data model is queried in several ways and it works fine with applying [JsonIgnore] attribute to some virtual properties in order to eliminate reference circulation issue. (I use third solution in this(link) answer)

but my problem is, I sometimes need to use json-ignored properties to query with model.

Is there any method to control [JsonIgnore] attribute during runtime? Or, is there any method to switch on-and-off property to be ignored in NewtonSoft.JSON during runtime?

5
  • You solved this problem? Commented Dec 5, 2013 at 9:38
  • @oakio // No, I didn't solve it. but I concluded that it is not possible because such a attribute is pre-compiler feature in general. Commented Dec 5, 2013 at 14:39
  • 1
    I am found solution, see 'Conditional Property Serialization' james.newtonking.com/json/help/html/ConditionalProperties.htm Commented Dec 5, 2013 at 17:31
  • @oakio // wow! amazing gem you found! :) thanks very much. it finally solved! Commented Dec 6, 2013 at 9:28
  • @oakio // if you write your contents as an answer, I will mark it as an answer. Commented Dec 8, 2013 at 17:07

1 Answer 1

2

I know two way to solve this problem:

  1. You may add additional boolean property (with specific name) into your model. This is solution is very simple, but requires changes to your model.
  2. You may write own IContractResolver. This method is more complex, but more flexible.

Both solutions described here: 'Conditional Property Serialization' http://james.newtonking.com/json/help/index.html?topic=html/ConditionalProperties.htm

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.