Codebelt.Extensions.Carter.AspNetCore.Newtonsoft.Json Namespace
When your Carter application needs the full power of Newtonsoft.Json—custom contract resolvers, StringEnumConverter, JsonIgnore attributes, or legacy compatibility mode—reach for the Codebelt.Extensions.Carter.AspNetCore.Newtonsoft.Json namespace. The central type, NewtonsoftJsonNegotiator, handles JSON response serialization through the Carter HttpResponse.Negotiate() pipeline.
This negotiator registers the application/json media type and uses a configurable NewtonsoftJsonFormatterOptions to control serialization settings, supported media types, and exception-descriptor formatting. It integrates with ASP.NET Core's options pattern and supports the Accept-Charset header for character-encoding negotiation.
Availability: .NET 10
When to use
Choose NewtonsoftJsonNegotiator when you depend on Newtonsoft.Json features that System.Text.Json does not provide—such as TypeNameHandling, custom JsonConverter pipelines, PreserveReferencesHandling, or ISerializable support. This negotiator is also the right choice when migrating an existing Newtonsoft.Json-based codebase to Carter without changing your serialization stack.
Start here
Install the Codebelt.Extensions.Carter.AspNetCore.Newtonsoft.Json NuGet package, then register NewtonsoftJsonNegotiator as the response negotiator in your Carter configuration via CarterConfigurator.WithResponseNegotiator<T>().
Classes
- NewtonsoftJsonNegotiator
-
The
NewtonsoftJsonNegotiatorintegrates Newtonsoft.Json serialization into the Carter response pipeline. Register it as the response negotiator to have allHttpResponse.Negotiate()calls produce JSON output using Newtonsoft.Json's configurable serializer.Availability: .NET 10
Example
The following example demonstrates how to register
NewtonsoftJsonNegotiatorwith a custom converter and negotiate a response model.using System; using System.IO; using System.Text; using System.Threading; using System.Threading.Tasks; using Codebelt.Extensions.Carter.AspNetCore.Newtonsoft.Json; using Codebelt.Extensions.Newtonsoft.Json.Formatters; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; using Microsoft.Net.Http.Headers; namespace DocfxExample; public static class NewtonsoftJsonNegotiatorExample { public static async Task DemonstrateAsync() { var options = Options.Create(new NewtonsoftJsonFormatterOptions()); var negotiator = new NewtonsoftJsonNegotiator(options); var acceptHeader = MediaTypeHeaderValue.Parse("application/json"); if (negotiator.CanHandle(acceptHeader)) { var context = new DefaultHttpContext(); context.Request.Headers.Accept = "application/json"; var model = new { Name = "Newtonsoft", Value = 42 }; await negotiator.Handle(context.Request, context.Response, model, CancellationToken.None); context.Response.Body.Position = 0; using var reader = new StreamReader(context.Response.Body, Encoding.UTF8); Console.WriteLine(await reader.ReadToEndAsync()); } } }