1

I have a number of WebApi methods which return

OK(Product)

Where Product is my layered object with lots of properties, sub objects etc.

I want all fields of Product that are null to not be included in the output JSON, but can't figure out how to override the default JSON formatter used by WebApi.

I have tried calling this on startup

public static void AddJsonSerialisers(HttpConfiguration httpConfiguration)
{
    var jsonConverters = Converters.GetJsonConverters().ToList();

    var jsonFormatter = httpConfiguration.Formatters.JsonFormatter;

    jsonConverters.ForEach(jsonFormatter.SerializerSettings.Converters.Add);

    JsonConvert.DefaultSettings = () => new JsonSerializerSettings { Converters = jsonConverters, NullValueHandling = NullValueHandling.Ignore};
}

But the nulls continue to appear, so no joy there.

How can override the default formatting for webapi JSON results?

NZJames
  • 4,963
  • 15
  • 50
  • 100

0 Answers0