Files
SocialPub/PrivaPub/Infrastructure/Json/UnregisteredJson.cs
T

26 lines
907 B
C#
Raw Normal View History

using System.Collections.Concurrent;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using Serilog;
namespace PrivaPub.Infrastructure.Json
{
public sealed class UnregisteredJson : IJsonTypeInfoResolver
{
public static readonly UnregisteredJson Instance = new();
static readonly DefaultJsonTypeInfoResolver Reflection = new();
static readonly ConcurrentDictionary<Type, byte> Reported = new();
public static bool Strict { get; set; }
public JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options)
{
if (Strict)
throw new NotSupportedException($"{type.FullName} is not registered in a JsonSerializerContext (HR0306).");
if (Reported.TryAdd(type, 0))
Log.ForContext<UnregisteredJson>().Error("{Type} is serialized without a JsonSerializerContext registration (HR0306)", type.FullName);
return Reflection.GetTypeInfo(type, options);
}
}
}