Note: Slides do not tell the whole story of the talk, so take the stand alone slides with a grain of salt. Things may be taken out of context.
See line 13
This file contains 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 Startup | |
{ | |
private readonly IWebHostEnvironment _environment; | |
public Startup(IWebHostEnvironment environment) | |
{ | |
_environment = environment; | |
} | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddControllers() | |
.AddJsonOptions(options => options.JsonSerializerOptions.WriteIndented = _environment.IsDevelopment()); | |
} | |
// Other code ommitted for brevity | |
} |
By default, JSON in ASP.NET Core will not be indented at all. By that I mean, the JSON is returned without any spacing or formatting, and in the cheapest way possible (from a “bytes-sent-over-the-wire” perspective).
Example:
This is usually what you want when in Production in order to decrease network bandwidth traffic. However, as the developer, if you’re trying to troubleshoot or parse this data with your eyes, it can be a little difficult to make out what’s what. Particularly if you have a large dataset and if you’re looking for a needle in a hay stack.
ASP.NET Core ships with the ability to make the JSON format as indented (aka “pretty print”) to make it more readable. Further, we can leverage the Environment in ASP.NET Core to only do this when in the Development environment for local troubleshooting and keep the bandwidth down in Production, as well as have the server spend less CPU cycles doing the formatting.
In ASP.NET Core 3.0 using the new System.Text.Json formatter:
This file contains 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 Startup | |
{ | |
private readonly IWebHostEnvironment _environment; | |
public Startup(IWebHostEnvironment environment) | |
{ | |
_environment = environment; | |
} | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddControllers() | |
.AddJsonOptions(options => options.JsonSerializerOptions.WriteIndented = _environment.IsDevelopment()); | |
} | |
// Other code ommitted for brevity | |
} |
In ASP.NET Core 2.2 and prior:
This file contains 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 Startup | |
{ | |
private readonly IHostingEnvironment _hostingEnvironment; | |
public Startup(IHostingEnvironment hostingEnvironment) | |
{ | |
_hostingEnvironment = hostingEnvironment; | |
} | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc() | |
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2) | |
.AddJsonOptions(options => | |
{ | |
options.SerializerSettings.Formatting = _hostingEnvironment.IsDevelopment() | |
? Formatting.Indented | |
: Formatting.None; | |
}); | |
} | |
// Other code ommitted for brevity | |
} |
Now when I run the app, the JSON looks much cleaner and easier to read.
There are also Chrome Extensions that will do this for you as well when you navigate to a URL that returns an application/json Content-Type. Some extensions add more formatting capabilities (such as colorization and collapsing). For instance, I use JSON Formatter as my go-to extension for this.
While I use the JSON Formatter extension, I still like setting the indented format in ASP.NET Core for a few reasons:
Hope this helps!
Note: Slides do not tell the whole story of the talk, so take the stand alone slides with a grain of salt. Things may be taken out of context.