return Enum.TryParse(enumString, out value);
}
- if (reader.TokenType != JsonTokenType.Number ||
- !reader.TryGetUInt64(out ulong ulongValue))
+ if (reader.TokenType != JsonTokenType.Number)
{
value = default;
return false;
}
- value = (TValue)Enum.ToObject(valueType, ulongValue);
- return true;
+ if (s_isUint64)
+ {
+ if (reader.TryGetUInt64(out ulong ulongValue))
+ {
+ value = (TValue)Enum.ToObject(valueType, ulongValue);
+ return true;
+ }
+ }
+ else if (reader.TryGetInt64(out long longValue))
+ {
+ value = (TValue)Enum.ToObject(valueType, longValue);
+ return true;
+ }
+
+ value = default;
+ return false;
}
public override void Write(TValue value, Utf8JsonWriter writer)
@"""MyEnum"" : 2" +
@"}";
+ private static readonly string s_jsonInt64EnumMin =
+ @"{" +
+ @"""MyInt64Enum"" : " + long.MinValue +
+ @"}";
+
+ private static readonly string s_jsonInt64EnumMax =
+ @"{" +
+ @"""MyInt64Enum"" : " + long.MaxValue +
+ @"}";
+
+ private static readonly string s_jsonUInt64EnumMax =
+ @"{" +
+ @"""MyUInt64Enum"" : " + ulong.MaxValue +
+ @"}";
+
[Fact]
public static void EnumAsStringFail()
{
SimpleTestClass obj = JsonSerializer.Parse<SimpleTestClass>(s_jsonIntEnum);
Assert.Equal(SampleEnum.Two, obj.MyEnum);
}
+
+ [Fact]
+ public static void EnumAsInt64Min()
+ {
+ SimpleTestClass obj = JsonSerializer.Parse<SimpleTestClass>(s_jsonInt64EnumMin);
+ Assert.Equal(SampleInt64Enum.Min, obj.MyInt64Enum);
+ }
+
+ [Fact]
+ public static void EnumAsInt64Max()
+ {
+ SimpleTestClass obj = JsonSerializer.Parse<SimpleTestClass>(s_jsonInt64EnumMax);
+ Assert.Equal(SampleInt64Enum.Max, obj.MyInt64Enum);
+ }
+
+ [Fact]
+ public static void EnumAsUInt64Max()
+ {
+ SimpleTestClass obj = JsonSerializer.Parse<SimpleTestClass>(s_jsonUInt64EnumMax);
+ Assert.Equal(SampleUInt64Enum.Max, obj.MyUInt64Enum);
+ }
}
}
public DateTime MyDateTime { get; set; }
public DateTimeOffset MyDateTimeOffset { get; set; }
public SampleEnum MyEnum { get; set; }
+ public SampleInt64Enum MyInt64Enum { get; set; }
+ public SampleUInt64Enum MyUInt64Enum { get; set; }
public short[] MyInt16Array { get; set; }
public int[] MyInt32Array { get; set; }
public long[] MyInt64Array { get; set; }