<PackageReference Include="System.Text.Json" Version="9.0.1" />

MemoryConverterFactory

using System.Diagnostics.CodeAnalysis; namespace System.Text.Json.Serialization.Converters { [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] internal sealed class MemoryConverterFactory : JsonConverterFactory { public override bool CanConvert(Type typeToConvert) { if (!typeToConvert.IsGenericType || !typeToConvert.IsValueType) return false; Type genericTypeDefinition = typeToConvert.GetGenericTypeDefinition(); if (!(genericTypeDefinition == typeof(Memory<>))) return genericTypeDefinition == typeof(ReadOnlyMemory<>); return true; } public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) { Type obj = (typeToConvert.GetGenericTypeDefinition() == typeof(Memory<>)) ? typeof(MemoryConverter<>) : typeof(ReadOnlyMemoryConverter<>); Type type = typeToConvert.GetGenericArguments()[0]; return (JsonConverter)Activator.CreateInstance(obj.MakeGenericType(type)); } } }