-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathSmartFlagEnumValueFormatter.cs
92 lines (88 loc) · 3.92 KB
/
SmartFlagEnumValueFormatter.cs
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
namespace Ardalis.SmartEnum.Utf8Json
{
using global::Utf8Json;
using global::Utf8Json.Internal;
using System;
/// <summary>
///
/// </summary>
/// <typeparam name="TEnum"></typeparam>
/// <typeparam name="TValue"></typeparam>
public class SmartFlagEnumValueFormatter<TEnum, TValue> : IJsonFormatter<TEnum>
where TEnum : SmartFlagEnum<TEnum, TValue>
where TValue : struct, IEquatable<TValue>, IComparable<TValue>
{
/// <summary>
///
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <param name="formatterResolver"></param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public void Serialize(ref JsonWriter writer, TEnum value, IJsonFormatterResolver formatterResolver)
{
if (value is null)
{
writer.WriteNull();
}
else if (typeof(TValue) == typeof(byte))
writer.WriteByte((byte)(object)value.Value);
else if (typeof(TValue) == typeof(sbyte))
writer.WriteSByte((sbyte)(object)value.Value);
else if (typeof(TValue) == typeof(short))
writer.WriteInt16((short)(object)value.Value);
else if (typeof(TValue) == typeof(ushort))
writer.WriteUInt16((ushort)(object)value.Value);
else if (typeof(TValue) == typeof(int))
writer.WriteInt32((int)(object)value.Value);
else if (typeof(TValue) == typeof(uint))
writer.WriteUInt32((uint)(object)value.Value);
else if (typeof(TValue) == typeof(long))
writer.WriteInt64((long)(object)value.Value);
else if (typeof(TValue) == typeof(ulong))
writer.WriteUInt64((ulong)(object)value.Value);
else if (typeof(TValue) == typeof(float))
writer.WriteSingle((float)(object)value.Value);
else if (typeof(TValue) == typeof(double))
writer.WriteDouble((double)(object)value.Value);
else
throw new ArgumentOutOfRangeException(typeof(TValue).ToString(), $"{typeof(TValue).Name} is not supported.");
}
/// <summary>
///
/// </summary>
/// <param name="reader"></param>
/// <param name="formatterResolver"></param>
/// <returns></returns>
public TEnum Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
{
if (reader.ReadIsNull())
return null;
return SmartFlagEnum<TEnum, TValue>.DeserializeValue(ReadValue(ref reader));
}
TValue ReadValue(ref JsonReader reader)
{
if (typeof(TValue) == typeof(byte))
return (TValue)(object)reader.ReadByte();
if (typeof(TValue) == typeof(sbyte))
return (TValue)(object)reader.ReadSByte();
if (typeof(TValue) == typeof(short))
return (TValue)(object)reader.ReadInt16();
if (typeof(TValue) == typeof(ushort))
return (TValue)(object)reader.ReadUInt16();
if (typeof(TValue) == typeof(int))
return (TValue)(object)reader.ReadInt32();
if (typeof(TValue) == typeof(uint))
return (TValue)(object)reader.ReadUInt32();
if (typeof(TValue) == typeof(long))
return (TValue)(object)reader.ReadInt64();
if (typeof(TValue) == typeof(ulong))
return (TValue)(object)reader.ReadUInt64();
if (typeof(TValue) == typeof(float))
return (TValue)(object)reader.ReadSingle();
if (typeof(TValue) == typeof(double))
return (TValue)(object)reader.ReadDouble();
throw new ArgumentOutOfRangeException(typeof(TValue).ToString(), $"{typeof(TValue).Name} is not supported.");
}
}
}