English 中文(简体)
System.Text.Json JsonStringEnumConverter with custom fallback in case of deserialization failures
原标题:

I have a .NET 6 program that needs to deserialize a JSON string value (returned by external API) into a .NET enum.

The issue is that there are over 100 possible enum values (and more could be added without my knowledge), but I m only interested in a few of them. So I would like to define an enum type like this and deserialize all the values that I m not interested in to MyEnum.Unknown:

public enum MyEnum
{
    Unknown = 0,
    Value1,
    Value2,
    Value3,
    // only values that I m interested in will be defined
}

If I m using Newtonsoft.Json, I can do this quite easily with a custom JSON converter:

public class DefaultFallbackStringEnumConverter : StringEnumConverter
{
    private readonly object _defaultValue;

    public DefaultFallbackStringEnumConverter() : this(0)
    {
    }

    public DefaultFallbackStringEnumConverter(object defaultValue)
    {
        _defaultValue = defaultValue;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        try
        {
            return base.ReadJson(reader, objectType, existingValue, serializer);
        }
        catch (JsonException)
        {
            return _defaultValue;
        }
    }
}

But with System.Text.Json, I can t figure out how this can be done easily, because the JsonStringEnumConverter in STJ is actually a JsonConverterFactory that doesn t do the serialization itself (it throws exceptions in all overrides of JsonConverter as you can see here). Instead the factory creates EnumConverter<T>s that actually do the work, but EnumConverter<T> is internal so I can t even reference or inherit from it in user code.

Any idea how this can be done easily with STJ or is it not possible at all? Thanks a lot for the help!

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签