81

I understand that ASP.NET Web API natively uses Json.NET for (de)serializing objects, but is there a way to specify a JsonSerializerSettings object that you want for it to use?

For example, what if I wanted to include type information into the serialized JSON string? Normally I'd inject settings into the .Serialize() call, but Web API does that silently. I can't find a way to inject settings manually.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Richard Neil Ilagan
  • 14,627
  • 5
  • 48
  • 66

3 Answers3

118

You can customize the JsonSerializerSettings by using the Formatters.JsonFormatter.SerializerSettings property in the HttpConfiguration object.

For example, you could do that in the Application_Start() method:

protected void Application_Start()
{
    HttpConfiguration config = GlobalConfiguration.Configuration;
    config.Formatters.JsonFormatter.SerializerSettings.Formatting =
        Newtonsoft.Json.Formatting.Indented;
}
carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • 1
    I cant get this to work in ASP.NET app with HangFire installed. It is referencing the incorrect libarary or something. had to use the other answer with Default settings.. – Piotr Kula Oct 28 '15 at 14:28
  • You can do it per controller or action: https://stackoverflow.com/questions/44499041/web-api-configure-json-serializer-settings-on-action-or-controller-level – Alx Jul 04 '18 at 14:10
45

You can specify JsonSerializerSettings for each JsonConvert, and you can set a global default.

Single JsonConvert with an overload:

// Option #1.
JsonSerializerSettings config = new JsonSerializerSettings { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore };
this.json = JsonConvert.SerializeObject(YourObject, Formatting.Indented, config);

// Option #2 (inline).
JsonConvert.SerializeObject(YourObject, Formatting.Indented,
    new JsonSerializerSettings() {
        ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
    }
);

Global Setting with code in Application_Start() in Global.asax.cs:

JsonConvert.DefaultSettings = () => new JsonSerializerSettings {
     Formatting = Newtonsoft.Json.Formatting.Indented,
     ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
};

Reference: https://github.com/JamesNK/Newtonsoft.Json/issues/78

smockle
  • 2,162
  • 17
  • 19
  • 4
    FWIW, the second method was what I initially tried which did *not* work. I had to use `HttpConfiguration` instead as in [carlosfigueira's answer](http://stackoverflow.com/a/13274791/44853) as settings configured in `JsonConvert.DefaultSettings` were not being observed. – lc. Aug 26 '14 at 05:51
  • 2
    In my case the Global setting usgin `JsonSerializerSettings ` is what worked for me. I could not get the HttpCOnfiguration to work, it was comming back with another assemblies methods (Hangifre) not sure why. – Piotr Kula Oct 28 '15 at 14:29
  • Can I use hidden field `formHiddenField.Value = JsonConvert.SerializeObject(listaCursos, Formatting.Indented, jsonSerializerSettings);` and use JQuery for get the value `var data = $('#formHiddenField').val();` ? – Kiquenet Feb 20 '18 at 10:06
1

Answer is adding this 2 lines of code to Global.asax.cs Application_Start method

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = 
    Newtonsoft.Json.PreserveReferencesHandling.All;

Reference: Handling Circular Object References

AEMLoviji
  • 3,217
  • 9
  • 37
  • 61