Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .docfx/Dockerfile.docfx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG NGINX_VERSION=1.31.0-alpine
ARG NGINX_VERSION=1.31.2-alpine

FROM --platform=$BUILDPLATFORM nginx:${NGINX_VERSION} AS base
RUN rm -rf /usr/share/nginx/html/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
uid: Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json
summary: *content
---
The `Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json` namespace contains both types and extension methods that complements the `Cuemon.Extensions.Newtonsoft.Json` namespace while being an addition to the `Microsoft.AspNetCore.Mvc` namespace. Provides JSON formatters for ASP.NET Core that is powered by Newtonsoft.Json.
The `Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json` namespace inserts Newtonsoft.Json-backed input and output formatters at the front of the ASP.NET Core MVC pipeline, letting MVC negotiate requests and responses through Newtonsoft.Json while leaving the built-in formatters available behind them. Use it in ASP.NET Core MVC or Web API applications that must keep Newtonsoft.Json converters, contract resolvers, or fault formatting without abandoning MVC's formatter infrastructure. Start with `AddNewtonsoftJsonFormatters` on `IMvcCoreBuilder` to register both the formatters and `NewtonsoftJsonFormatterOptions`; use `AddNewtonsoftJsonFormattersOptions` when MVC formatter insertion is already handled elsewhere and you still need the shared formatter options plus the exception-response formatter.

[!INCLUDE [availability-modern](../../includes/availability-modern.md)]

Complements: [Codebelt.Extensions.Newtonsoft.Json namespace](/api/extensions/jsonnet/Cuemon.Extensions.Newtonsoft.Json.html) 📘

### Extension Methods
### Extension Members

|Type|Ext|Methods|
|--:|:-:|---|
|HttpExceptionDescriptorResponseHandler|⬇️|`AddNewtonsoftJsonResponseHandler`|
|JsonSerializerSettings|⬇️|`Use{T}`|
|IMvcBuilder|⬇️|`AddNewtonsoftJsonFormatters`, `AddNewtonsoftJsonFormattersOptions`|
|IMvcCoreBuilder|⬇️|`AddNewtonsoftJsonFormatters`, `AddNewtonsoftJsonFormattersOptions`|
|JsonSerializerSettings|⬇️|`Use<T>`|
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
uid: Codebelt.Extensions.AspNetCore.Newtonsoft.Json.Converters
summary: *content
---
The `Codebelt.Extensions.AspNetCore.Newtonsoft.Json.Converters` namespace contains extension methods that complements the `Cuemon.Extensions.Newtonsoft.Json.Converters` namespace.
The `Codebelt.Extensions.AspNetCore.Newtonsoft.Json.Converters` namespace registers ASP.NET Core-specific Json.NET converters — `ProblemDetails`, `HttpExceptionDescriptor`, and `StringValues` — into a converter collection. Use this namespace in ASP.NET Core applications where HTTP error responses must serialize through the standard `IHttpExceptionDescriptorResponseFormatter` pipeline. Start with `AddHttpExceptionDescriptorConverter` on `ICollection<JsonConverter>` to register an `HttpExceptionDescriptor` converter with optional options; `AddProblemDetailsConverter` and `AddStringValuesConverter` handle the remaining ASP.NET Core types.

[!INCLUDE [availability-modern](../../includes/availability-modern.md)]

Complements: [Codebelt.Extensions.Newtonsoft.Json namespace](/api/extensions/jsonnet/Cuemon.Extensions.Newtonsoft.Json.Converters.html) 📘
Complements: [Codebelt.Extensions.Newtonsoft.Json.Converters namespace](/api/extensions/jsonnet/Cuemon.Extensions.Newtonsoft.Json.Converters.html) 📘

### Extension Methods
### Extension Members

|Type|Ext|Methods|
|--:|:-:|---|
|JsonConverter|⬇️|`AddHttpExceptionDescriptorConverter`, `AddStringValuesConverter`, `AddProblemDetailsConverter`|
|ICollection&lt;JsonConverter&gt;|⬇️|`AddHttpExceptionDescriptorConverter`, `AddProblemDetailsConverter`, `AddStringValuesConverter`|
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
uid: Codebelt.Extensions.AspNetCore.Newtonsoft.Json.Formatters
summary: *content
---
The `Codebelt.Extensions.AspNetCore.Newtonsoft.Json.Formatters` namespace contains both types and extension methods that complements the `Cuemon.Extensions.Newtonsoft.Json` namespace while being an addition to the `Microsoft.AspNetCore.Mvc` namespace. Provides JSON formatters for ASP.NET Core that is powered by Newtonsoft.Json.
The `Codebelt.Extensions.AspNetCore.Newtonsoft.Json.Formatters` namespace registers `NewtonsoftJsonFormatterOptions` and the `IHttpExceptionDescriptorResponseFormatter` into ASP.NET Core's `IServiceCollection`. Start with `AddNewtonsoftJsonExceptionResponseFormatter` on `IServiceCollection` to wire up exception-to-JSON formatting with the response formatter registered; use `AddNewtonsoftJsonFormatterOptions` when you only need the options registered without the response formatter.

[!INCLUDE [availability-modern](../../includes/availability-modern.md)]

Complements: [Codebelt.Extensions.Newtonsoft.Json namespace](/api/extensions/jsonnet/Cuemon.Extensions.Newtonsoft.Json.html) 📘

### Extension Methods
### Extension Members

|Type|Ext|Methods|
|--:|:-:|---|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
uid: Codebelt.Extensions.AspNetCore.Newtonsoft.Json
summary: *content
---
The `Codebelt.Extensions.AspNetCore.Newtonsoft.Json` namespace wires Newtonsoft.Json into ASP.NET Core's dependency injection container so exception responses serialize through `IHttpExceptionDescriptorResponseFormatter` without manual wiring. Use it in any ASP.NET Core project where structured JSON error responses must flow through Newtonsoft.Json serialization. Call `AddMinimalNewtonsoftJsonOptions` on `IServiceCollection` to register `NewtonsoftJsonFormatterOptions` and the response formatter in a single call — this is the primary entry point in this namespace.

[!INCLUDE [availability-modern](../../includes/availability-modern.md)]

Complements: [Codebelt.Extensions.Newtonsoft.Json namespace](/api/extensions/jsonnet/Cuemon.Extensions.Newtonsoft.Json.html) 📘

### Extension Members

|Type|Ext|Methods|
|--:|:-:|---|
|IServiceCollection|⬇️|`AddMinimalNewtonsoftJsonOptions`|
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
uid: Codebelt.Extensions.Newtonsoft.Json.Converters
summary: *content
---
The `Codebelt.Extensions.Newtonsoft.Json.Converters` namespace contains both types and extension methods that complements the `Newtonsoft.Json.Converters` namespace.
The `Codebelt.Extensions.Newtonsoft.Json.Converters` namespace provides Newtonsoft.Json converters for enums, exceptions, failures, and data pairs, plus extension methods that register them into a `JsonConverter` collection. Use `AddStringEnumConverter` to serialize enums as their string names, `AddExceptionConverter` to write exceptions with configurable stack-trace and data inclusion, and the remaining converters for specialized `TransientFaultException`, `DataPair`, and `Failure` serialization.

[!INCLUDE [availability-default](../../includes/availability-default.md)]

Complements: [Newtonsoft.Json.Converters namespace](https://www.newtonsoft.com/json/help/html/N_Newtonsoft_Json_Converters.htm) 🔗

### Extension Methods
### Extension Members

|Type|Ext|Methods|
|--:|:-:|---|
|JsonConverter|⬇️|`AddStringEnumConverter`, `AddStringFlagsEnumConverter`, `AddExceptionDescriptorConverterOf{T}`, `AddExceptionConverter`, `AddDataPairConverter`, `AddTransientFaultExceptionConverter`, `AddFailureConverter`|
|ICollection<JsonConverter>|⬇️|`AddStringEnumConverter`, `AddStringFlagsEnumConverter`, `AddExceptionDescriptorConverterOf<T>`, `AddExceptionConverter`, `AddDataPairConverter`, `AddTransientFaultExceptionConverter`, `AddFailureConverter`|
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
uid: Codebelt.Extensions.Newtonsoft.Json.Formatters
summary: *content
---
The `Codebelt.Extensions.Newtonsoft.Json.Formatters` namespace contains types that are used to serialize and deserialize objects into and from JSON format using a generic signature.
The `Codebelt.Extensions.Newtonsoft.Json.Formatters` namespace provides `NewtonsoftJsonFormatter`, a stream-based serializer/deserializer that converts objects to and from JSON using Newtonsoft.Json, and `NewtonsoftJsonFormatterOptions` for configuring `JsonSerializerSettings`, supported media types, sensitivity details, and converter collections. Use `NewtonsoftJsonFormatter` when you need JSON serialization with a generic `StreamFormatter<T>` pipeline, such as in ASP.NET Core input/output formatters.

[!INCLUDE [availability-default](../../includes/availability-default.md)]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
uid: Codebelt.Extensions.Newtonsoft.Json.Serialization
summary: *content
---
The `Codebelt.Extensions.Newtonsoft.Json.Serialization` namespace contains extension methods that help in the serialization process.
The `Codebelt.Extensions.Newtonsoft.Json.Serialization` namespace resolves the configured `NamingStrategy` from any `IContractResolver`, saving you from type-checking the resolver at runtime. Call `ResolveNamingStrategyOrDefault` on an `IContractResolver` to extract its `NamingStrategy` (for example, `CamelCaseNamingStrategy` from `CamelCasePropertyNamesContractResolver`) or fall back to a default when none is set. This is the sole entry point in this namespace.

[!INCLUDE [availability-default](../../includes/availability-default.md)]

Complements: [Newtonsoft.Json.Serialization namespace](https://www.newtonsoft.com/json/help/html/N_Newtonsoft_Json_Serialization.htm) 🔗

### Extension Methods
### Extension Members

|Type|Ext|Methods|
|--:|:-:|---|
Expand Down
7 changes: 3 additions & 4 deletions .docfx/api/namespaces/Codebelt.Extensions.Newtonsoft.Json.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
uid: Codebelt.Extensions.Newtonsoft.Json
summary: *content
---
The `Codebelt.Extensions.Newtonsoft.Json` namespace contains both types and extension methods that complements the `Newtonsoft.Json` namespace by adding new ways of working with JSON; both in terms of serialization and parsing.
The `Codebelt.Extensions.Newtonsoft.Json` namespace helps you focus on your domain objects instead of writing boilerplate JSON serialization code. When you need dynamic converter creation, custom contract resolution per property, or rich JSON parsing that surfaces path and value details, this namespace provides the building blocks. Start with `JData.ReadAll` to parse JSON into navigable `JDataResult` sequences; use `JsonConverterFactory.Create` when you need a custom `JsonConverter` from a delegate; reach for `DynamicContractResolver.Create` when per-property contract customization is required.

[!INCLUDE [availability-default](../../includes/availability-default.md)]

Complements: [Newtonsoft.Json namespace](https://www.newtonsoft.com/json/help/html/N_Newtonsoft_Json.htm) 🔗

### Extension Methods
### Extension Members

|Type|Ext|Methods|
|--:|:-:|---|
|JDataResult|⬇️|`Flatten`, `ExtractArrayValues`, `ExtractObjectValues`|
|JsonReader|⬇️|`ToHierarchy`|
|IEnumerable&lt;JDataResult&gt;|⬇️|`Flatten`, `ExtractArrayValues`, `ExtractObjectValues`|
|JsonSerializerSettings|⬇️|`ApplyToDefaultSettings`|
|JsonWriter|⬇️|`WriteObject`, `WritePropertyName`|
|Validator|⬇️|`InvalidJsonDocument`|
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
uid: Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json.JsonSerializationInputFormatter
example:
- *content
---

Create `JsonSerializationInputFormatter` directly when you want to confirm which JSON media types and encodings MVC will accept before the formatter is inserted into `MvcOptions`.

```csharp
// Program.cs
using System;
using System.Linq;
using System.Net.Http.Headers;
using Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json;
using Codebelt.Extensions.Newtonsoft.Json.Formatters;

var options = new NewtonsoftJsonFormatterOptions();
options.SupportedMediaTypes = options.SupportedMediaTypes
.Append(MediaTypeHeaderValue.Parse("application/vnd.weather+json"))
.ToArray();

var formatter = new JsonSerializationInputFormatter(options);

Console.WriteLine(formatter.SupportedMediaTypes.Any(mediaType => mediaType == "application/json"));
Console.WriteLine(formatter.SupportedMediaTypes.Any(mediaType => mediaType == "application/vnd.weather+json"));
Console.WriteLine(formatter.SupportedEncodings.Count);
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
uid: Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json.JsonSerializationMvcOptionsSetup
example:
- *content
---

Use `JsonSerializationMvcOptionsSetup` when you want `MvcOptions` to prefer the Newtonsoft.Json formatters first while preserving any later formatters MVC already knows about.

```csharp
// Program.cs
using System;
using System.Linq;
using Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json;
using Codebelt.Extensions.Newtonsoft.Json.Formatters;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;

var services = new ServiceCollection();
services.Configure<NewtonsoftJsonFormatterOptions>(options => options.Settings.Formatting = Formatting.None);
services.AddSingleton<IConfigureOptions<MvcOptions>, JsonSerializationMvcOptionsSetup>();

var provider = services.BuildServiceProvider();
var mvcOptions = new MvcOptions();

foreach (var configurator in provider.GetServices<IConfigureOptions<MvcOptions>>())
{
configurator.Configure(mvcOptions);
}

Console.WriteLine(mvcOptions.OutputFormatters[0] is JsonSerializationOutputFormatter);
Console.WriteLine(mvcOptions.InputFormatters[0] is JsonSerializationInputFormatter);
Console.WriteLine(mvcOptions.OutputFormatters.OfType<JsonSerializationOutputFormatter>().Count());
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
uid: Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json.JsonSerializationOutputFormatter
example:
- *content
---

Create `JsonSerializationOutputFormatter` directly when you need to inspect the response media types MVC will negotiate after you customize the shared Newtonsoft.Json formatter options.

```csharp
// Program.cs
using System;
using System.Linq;
using System.Net.Http.Headers;
using Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json;
using Codebelt.Extensions.Newtonsoft.Json.Formatters;

var options = new NewtonsoftJsonFormatterOptions();
options.SupportedMediaTypes = options.SupportedMediaTypes
.Append(MediaTypeHeaderValue.Parse("application/vnd.invoice+json"))
.ToArray();

var formatter = new JsonSerializationOutputFormatter(options);

Console.WriteLine(formatter.SupportedMediaTypes.Any(mediaType => mediaType == "application/problem+json"));
Console.WriteLine(formatter.SupportedMediaTypes.Any(mediaType => mediaType == "application/vnd.invoice+json"));
Console.WriteLine(formatter.SupportedEncodings.Count);
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
uid: Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json.JsonSerializerSettingsExtensions
example:
- *content
---

Use `JsonSerializerSettingsExtensions.Use<T>` when you already have a `JsonSerializerSettings` instance and want to copy a reusable formatter profile into it before MVC applies the settings to input and output formatters.

```csharp
// Program.cs
using System;
using Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json;
using Cuemon.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

var target = new JsonSerializerSettings();

target.Use<MvcJsonSerializerSettings>(settings =>
{
settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});

Console.WriteLine(target.Formatting);
Console.WriteLine(target.NullValueHandling);
Console.WriteLine(target.ReferenceLoopHandling);
Console.WriteLine(target.ContractResolver is CamelCasePropertyNamesContractResolver);

sealed class MvcJsonSerializerSettings : JsonSerializerSettings, IParameterObject
{
public MvcJsonSerializerSettings()
{
Formatting = Formatting.Indented;
NullValueHandling = NullValueHandling.Ignore;
ContractResolver = new CamelCasePropertyNamesContractResolver();
}
}
```

---
uid: Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json.JsonSerializerSettingsExtensions.Use``1(Newtonsoft.Json.JsonSerializerSettings,System.Action{``0})
example:
- *content
---

Call `Use<T>` on the target `JsonSerializerSettings` instance when a formatter profile already captures the JSON conventions you want MVC to reuse.

```csharp
// Program.cs
using System;
using Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json;
using Cuemon.Configuration;
using Newtonsoft.Json;

var target = new JsonSerializerSettings();
target.Use<MvcJsonSerializerSettings>();

Console.WriteLine(target.Formatting);
Console.WriteLine(target.NullValueHandling);

sealed class MvcJsonSerializerSettings : JsonSerializerSettings, IParameterObject
{
public MvcJsonSerializerSettings()
{
Formatting = Formatting.Indented;
NullValueHandling = NullValueHandling.Ignore;
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
uid: Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json.MvcBuilderExtensions
example:
- *content
---

Call `MvcBuilderExtensions.AddNewtonsoftJsonFormatters` when an `IMvcBuilder` should register the shared Newtonsoft.Json options and move the Newtonsoft.Json input and output formatters to the front of MVC's formatter lists.

```csharp
// Program.cs
using System;
using Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json;
using Codebelt.Extensions.Newtonsoft.Json.Formatters;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;

var services = new ServiceCollection();
services.AddControllers()
.AddNewtonsoftJsonFormatters(options =>
{
options.Settings.Formatting = Formatting.None;
});

var provider = services.BuildServiceProvider();
var formatterOptions = provider.GetRequiredService<IOptions<NewtonsoftJsonFormatterOptions>>().Value;
var mvcOptions = new MvcOptions();

foreach (var configurator in provider.GetServices<IConfigureOptions<MvcOptions>>())
{
configurator.Configure(mvcOptions);
}

Console.WriteLine(formatterOptions.Settings.Formatting);
Console.WriteLine(mvcOptions.OutputFormatters[0] is JsonSerializationOutputFormatter);
Console.WriteLine(mvcOptions.InputFormatters[0] is JsonSerializationInputFormatter);

var optionServices = new ServiceCollection();
optionServices.AddControllers()
.AddNewtonsoftJsonFormattersOptions(options =>
{
options.SynchronizeWithJsonConvert = true;
});

var optionProvider = optionServices.BuildServiceProvider();
var optionOnly = optionProvider.GetRequiredService<IOptions<NewtonsoftJsonFormatterOptions>>().Value;
Console.WriteLine(optionOnly.SynchronizeWithJsonConvert);
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
uid: Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json.MvcCoreBuilderExtensions
example:
- *content
---

Call `MvcCoreBuilderExtensions.AddNewtonsoftJsonFormattersOptions` when an `IMvcCoreBuilder` already owns the MVC core services and you need the shared Newtonsoft.Json formatter options plus the exception-response formatter without inserting the MVC input and output formatters.

```csharp
// Program.cs
using System;
using Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json;
using Codebelt.Extensions.Newtonsoft.Json.Formatters;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

var services = new ServiceCollection();
services.AddMvcCore()
.AddNewtonsoftJsonFormatters(options =>
{
options.Settings.DateFormatString = "yyyy-MM-dd";
});

var provider = services.BuildServiceProvider();
var formatterOptions = provider.GetRequiredService<IOptions<NewtonsoftJsonFormatterOptions>>().Value;

Console.WriteLine(formatterOptions.Settings.DateFormatString);

var optionServices = new ServiceCollection();
optionServices.AddMvcCore()
.AddNewtonsoftJsonFormattersOptions(options =>
{
options.SynchronizeWithJsonConvert = true;
});

var optionProvider = optionServices.BuildServiceProvider();
var optionOnly = optionProvider.GetRequiredService<IOptions<NewtonsoftJsonFormatterOptions>>().Value;

Console.WriteLine(optionOnly.SynchronizeWithJsonConvert);
```
Loading
Loading