Table of Contents

Class EnumCacheJson<T>

Namespace
PolyMod.Json
Assembly
PolyMod.dll

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

T

The 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

typeToConvert Type

The type to check.

Returns

bool

True if the type is T or List<T> of T, otherwise false.

Remarks

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

reader Utf8JsonReader

The JSON reader, at a string token or a property name token.

typeToConvert Type

The type to read. It must be T.

options JsonSerializerOptions

The serializer options. The method does not use them.

Returns

T

The enum value that has the name in the JSON string.

Exceptions

NotSupportedException

typeToConvert is not T.

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

reader Utf8JsonReader

The JSON reader, at a property name token.

typeToConvert Type

The type to read. It must be T.

options JsonSerializerOptions

The 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

writer Utf8JsonWriter

The JSON writer.

value T

The enum value to write.

options JsonSerializerOptions

The 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

writer Utf8JsonWriter

The JSON writer.

value T

The enum value to write.

options JsonSerializerOptions

The serializer options. The method does not use them.