-
-
Save GFoley83/558ec48c7912cfacf47d09f40d36b5be to your computer and use it in GitHub Desktop.
JSON Media-Type Formatter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; | |
json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; | |
json.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Employee | |
{ | |
public string Name { get; set; } | |
public Department Department { get; set; } | |
} | |
public class Department | |
{ | |
public string Name { get; set; } | |
public Employee Manager { get; set; } | |
} | |
public class DepartmentsController : ApiController | |
{ | |
public Department Get(int id) | |
{ | |
Department sales = new Department() { Name = "Sales" }; | |
Employee alice = new Employee() { Name = "Alice", Department = sales }; | |
sales.Manager = alice; | |
return sales; | |
} | |
} | |
=========================================================================================== | |
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; | |
json.SerializerSettings.PreserveReferencesHandling = | |
Newtonsoft.Json.PreserveReferencesHandling.All; | |
will produce following result : | |
{"$id":"1","Name":"Sales","Manager":{"$id":"2","Name":"Alice","Department":{"$ref":"1"}}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void ConfigureApi(HttpConfiguration config) | |
{ | |
// Remove the JSON formatter | |
config.Formatters.Remove(config.Formatters.JsonFormatter); | |
// or | |
// Remove the XML formatter | |
config.Formatters.Remove(config.Formatters.XmlFormatter); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment