Class EnumCacheJson<T>
The JSON converter that reads and writes a game enum value as its name in EnumCache.
The JSON form is a string, for example "imperius". The converter also reads and writes the value as a property name.
public class EnumCacheJson<T> : JsonConverter<T> where T : struct, Enum
Type Parameters
TThe enum type to convert.
- Inheritance
-
EnumCacheJson<T>
- Inherited Members
Remarks
Use this converter for enums that mods can extend, because EnumCache also has the names of the values that mods add.
Methods
CanConvert(Type)
Finds if the converter accepts a type.
The method gives true for T and for List<T> of T.
public override bool CanConvert(Type typeToConvert)
Parameters
typeToConvertTypeThe type to check.
Returns
Remarks
The Read(ref Utf8JsonReader, Type, JsonSerializerOptions) method can read only T. For lists, use EnumCacheListJson<T>.
Read(ref Utf8JsonReader, Type, JsonSerializerOptions)
Reads a JSON string and gets the enum value with that name from EnumCache.
If EnumCache does not have the name, EnumCache throws an exception.
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
Parameters
readerUtf8JsonReaderThe JSON reader, at a string token or a property name token.
typeToConvertTypeThe type to read. It must be
T.optionsJsonSerializerOptionsThe serializer options. The method does not use them.
Returns
- T
The enum value that has the name in the JSON string.
Exceptions
- NotSupportedException
typeToConvertis notT.
ReadAsPropertyName(ref Utf8JsonReader, Type, JsonSerializerOptions)
Reads a JSON property name and gets the enum value with that name from EnumCache.
The method calls Read(ref Utf8JsonReader, Type, JsonSerializerOptions).
public override T ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
Parameters
readerUtf8JsonReaderThe JSON reader, at a property name token.
typeToConvertTypeThe type to read. It must be
T.optionsJsonSerializerOptionsThe serializer options. The method does not use them.
Returns
- T
The enum value that has the name in the property name.
Write(Utf8JsonWriter, T, JsonSerializerOptions)
Writes the enum value as a JSON string with its name from EnumCache.
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
Parameters
writerUtf8JsonWriterThe JSON writer.
valueTThe enum value to write.
optionsJsonSerializerOptionsThe serializer options. The method does not use them.
WriteAsPropertyName(Utf8JsonWriter, T, JsonSerializerOptions)
Writes the enum value as a JSON property name with its name from EnumCache.
public override void WriteAsPropertyName(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
Parameters
writerUtf8JsonWriterThe JSON writer.
valueTThe enum value to write.
optionsJsonSerializerOptionsThe serializer options. The method does not use them.