1

Using C#

Im using web api method webmethod1 to return a list of a POCO class pcl1 with 10 members. I also have webmethod2 which returns a shortened version of pcl1. By shortened I mean I deliberately populate just 3 members of that class.

Because of large numbers of records returned I would like to slightly improve the performance by ignoring null value members in my POCO class. Of course I could create a 2nd POCO class and in my code loop thorugh thr result and cast it and so on but I thought I'd explore this dunamic Ignore if null option if available. Also I can't use [Ignore if null] attribute on my 10-member POCO class.

So is there a way to dynamically ignore null values when compiling JSON result ?

nlv
  • 791
  • 7
  • 28
  • Are you looking for [How to ignore a property in class if null, using json.net](https://stackoverflow.com/questions/6507889/how-to-ignore-a-property-in-class-if-null-using-json-net)? See also [Reducing Serialized JSON Size](http://www.newtonsoft.com/json/help/html/ReducingSerializedJSONSize.htm). – dbc May 10 '16 at 07:04
  • Yes and no. As mentioned in my question using attributes is not an option. But if Youre referencing serialiser configuration then - yes, thats what im looking for...But the question is how do I configure that for one web api controller... – nlv May 10 '16 at 07:11
  • 1
    See [How to set custom JsonSerializerSettings for Json.NET in MVC 4 Web API?](https://stackoverflow.com/questions/13274625/how-to-set-custom-jsonserializersettings-for-json-net-in-mvc-4-web-api). – dbc May 10 '16 at 07:15
  • The "Single JsonConvert with an overload" forces the controller to return a type of string rather then injecting the settings in its "silent" object to json object conversion – nlv May 10 '16 at 07:28

0 Answers0