Codebelt.Extensions.Carter.AspNetCore.Text.Json Namespace
Use the Codebelt.Extensions.Carter.AspNetCore.Text.Json namespace when your Carter application needs JSON response serialization backed by System.Text.Json—the high-performance, allocation-friendly JSON serializer built into the ASP.NET Core runtime. The JsonResponseNegotiator type integrates this serializer into the Carter HttpResponse.Negotiate() pipeline.
This negotiator uses the JsonFormatterOptions to configure serialization behavior, supported media types, and exception-descriptor formatting. It respects the Accept and Accept-Charset headers for content-type and encoding negotiation.
Availability: .NET 10
When to use
Choose JsonResponseNegotiator when you prefer the modern System.Text.Json stack for its performance characteristics and native ASP.NET Core integration, when you have no dependency on Newtonsoft.Json-specific features, or when you want to minimize external dependencies beyond the ASP.NET Core framework.
Start here
Install the Codebelt.Extensions.Carter.AspNetCore.Text.Json NuGet package, then register JsonResponseNegotiator as the response negotiator in your Carter configuration via CarterConfigurator.WithResponseNegotiator<T>().
Classes
- JsonResponseNegotiator
-
The
JsonResponseNegotiatorintegrates System.Text.Json serialization into the Carter response pipeline. Register it as the response negotiator to have allHttpResponse.Negotiate()calls produce JSON output using the modern, high-performance System.Text.Json serializer.Availability: .NET 10
Example
The following example demonstrates how to register
JsonResponseNegotiatorand negotiate a response model with custom options.using System; using System.IO; using System.Text; using System.Threading; using System.Threading.Tasks; using Codebelt.Extensions.Carter.AspNetCore.Text.Json; using Cuemon.Extensions.Text.Json.Formatters; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; using Microsoft.Net.Http.Headers; namespace DocfxExample; public static class JsonResponseNegotiatorExample { public static async Task DemonstrateAsync() { var options = Options.Create(new JsonFormatterOptions()); var negotiator = new JsonResponseNegotiator(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 = "System.Text.Json", 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()); } } }