Table of Contents

Class DynamicJsonConverter

Namespace
Cuemon.Extensions.Text.Json
Assembly
Cuemon.Extensions.Text.Json.dll

Provides a factory based way to create and wrap an JsonConverter implementation.

public static class DynamicJsonConverter
Inheritance
DynamicJsonConverter

Examples

The following example shows how to register a custom JSON converter for the Version type using DynamicJsonConverter. It serializes a version to JSON and deserializes it back, printing each result.

using System;
using System.Text.Json;
using Cuemon.Extensions.Text.Json;

namespace Cuemon.Extensions.Text.Json;

public class DynamicJsonConverterExample
{
    public void Demonstrate()
    {
        var options = new JsonSerializerOptions();
        options.Converters.Add(DynamicJsonConverter.Create<Version>(
            writer: (utf8Writer, value, _) => utf8Writer.WriteStringValue(value.ToString()),
            reader: (ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions jsonOptions) => Version.Parse(reader.GetString())
        ));

        var version = new Version("5.0.0");
        string json = JsonSerializer.Serialize(version, options);
        Console.WriteLine(json);

        var deserialized = JsonSerializer.Deserialize<Version>("\"6.0.0\"", options);
        Console.WriteLine(deserialized);
    }
}

Methods

Create(Func<Type, bool>, Func<Type, JsonSerializerOptions, JsonConverter>)

Creates a dynamic instance of an JsonConverter implementation wrapping CanConvert(Type) through predicate and CreateConverter(Type, JsonSerializerOptions) through converterFactory.

public static JsonConverter Create(Func<Type, bool> predicate, Func<Type, JsonSerializerOptions, JsonConverter> converterFactory)

Parameters

predicate Func<Type, bool>

The function delegate that validates if a given Type can be converted to and from JSON.

converterFactory Func<Type, JsonSerializerOptions, JsonConverter>

The function delegate that converts a given Type to its JSON representation using a factory pattern.

Returns

JsonConverter

An JsonConverter implementation of Type.

Create(Type, Func<Type, JsonSerializerOptions, JsonConverter>)

Creates a dynamic instance of an JsonConverter implementation wrapping CanConvert(Type) through typeToConvert and CreateConverter(Type, JsonSerializerOptions) through converterFactory.

public static JsonConverter Create(Type typeToConvert, Func<Type, JsonSerializerOptions, JsonConverter> converterFactory)

Parameters

typeToConvert Type

The type of the object to convert.

converterFactory Func<Type, JsonSerializerOptions, JsonConverter>

The function delegate that converts typeToConvert to its JSON representation using a factory pattern.

Returns

JsonConverter

An JsonConverter implementation of typeToConvert.

Create<T>(Utf8JsonWriterAction<T>, Utf8JsonReaderFunc<T>)

Creates a dynamic instance of an JsonConverter implementation wrapping Write(Utf8JsonWriter, T, JsonSerializerOptions) through writer and Read(ref Utf8JsonReader, Type, JsonSerializerOptions) through reader.

public static JsonConverter Create<T>(Utf8JsonWriterAction<T> writer = null, Utf8JsonReaderFunc<T> reader = null)

Parameters

writer Utf8JsonWriterAction<T>

The delegate that converts T to its JSON representation.

reader Utf8JsonReaderFunc<T>

The function delegate that generates T from its JSON representation.

Returns

JsonConverter

An JsonConverter implementation of T.

Type Parameters

T

The type to implement an JsonConverter.

Create<T>(Func<Type, bool>, Utf8JsonWriterAction<T>, Utf8JsonReaderFunc<T>)

Creates a dynamic instance of an JsonConverter implementation wrapping Write(Utf8JsonWriter, T, JsonSerializerOptions) through writer and Read(ref Utf8JsonReader, Type, JsonSerializerOptions) through reader.

public static JsonConverter Create<T>(Func<Type, bool> predicate, Utf8JsonWriterAction<T> writer = null, Utf8JsonReaderFunc<T> reader = null)

Parameters

predicate Func<Type, bool>

The function delegate that validates if given Type can be converted to and from JSON.

writer Utf8JsonWriterAction<T>

The delegate that, when predicate returns true, converts T to its JSON representation.

reader Utf8JsonReaderFunc<T>

The function delegate that, when predicate returns true, generates T from its JSON representation.

Returns

JsonConverter

An JsonConverter implementation of T.

Type Parameters

T

The type to implement an JsonConverter.