-
-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathSmartEnumValueConverter.cs
More file actions
168 lines (158 loc) · 7.27 KB
/
SmartEnumValueConverter.cs
File metadata and controls
168 lines (158 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
namespace Ardalis.SmartEnum.SystemTextJson
{
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
/// <summary>
///
/// </summary>
/// <typeparam name="TEnum"></typeparam>
/// <typeparam name="TValue"></typeparam>
public class SmartEnumValueConverter<TEnum, TValue> : JsonConverter<TEnum>
where TEnum : SmartEnum<TEnum, TValue>
where TValue : IEquatable<TValue>, IComparable<TValue>
{
/// <summary>
///
/// </summary>
/// <param name="reader"></param>
/// <param name="typeToConvert"></param>
/// <param name="options"></param>
/// <returns></returns>
public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
return null;
return GetFromValue(ReadValue(ref reader));
}
/// <summary>
///
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <param name="options"></param>
public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options)
{
if (value == null)
writer.WriteNullValue();
else if (typeof(TValue) == typeof(bool))
writer.WriteBooleanValue((bool)(object)value.Value);
else if (typeof(TValue) == typeof(short))
writer.WriteNumberValue((short)(object)value.Value);
else if (typeof(TValue) == typeof(int))
writer.WriteNumberValue((int)(object)value.Value);
else if (typeof(TValue) == typeof(double))
writer.WriteNumberValue((double)(object)value.Value);
else if (typeof(TValue) == typeof(decimal))
writer.WriteNumberValue((decimal)(object)value.Value);
else if (typeof(TValue) == typeof(ulong))
writer.WriteNumberValue((ulong)(object)value.Value);
else if (typeof(TValue) == typeof(uint))
writer.WriteNumberValue((uint)(object)value.Value);
else if (typeof(TValue) == typeof(float))
writer.WriteNumberValue((float)(object)value.Value);
else if (typeof(TValue) == typeof(long))
writer.WriteNumberValue((long)(object)value.Value);
else
writer.WriteStringValue(value.Value.ToString());
}
private TEnum GetFromValue(TValue value)
{
try
{
return SmartEnum<TEnum, TValue>.FromValue(value);
}
catch (Exception ex)
{
throw new JsonException($"Error converting value '{value}' to a smart enum.", ex);
}
}
private TValue ReadValue(ref Utf8JsonReader reader)
{
if (typeof(TValue) == typeof(bool))
return (TValue)(object)reader.GetBoolean();
if (typeof(TValue) == typeof(byte))
return (TValue)(object)reader.GetByte();
if (typeof(TValue) == typeof(sbyte))
return (TValue)(object)reader.GetSByte();
if (typeof(TValue) == typeof(short))
return (TValue)(object)reader.GetInt16();
if (typeof(TValue) == typeof(ushort))
return (TValue)(object)reader.GetUInt16();
if (typeof(TValue) == typeof(int))
return (TValue)(object)reader.GetInt32();
if (typeof(TValue) == typeof(uint))
return (TValue)(object)reader.GetUInt32();
if (typeof(TValue) == typeof(long))
return (TValue)(object)reader.GetInt64();
if (typeof(TValue) == typeof(ulong))
return (TValue)(object)reader.GetUInt64();
if (typeof(TValue) == typeof(float))
return (TValue)(object)reader.GetSingle();
if (typeof(TValue) == typeof(double))
return (TValue)(object)reader.GetDouble();
if (typeof(TValue) == typeof(string))
return (TValue)(object)reader.GetString();
if (typeof(TValue) == typeof(Guid))
return (TValue)(object)Guid.Parse(reader.GetString());
throw new ArgumentOutOfRangeException(typeof(TValue).ToString(), $"{typeof(TValue).Name} is not supported.");
}
/// <summary>
///
/// </summary>
/// <param name="reader"></param>
/// <param name="typeToConvert"></param>
/// <param name="options"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public override TEnum ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.PropertyName)
{
throw new ArgumentException(
$"Unexpected token {reader.TokenType} when parsing a dictionary with smart enum key.");
}
return GetFromValue(ReadPropertyName(ref reader));
}
/// <summary>
///
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <param name="options"></param>
public override void WriteAsPropertyName(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options)
{
writer.WritePropertyName(value.Value?.ToString() ?? string.Empty);
}
private TValue ReadPropertyName(ref Utf8JsonReader reader)
{
if (typeof(TValue) == typeof(bool))
return (TValue)(object)Convert.ToBoolean(reader.GetString());
if (typeof(TValue) == typeof(byte))
return (TValue)(object)Convert.ToByte(reader.GetString());
if (typeof(TValue) == typeof(sbyte))
return (TValue)(object)Convert.ToSByte(reader.GetString());
if (typeof(TValue) == typeof(short))
return (TValue)(object)Convert.ToInt16(reader.GetString());
if (typeof(TValue) == typeof(ushort))
return (TValue)(object)Convert.ToUInt16(reader.GetString());
if (typeof(TValue) == typeof(int))
return (TValue)(object)Convert.ToInt32(reader.GetString());
if (typeof(TValue) == typeof(uint))
return (TValue)(object)Convert.ToUInt32(reader.GetString());
if (typeof(TValue) == typeof(long))
return (TValue)(object)Convert.ToInt64(reader.GetString());
if (typeof(TValue) == typeof(ulong))
return (TValue)(object)Convert.ToUInt64(reader.GetString());
if (typeof(TValue) == typeof(float))
return (TValue)(object)Convert.ToSingle(reader.GetString());
if (typeof(TValue) == typeof(double))
return (TValue)(object)Convert.ToDouble(reader.GetString());
if (typeof(TValue) == typeof(Guid))
return (TValue)(object)Guid.Parse(reader.GetString());
if (typeof(TValue) == typeof(string))
return (TValue)(object)reader.GetString();
throw new ArgumentOutOfRangeException(typeof(TValue).ToString(), $"{typeof(TValue).Name} is not supported.");
}
}
}