0

I created a Web API using VS 2012. I have a method with a custom object parameter that I am passing JSON to via Fiddler for testing:

[HttpPost, HttpPut] public HttpResponseMessage UpsertProject(Projects p) { ... }

My Projects object has about a dozen properties marked as JsonIgnore. My assumption was that when my object was serialized into Json those properties would be ignored...which is true. However, when I debug my method I'm noticing that all the object properties marked with JsonIgnore are set to null even if the Json that I pass in from Fiddler is setting them. I also try to get data as Json and deserialize it into a new instance of the object but that also does not set the properties that are marked JsonIngore. I knew JsonIgnore would work for serializing but didn't think it would prevent properties from being set when deserializing. What's frustrating is I know that ScriptIgnore doesn't behave this way, but I want to use JSON.net to handle my serializing/deserializing. I've also created a windows app and tested the same serializing/deserializing functionality and it works in it. So I'm wondering if this is a Web API limitation with the JsonIgnore attribute?

kaya3
  • 47,440
  • 4
  • 68
  • 97
user2337213
  • 27
  • 1
  • 6

1 Answers1

0

If it works the way you want in the Windows application but not in the Web API, that tells me that the JSON serializer settings are different between the two. What settings are you using in the Windows app that makes it work? You can take those settings and apply them to the Web API serializer in the Register method of the WebApiConfig class (in the App_Start folder of your Web API project). For example:

JsonSerializerSettings jsonSettings = config.Formatters.JsonFormatter.SerializerSettings;
jsonSettings.NullValueHandling = NullValueHandling.Ignore;
jsonSettings.DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate;
...

EDIT

OK, so if I understand you correctly, based on your most recent comments, you want everything to be deserialized if possible, but you only want two specific properties to be serialized and those apparently do not have null or default values. In that case, there are two approaches you can take:

  1. Set the properties that you don't want serialized to null or zero or false (the default value) just before serializing. Because you have DefaultValueHandling set to Ignore, this will cause those properties not to be serialized.

  2. Create several boolean ShouldSerializeXXX() methods in your class where XXX is the name of each property you don't want serialized. These methods should return false. See the first answer of this question for an example.

Don't use JsonIgnore because, as you have seen, this will cause the property to be completely ignored by Json.Net, both for serializing and deserializing.

Community
  • 1
  • 1
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • Hey Brian - thanks for the reply. I haven't set any specific JSON Serializer Settings in either the Windows App or Web API. Not sure if there are any underlying settings I'm not aware of? A little more detail, my code is contained within an API controller and not MVC API controller, if that matters. Because of this issue, I'm essentially using the JavaScriptSerializer with ScriptIgnore. It's working which is fine, I guess I was just hoping to use Json.Net for everything in my project. Let me know if there is anymore informaiton I can provide to help trouble-shoot this issue. Thanks. – user2337213 May 06 '13 at 17:21
  • Is your base class `ApiController` or just `Controller`? The former uses Json.Net by default as of MVC version 4, while the latter uses the JavaScriptSerializer. – Brian Rogers May 06 '13 at 22:45
  • The base class for my controller is ApiController. – user2337213 May 07 '13 at 13:55
  • OK, then it should be using Json.Net. Try removing the JsonIgnore attributes from the properties in your object, and then the add the settings as shown in my answer above to the Register method of the WebApiConfig class in your MVC project. – Brian Rogers May 08 '13 at 00:09
  • Very close to working. The only issue I have is that I'm passing a value in my Json to my API method that I don't want serialized. In my testing, the values come into my method fine via my object parameter, but I then only want to serialize two of the object properties. If I add JsonIgnore to that property, I'm having the same issue as before where the property value of the object is not set, comes in as NULL. – user2337213 May 08 '13 at 16:30
  • OK, thanks Brian. Your updated information answered my question about the JsonIgnore property. For me personally, I would consider it a bug that should be fixed so JsonIgnore only works during serializing and not deserializing, basically the way ScriptIgnore works. Anyway, thanks a lot for your help with this. I'll mark your answer as the fix. – user2337213 May 13 '13 at 13:44