70 lines
3.1 KiB
C#
70 lines
3.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace SmartNav.Model.Converter;
|
|
|
|
public abstract class EnumConverter<T> : JsonConverter<T> where T : Enum
|
|
{
|
|
private readonly ReadOnlyDictionary<T, string> _enumToString;
|
|
|
|
private readonly ReadOnlyDictionary<string, T> _stringToEnum;
|
|
|
|
protected EnumConverter(IReadOnlyDictionary<T, string> values)
|
|
{
|
|
_enumToString = ((values is IDictionary<T, string> dictionary) ? new ReadOnlyDictionary<T, string>(dictionary) : new ReadOnlyDictionary<T, string>(values.ToDictionary<KeyValuePair<T, string>, T, string>((KeyValuePair<T, string> x) => x.Key, (KeyValuePair<T, string> x) => x.Value)));
|
|
_stringToEnum = new ReadOnlyDictionary<string, T>(_enumToString.ToDictionary<KeyValuePair<T, string>, string, T>((KeyValuePair<T, string> x) => x.Value, (KeyValuePair<T, string> x) => x.Key));
|
|
}
|
|
|
|
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
int num = ((reader.TokenType == JsonTokenType.String) ? 1875836266 : 1875836268);
|
|
string text = default(string);
|
|
T value = default(T);
|
|
while (true)
|
|
{
|
|
int num2 = num;
|
|
int num3 = 1875836271;
|
|
int num4 = num2;
|
|
switch ((num4 | num3) - (num4 & num3))
|
|
{
|
|
case 4:
|
|
num = (_stringToEnum.TryGetValue(text, out value) ? 1875836270 : 1875836265);
|
|
break;
|
|
case 6:
|
|
throw _200F_200D_200D_200B_206E_202D_206A_200E_202A_202D_206B_206A_200C_202C_200C_200F_200E_202B_200B_202D_202C_202A_200C_200C_200E_200C_202C_202E_200F_202D_200E_206C_202C_206F_202E_202E_206B_206A_202A_202A_202E();
|
|
case 0:
|
|
num = 1875836268;
|
|
break;
|
|
case 2:
|
|
throw _200F_200D_200D_200B_206E_202D_206A_200E_202A_202D_206B_206A_200C_202C_200C_200F_200E_202B_200B_202D_202C_202A_200C_200C_200E_200C_202C_202E_200F_202D_200E_206C_202C_206F_202E_202E_206B_206A_202A_202A_202E();
|
|
case 1:
|
|
return value;
|
|
case 3:
|
|
throw _200F_200D_200D_200B_206E_202D_206A_200E_202A_202D_206B_206A_200C_202C_200C_200F_200E_202B_200B_202D_202C_202A_200C_200C_200E_200C_202C_202E_200F_202D_200E_206C_202C_206F_202E_202E_206B_206A_202A_202A_202E();
|
|
case 5:
|
|
text = reader.GetString();
|
|
num = ((text != null) ? 1875836267 : 1875836269);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
|
|
{
|
|
_202B_206D_206A_202B_200B_200C_202C_206A_206C_200D_200C_206C_202B_206A_200C_200E_206F_200D_202B_202A_206E_200F_206F_200B_202B_206D_200E_206A_200E_202E_202D_202C_200E_202A_200E_202D_200D_202A_206C_202A_202E(writer, _enumToString[value]);
|
|
}
|
|
|
|
static JsonException _200F_200D_200D_200B_206E_202D_206A_200E_202A_202D_206B_206A_200C_202C_200C_200F_200E_202B_200B_202D_202C_202A_200C_200C_200E_200C_202C_202E_200F_202D_200E_206C_202C_206F_202E_202E_206B_206A_202A_202A_202E()
|
|
{
|
|
return new JsonException();
|
|
}
|
|
|
|
static void _202B_206D_206A_202B_200B_200C_202C_206A_206C_200D_200C_206C_202B_206A_200C_200E_206F_200D_202B_202A_206E_200F_206F_200B_202B_206D_200E_206A_200E_202E_202D_202C_200E_202A_200E_202D_200D_202A_206C_202A_202E(Utf8JsonWriter P_0, string P_1)
|
|
{
|
|
P_0.WriteStringValue(P_1);
|
|
}
|
|
}
|