Class EnumNameJson<TEnum>
The JSON converter that reads and writes an enum value as its C# member name.
The JSON form is a string, for example "Linear".
public class EnumNameJson<TEnum> : JsonConverter<TEnum> where TEnum : struct, Enum
Type Parameters
TEnumThe enum type to convert.
- Inheritance
-
JsonConverter<TEnum>EnumNameJson<TEnum>
- Inherited Members
Remarks
Use this converter for enums that mods cannot extend and that EnumCache does not have.
Methods
Read(ref Utf8JsonReader, Type, JsonSerializerOptions)
Reads a JSON string and gets the enum value with that member name. The name is not case-sensitive.
The method uses Enum.TryParse, so it also accepts numeric strings.
public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
Parameters
readerUtf8JsonReaderThe JSON reader, at a string token.
typeToConvertTypeThe type to read. The method does not use it.
optionsJsonSerializerOptionsThe serializer options. The method does not use them.
Returns
- TEnum
The enum value that has the name in the JSON string.
Exceptions
- JsonException
The JSON value is null or is not a member name of
TEnum. The message shows the valid names.
Write(Utf8JsonWriter, TEnum, JsonSerializerOptions)
Writes the enum value as a JSON string with its C# member name.
public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options)
Parameters
writerUtf8JsonWriterThe JSON writer.
valueTEnumThe enum value to write.
optionsJsonSerializerOptionsThe serializer options. The method does not use them.