Open Silnar opened 4 years ago
I also run into this when trying to de-serialize ~0UL
for a [Flags]
enum.
The basic problem seems to be that, when the numeric value of an enum is larger than long.MaxValue
, JsonReader.Value
is a BigInteger
and the enum
deserialization code isn't handling this.
For a fixed version of StringEnumConverter
that handles ulong
values larger than long.MaxValue
see JSON.NET Unable to deserialize ulong flag type enum on stack overflow. Of course that converter handles serialization and deserialization of enums as strings. If you only want numeric serialization you could make a custom JsonConverter
for all enum
types whose underlying type is ulong
that checks for and handles BigInteger
values in a similar way.
Serialization of ulong enum values works fine, but deserialization fails on large values.
Here is an example code that serializes and then deserializes large enum value:
It throws the following error:
And here is a link .NET Fiddle so you can check it by yourself.
Everything works fine for lower values (
Type.Alpha
andType.Beta
), but crashes for larger values (Type.Gamma
andType.Omega
).