-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The future of JSON in .NET Core 3.0 #27761
Comments
This is great. I am all for faster and less allocating json parsing. Will there be a discussion about the features from json.net that the new json apis will support? If there is,I think the two major features that come to mind would be renaming/casing properties and ignoring null properties. |
Yes. We've done some early thinking that we will migrate to CoreFx. It will be a feature that is designed & built in the open as usual. In addition, I've reached to authors of many of the popular JSON libraries and invited them to review early drafts of this announcement. My hope is that we can work together to create a solid JSON component for the platform while also keeping the ecosystem on top of it (such as ASP.NET Core) pluggable to allow for others. In the end, different consumers will have different goals and being able to plug-in a different library means you can get maximum flexibility in choosing the component that has the best cost/benefit for your app. |
Hey @terrajobst. Will the new JSON appear as a netstandard API surface, or just integrated into Core for now? |
Yes, the question is just which release train it can catch. 2.1 might be too early. |
So the JSON parsing bits baked into the framework are planned to be available when v3.0 goes to RTM or will only the integration Apis in ASP.NET Core be complete (with just one implementation - JSON.NET) that will be swappable at a later date? |
The plan for 3.0 is as follows:
There is an open ended question what the templates for ASP.NET in 3.0 will use. Depending on fidelity we can provide by 3.0 we might have them pull in the Json.NET integration package. However, the goal is to deliver enough fidelity & parity to only depend on the built-in ones by default. |
Thanks - that helps clear things up. 👍 And some additional questions! If an integration package is used, will it be used throughout the entire ASP.NET Core pipeline or only in some places? Would the Api ergonomics be:
|
Assuming that this new parser will be used for all the built-in JSON stuff like Thanks. |
This is awesome news! Quick question: What packages will this library depend on? |
Why to reinvent a wheel that is tested by production customers? If there is a problem with Json.Net, just send a PR as it's open source. I suppose the problem with Json.NET is that it's not owned by Microsoft, so it has to be replaced. Oh but there is the one already in System.Runtime.Serialization, called DataContractJsonSerializer. Can you use that, or is it just so fun to code new APIs, DIY, that it cannot be avoided? The reason I'm not very happy with this is that Json.Net supports already edge cases like e.g. F# Discriminated Unions. Not particularly well, but on a level that developers can live with that. Instead any new API's usually forget anything else than the use case of an ASP.NET-website. |
@markrendle There is a opt-in setting on JsonReader (work in progress) to allow comments. The configuration system will likely enable that setting by default. |
@Thorium Did you actually read the OP? It explains why not JSON.NET, and that JSON.NET will continue to be officially supported with an add-in package. |
@JamesNK 😄 |
@Thorium Json.NET isn't going away. You aren't losing anything. This is another option for simple and high performance scenarios. |
How will the Json generated to be backward compatible? For example, I'm using SignalR which is using Json.NET in the background. Now, will my F# discriminated unions serialize to similar structures so that I will not be fighting issues with the new Azure Signalr Service (backplane) throwing runtime exceptions because of serializating the structures differently than my server's current SignalR library? |
I hope others will pick up the new APIs quickly. Looking at you, @AzureCosmosDB ;-) |
Are you planning on including a class like JObject and support for dynamic or this is out of scope for this feature? |
I recommend a look in one of this libs:
this could be a really good way to get inspirations. |
Will |
Is there a reason why the 2nd most popular JSON library after JSON.NET - ServiceStack.Text which has already been refactored to be built on Span APIs has been excluded? ServiceStack.Text serializers are what's used to power ServiceStack which is one of the most popular "Alternative .NET Core compatible Web Framework" that supports running on more platforms in .NET. I'm curious about which "ecosystem" you're referring to and hoping to "work together" with here? I'd obviously be interested in how compatible these "pluggable" APIs end up being or whether this ends up being another area where the adoption and integration of new MS libraries ends up killing the ecosystem that it's replacing. |
It may be worth reviewing the MIT licensed high-performance https://github.com/neuecc/Utf8Json |
This is definitely what we need... my suggestion for the main class name, just use "Json". |
@terrajobst I was wondering when this would happen... I was always wondering why JSON.Net was added as a direct dependency rather than an abstraction (even considering it is the de-facto JSON package for .Net ecosystem). However, I think add an abstraction for JSON-only is somehow a shoot on your feet. I think a serializer abstraction like we have in Orleans IExternalSerializer in a shape of Is there any particular reason why make is JSON-only? I see other cases where people can plug other types of serializers... |
@galvesribeiro Something like |
@yaakov-h wasn't aware of those... Were are they? |
Okey... makes sense now. So where this new JSON-only abstractions comes to play? |
The decision to start this undertaking is also a testament about the inefficiency of System.String (UTF-16 String). |
Yeah... I remember @migueldeicaza saying a while ago that someday, he will make .Net use |
@jges42 @galvesribeiro The proposal to add Utf8String is https://github.com/dotnet/corefx/issues/30503. It seems it's also planned for .Net Core 3.0. |
@JonPSmith: Imho both use cases are invalid from both best practice and DDD point of view.
On top of that, the new JSON API is for high performance scenarios. For everything else where you need rich feature set, you (and should) still use JSON.NET or whatever fulfills your needs. |
@suncodefactory This is the opposite of a breaking change. 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. For example, look at Microsoft.Extensions.Configuration.Json 2.2.0 - it has a dependency on Newtonsoft.Json 11.0.2. That's a configuration package; nothing to do with HTTP request handling or ASP.NET Core MVC. Or look at Microsoft.IdentityModel.Protocols.OpenIdConnect, which uses it for handling JSON Web Tokens; that's a hot path which needs as much performance as possible. JSON.NET is not a slow library by any standards, but it strikes a balance between performance, feature-richness and support for a massive range of user scenarios. Microsoft's new JSON library doesn't need to do that, because JSON.NET exists. So it can focus on handling the absolute basics with maximum performance. .NET has always had its own JSON serialization solution in System.Runtime.Serialization.Json, but in the high-performance world of .NET Core it's not a very good one. I certainly wouldn't want it being invoked to check credentials on every incoming request. A new JSON library, with modern UTF-8 data handling and minimal allocations, is very welcome. You will still be able to reference Newtonsoft.Json in your application, and continue to use it as the deserialization/serialization pipeline for request/response data as before. And from now on, you'll be able to do so without worrying about which version the Core framework depends on. That's a win for everybody. |
Thanks @phillip-haydon and @TsengSR for your thoughts. I was asking if these features would be supported and you says that aren't, which understand and accept. I will continue to use Json.NET for the case where I need to serializing/deserializing EF Core classes. BTW. I do have a valid reason for serializing/deserializing DDD-styles EF Core entity classes. I have a library that contains a feature I call Seed from Production which allows developers to take a snapshot of data from a production database, anonymise any private data, and then seed a new database with the snapshot. I needed this feature for one on my clients and instead of writing just for them I built it into my open-source library EfCore.TestSupport so others can use it (and my client didn't have to pay me for it). |
Is there a plan to support Today this is a way to define how types should serialize/deserialize (e.g. the field name) in a way that doesn't bring a dependency to any serialization library to the project using it. The current |
Not supporting [DataContract] See dotnet/corefx#33115
Hi. |
Thanks @khellang. |
@agjini : services.AddControllers()
.AddNewtonsoftJson() |
Thanks for your help guys. |
IMO json.net is half baked and making it the default (ie for signalr) which breaks existing code was premature. |
On the other hand, migrating from .NET Core 2.2 to 3.0 is a major version upgrade and even if the .NET Core team isn't strictly following semantic versioning, I would expect things to break while upgrading from one version to another without explicit changes (like adding explicitly Newtonsoft's library in the pipeline) |
Closing given this is an announcement and not an issue |
Although the community has a lot of voices against improvement, as a new high-performance framework, the bad speed is unacceptable. |
I know it has been said before, but I'd like to add my wish as well. It would be really awesome if we could have immutable objects. I know it's possible by adding |
We provide .NET Standard Class library to customers so that they can use our library to work on any platform that supports .NET Standard. We need to use System.Text.Json in our class library. What will be the plan to support System.Text.Json in .NET Standard? |
Do you only need the ability to prevent others from mutating it or do you also need the ability to create new instances with parts being smartly replaced (like immutable collections and Roslyn)? If you need the former, we've got you covered with the upcoming |
It's available as a NuGet package for .NET Standard 2.0: System.Text.Json. |
Thanks. When will this System.Text.Json be included in the .NET Standard SDK? |
Are there any plans to make Deserialize method work with PipeReader? Or add Patch method that can be used in streaming scenarios where we don't have all the data when we start deserialization. Here is a simplified version of the proposed API: private async ValueTask<T> Deserialize<T>(PipeReader reader, CancellationToken cancellationToken)
where T: new()
{
T model = new T();
while (!cancellationToken.IsCancellationRequested)
{
ReadResult readResult = await reader.ReadAsync(cancellationToken);
ReadOnlySequence<byte> buffer = readResult.Buffer;
if (readResult.IsCanceled) break;
if (buffer.IsEmpty && readResult.IsCompleted) break;
SequencePosition consumed = JsonSerializer.Patch(model, buffer, readResult.IsCompleted);
reader.AdvanceTo(consumed, buffer.End);
}
return model;
}
public SequencePosition Patch<T>(T model, ReadOnlySequence<byte> jsonData, bool isFinalBlock, JsonSerializerOptions options = null)
{
...
} |
Only this currently. Is really just for 'data-transfer-objects'. Great news! |
There is no .NET Standard SDK. .NET Standard is an API surface, available on all supported Platforms. You can ship |
Well, there is a project type that allows you to use the APIs. I think @mwoo-o is asking whether we have plans to add |
It's terrible.Too few functions to be applied in the project. |
With this commit, we switch to an official Microsoft-developed JSON library. See https://github.com/dotnet/corefx/issues/33115 for more information about the advantages of this new library,
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.
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 inSystem.Buffers
namespaces. And withSystem.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: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.
The text was updated successfully, but these errors were encountered: