JsonConverterExtensions Class
Definition
- Namespace
- Savvyio.Extensions.Text.Json
- Assemblies
- Savvyio.Extensions.Text.Json.dll
Extension methods for the JsonConverter class.
public static class JsonConverterExtensions
- Inheritance
-
objectJsonConverterExtensions
Examples
JsonConverterExtensions provides fluent methods for adding the complete set of Savvy I/O converters to JsonSerializerOptions.Converters. The prerequisite is an existing JsonSerializerOptions instance; each extension method returns the same ICollection<JsonConverter> so calls can be chained. The example chains all available registration methods and serializes an IMessage<T> to verify the converters compose correctly.
using Savvyio;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Savvyio.Extensions.Text.Json;
using Savvyio.Messaging;
namespace ExampleApp;
public static class JsonOptionsFactory
{
public static JsonSerializerOptions Create()
{
var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
options.Converters.RemoveAllOf(typeof(JsonConverter));
options.Converters.RemoveAllOf<JsonConverter>();
options.Converters.AddMetadataDictionaryConverter();
options.Converters.AddMessageConverter();
options.Converters.AddRequestConverter();
options.Converters.AddSingleValueObjectConverter();
options.Converters.AddDateTimeConverter();
options.Converters.AddDateTimeOffsetConverter();
var message = new Message<PlaceOrderCommand>(
"msg-001",
new Uri("urn:orders"),
nameof(PlaceOrderCommand),
new PlaceOrderCommand("SO-42"),
DateTime.UtcNow);
_ = JsonSerializer.Serialize(message, options);
return options;
}
}
public sealed record PlaceOrderCommand(string OrderId) : Request;
Methods
| Name | Description |
|---|---|
| AddDateTimeConverter(ICollection<JsonConverter>) | Adds a DateTime converter to the collection. |
| AddDateTimeOffsetConverter(ICollection<JsonConverter>) | Adds a DateTimeOffset converter to the collection. |
| AddMessageConverter(ICollection<JsonConverter>) | Adds an IMessage<T> converter (or derived) to the collection. |
| AddMetadataDictionaryConverter(ICollection<JsonConverter>) | Adds a IMetadataDictionary converter to the collection. |
| AddRequestConverter(ICollection<JsonConverter>) | Adds a IRequest converter to the collection. |
| AddSingleValueObjectConverter(ICollection<JsonConverter>) | Adds a SingleValueObject<T> converter to the collection. |
| RemoveAllOf(ICollection<JsonConverter>, params Type[]) | Removes one or more JsonConverter implementations where CanConvert(Type) evaluates |
| RemoveAllOf<T>(ICollection<JsonConverter>) | Removes one or more JsonConverter implementations where CanConvert(Type) evaluates |