JSON has become an essential part of virtually all modern .NET applications and in many cases even surpassed the usage of XML. However, .NET hasn't had a (great) built-in way to deal with JSON. Instead we've relied on Json.NET which continues to serve the .NET ecosystem well.
ASP.NET Core already uses JSON.NET as JavaScriptSerializer isn't implemented/ported to.NET Core. Microsoft.AspNetCore.Mvc depends on Microsoft.AspNetCore.Formatter.Json which depends on Microsoft.AspNetCore.JsonPatch, which depends on Newtonsoft.Json (see source). This is only true for ASP.NET Core 1.0 to 2.2. Setting JSON Serialization Configuration At Runtime On A.NET Core API May 5, 2018 by Wade 2 Comments I’m currently living the whole snake case vs camel case argument all over again.
- Right now, in ASP.NET Core 2.2, JSON.NET is used by the framework as well as by user code. This has the potential to cause conflicts with your own use of Newtonsoft.Json; if ASP.NET Core 3.0 moved to JSON.NET 12.x and there was some kind of issue in there that broke your application, you'd have a problem.
- Json.NET has been removed from the ASP.NET Core 3.0 shared framework. You can use the new JSON serializer layers on top of the high-performance Utf8JsonReader and Utf8JsonWriter. It deserializes objects from JSON and serializes objects to JSON. Memory allocations are kept minimal and includes support for reading and writing JSON with Stream asynchronously.
- The JSON Serializer converts JSON data into C# Objects. Your first task is to define a C# class type to contain the information you use from this response. Let's build this slowly, so start with a simple C# type that contains the name of the repository.
- To serialize a.Net object to JSON string use Serialize method. It's possible to deserialize JSON string to.Net object using Deserialize or DeserializeObject methods. Let's see how to implement serialization and deserialization using JavaScriptSerializer. AutoWrapper - Prettify Your ASP.NET Core APIs With Meaningful Responses.
Moving forward, we plan on making some changes to our JSON support:
We need high-performance JSON APIs. We need a new set of JSON APIs that are highly tuned for performance by using
Span<T>
and allows for processing UTF-8 directly without having to transcode to UTF-16string
instances. Both aspects are critical for our web server Kestrel, where throughput is a key requirement.Remove dependency from ASP.NET Core to Json.NET. Today, ASP.NET Core has a dependency on Json.NET. While this provides a tight integration between ASP.NET Core and Json.NET, it also means that application developers cannot freely choose which JSON library they are using. This is also problematic for customers of Json.NET as the version is dictated by the underlying platform. However, Json.NET is frequently updated and application developers often want to -- or even have to -- use a specific version. Thus, we want to remove the dependency from ASP.NET Core 3.0 to Json.NET so that customers can choose which version to use, without fearing they might accidentally break the underlying platform. In addition, this makes it also possible to plug-in an entirely different JSON library.
Provide an ASP.NET Core integration package for Json.NET. Json.NET has basically become the Swiss Army knife of JSON processing in .NET. It provides many options and facilities that allow customers to handle their JSON needs with ease. We don't want to compromise on the Json.NET support customers are getting today, for example, the ability to configure the JSON serialization via the
AddJsonOptions
extension method. Thus, we want to provide the Json.NET integration as a NuGet package that developers can optionally install so they get all the bells and whistles they get from Json.NET today. The other part of this work item is to ensure we have the right extension points so that other parties can provide similar integration packages for their JSON library of choice.
Below are more details around this plan.
The need for high-performance JSON APIs
The requirements for the .NET stack have changed a bit since the arrival of .NET Core. Historically, .NET has valued usability and convenience. With .NET Core, we've added a focus on performance, and we've made significant investments to serve high performance needs. And the improvements we made in the popular TechEmpower benchmark are a testament to that.
With .NET Core 2.1, we've added a brand new primitive called Span<T> that allows us to represent native memory and arrays in a uniform way. With this type, we've also added a set of parsing and encoding APIs that are much more memory efficient without having to resort to unsafe code.
Part of the work of minimizing allocations is to avoid having to transcode UTF-8 payloads into UTF-16 strings, purely for parsing reasons. Currently, Json.NET is implemented by reading UTF-16. We need the ability to read (and write) JSON documents directly in UTF-8 because most network protocols (including HTTP) use UTF-8.
During .NET Core 2.1 we've learned that updating our existing APIs to leverage Span<T>
has limits. While we did add a bunch of overloads that accept spans, we also had to produce brand new APIs that are designed around minimizing allocations and dealing with buffers, which we exposed in System.Buffers
namespaces. And with System.IO.Pipelines
we've also added a programming model that enables developers to share buffers without having to deal with lifetime issues.
Based on these experiences we believe in order to support JSON parsing, we'll need to expose a new set of JSON APIs that are specifically geared for high-performance scenarios.
You might wonder why we can't just update Json.NET to include support for parsing JSON using Span<T>
? Well, James Newton-King -- the author of Json.NET -- has the following to say about that:
Json.NET was created over 10 years ago, and since then it has added a wide range of features aimed to help developers work with JSON in .NET. In that time Json.NET has also become far and away NuGet's most depended on and downloaded package, and is the go-to library for JSON support in .NET. Unfortunately, Json.NET's wealth of features and popularity works against making major changes to it. Supporting new technologies like Span<T>
would require fundamental breaking changes to the library and would disrupt existing applications and libraries that depend on it.
Going forward Json.NET will continue to be worked on and invested in, both addressing known issues today and supporting new platforms in the future. Json.NET has always existed alongside other JSON libraries for .NET, and there will be nothing to prevent you using one or more together, depending on whether you need the performance of the new JSON APIs or the large feature set of Json.NET.
Move Json.NET integration into a separate NuGet package
Today, you cannot use ASP.NET Core without Json.NET because it is a dependency of ASP.NET Core itself. Over the years, we've received feedback that the dependency can conflict with other libraries that have their own dependency on a different version of Json.NET. In the past, we've considered addressing this issue by using a private copy of Json.NET in ASP.NET. However, this would create problems when developers want to configure Json.NET (for instance, in order to control how the serializer behaves when formatting JSON objects).
Moving forward we'd like to:
Replace the internal usage of Json.NET in ASP.NET Core by the new platform-provided JSON APIs.
Factor the public facing usage of Json.NET into an optional integration package that can be acquired from NuGet.
So the existing integration between ASP.NET Core and Json.NET will continue to be supported, but will be moving out of the platform and into a separate package. However, since the integration is then designed to sit on top of the platform, it will also allow customers to update Json.NET to later versions.
Furthermore, customers who need more performance can also choose to use the new JSON APIs, at the expense of the rich feature set that Json.NET offers.
Is it possible to customize the way types are serialized to the response in ASP.NET Core MVC?
In my particular use case I've got a struct, AccountId
, that simply wraps around a Guid
:
When I return it from an action method, unsurprisingly, it serializes to the following:
Instead, I'd like to automatically unwrap the Value
so it serializes to a plain string:
Can MVC be configured to achieve this?
Marc LaFleurNet Core Json Serializer Free
1 Answer
.net Core Json Serializer Camelcase
You can customize the output produced by JSON.NET with a custom converter.
In your case, it would look like this:
If you prefer not to use the JsonConverter
attribute, it's possible to add converters in ConfigureServices
(requires Microsoft.AspNetCore.Mvc.Formatters.Json
):