Great, but should also (or instead) document use of Microsoft's own implementation in Microsoft.AspNetCore.JsonPatch.
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
@serpent5 here's an easy one if you're interested.
Microsoft.AspNetCore.JsonPatch _is_ being used in the samples; it's just indirect. The Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package has a dependency on Microsoft.AspNetCore.JsonPatch.
For reference, Microsoft.AspNetCore.JsonPatch provides JsonPatchDocument and the associated "patching" logic. Microsoft.AspNetCore.Mvc.NewtonsoftJson includes a Json.NET-based input formatter that converts a JSON Patch request body to a JsonPatchDocument. Microsoft.AspNetCore.JsonPatch is also dependent on Json.NET.
https://github.com/aspnet/AspNetCore/issues/14035 shows a request for System.Text.Json-based JSON Patch support but:
This is not something we plan to do in the near future.
Following that issue through, there's an example of how to use Json.NET only for the JSON Patch side of things, but I don't know whether that's something the team would like to document as a suggested approach (it's a bit "hacky"). Having said that, it looks like the only option if you really want to use System.Text.Json for all non-JSON Patch requests.
@Rick-Anderson:
What should we do about this? It looks like changes in this section would be a good start. Although the migration docs don't show it in the list, I don't think the following is true for 3.0:
The package is included in the Microsoft.AspnetCore.App metapackage.
I checked the contents of the 2.2 version and it's there but it's not in the 3.0 version.
There's no 3.x sample for this topic. Should I create one?
I really appreciate the followup.
The key point in https://github.com/aspnet/AspNetCore/issues/14035 is this:
Yes, we can simply add AddNewtonsoftJson() on the service collection instance, but it replaces the default System.Text.Json formatter for all JSON which is very unfortunate and defeat the purpose of moving away from using Newtonsoft.JSON.
Which is disappointing. I’m hoping there are coherent plans to move to a full utf-8 string / json stack and avoid much of the transcoding and allocation in the pipeline (see Utf8Json benchmarks).
@Paul-Dempsey can you respond in GitHub and not via email? Can you edit your last response?
You make an excellent point, but one that should be made at https://github.com/aspnet/AspNetCore/issues/14035, not here. @Paul-Dempsey Can you move your comment to https://github.com/aspnet/AspNetCore/issues/14035 and ask them to consider reopening the issue or providing more guidance?
@pranavkm please review and provide an outline to resolve this issue.
Do we want to document this approach use Json.Net only for application/json-patch+json requests
@serpent5 I took care of The package is included in the Microsoft.AspnetCore.App metapackage.
The MD is divided into
::: moniker range=">= aspnetcore-3.0"
// Copy exiting MDF file here, I fixed the package included
::: moniker-end
::: moniker range="< aspnetcore-3.0"
// Original file here
::: moniker-end
Let's hold off until @pranavkm responds.
How about this: https://github.com/aspnet/AspNetCore.Docs/pull/15400?
Is there a mistake in the docs? The code sample for using System.Text.Json and setting up Newtonsoft.Json just for JsonPatchDocument<T> is:
```c#
public void ConfigureServices(IServiceCollection services)
{
var jsonPatchServices = new ServiceCollection();
jsonPatchServices.AddLogging()
.AddMvc()
.AddNewtonsoftJson();
var jsonPatchServiceProvider = jsonPatchServices.BuildServiceProvider();
var jsonPatchOptions = jsonPatchServiceProvider.GetRequiredService<IOptions<MvcOptions>>().Value;
var jsonPatchInputFormatter = jsonPatchOptions.InputFormatters.OfType<NewtonsoftJsonPatchInputFormatter>().First();
services.AddControllersWithViews(options => jsonPatchOptions.InputFormatters.Insert(0, jsonPatchInputFormatter));
}
Shouldn't the last line be this?
```c#
services.AddControllersWithViews(options => options.InputFormatters.Insert(0, jsonPatchInputFormatter));
@serpent5 do you want to fix this?
Yeah, sure. PR incoming.
Most helpful comment
Is there a mistake in the docs? The code sample for using
System.Text.Jsonand setting upNewtonsoft.Jsonjust forJsonPatchDocument<T>is:```c#
public void ConfigureServices(IServiceCollection services)
{
var jsonPatchServices = new ServiceCollection();
}