Class AdvancedEnumSerializer<E extends Enum<E>>
- Type Parameters:
E
- enum class
- All Implemented Interfaces:
FieldTypeSerializer<E>
Enum
serializer, which uses different tricks to serialize enum constants to user-friendly
values for configs, and also to read them properly.
Lets be real, every developer tried or is going to try to serialize enum constants, so they
are more user-friendly to be configured. Yet everyone who tried wasn't really able to go farther
than accepting this input: enum_constant
for an enum constant of ENUM_CONSTANT
.
As already mentioned, this serializer uses tricks in order to accept literally every output
possible. Say you have an enum constant of ACCEPT_ONLY_TRUE
. This serializer serializes
this constant as accept only true
, but upon deserialization, the serializer accepts
literally any possible way of combining these words. This basically means it will accept
only accept true
, accept true only
, etc., and of course the input as a regular enum
constant e.g. ACCEPT_ONLY_TRUE
and accept_only_true
.
Example: Say you have an enum of:
public enum Option {
ACCEPT_TRUE,
ACCEPT_FALSE,
ACCEPT_BOTH,
DO_NOT_ACCEPT_TRUE,
DO_NOT_ACCEPT_FALSE,
DO_NOT_ACCEPT_BOTH
}
and you want to use AdvancedEnumSerializer to serialize it, you just register it using the SerializerRegistry
. An example of this is:
SerializerRegistry.INSTANCE.registerSerializer(Option.class, AdvancedEnumSerializer.forEnum(Option.class));
- Since:
- 2.0.0
- Author:
- MrIvanPlays
-
Field Summary
Modifier and TypeFieldDescriptionstatic final BiPredicate<Integer,
List<String>> The default matches condition -
Method Summary
Modifier and TypeMethodDescriptiondeserialize
(DataObject data, Field field) AnnotationConfig invokes this call-back method during deserialization when it encounters a field of the specified type.static <E extends Enum<E>>
AdvancedEnumSerializer<E>Creates a newAdvancedEnumSerializer
for the specifiedenumClass
static <E extends Enum<E>>
AdvancedEnumSerializer<E>forEnumWithCondition
(Class<E> enumClass, BiPredicate<Integer, List<String>> matchesCondition) Creates a newAdvancedEnumSerializer
for the specifiedenumClass
with the specifiedmatchesCondition
.AnnotationConfig invokes this call-back method during serialization when it encounters a field of the specified type.
-
Field Details
-
DEFAULT_MATCHES_CONDITION
The default matches condition
-
-
Method Details
-
forEnum
Creates a newAdvancedEnumSerializer
for the specifiedenumClass
- Type Parameters:
E
- enum class- Parameters:
enumClass
- the enum class you want an advanced enum serializer- Returns:
- advanced enum serializer
- Throws:
NullPointerException
- if null enumClass specifiedIllegalArgumentException
- if specified enumClass is not anEnum
-
forEnumWithCondition
public static <E extends Enum<E>> AdvancedEnumSerializer<E> forEnumWithCondition(Class<E> enumClass, BiPredicate<Integer, List<String>> matchesCondition) Creates a newAdvancedEnumSerializer
for the specifiedenumClass
with the specifiedmatchesCondition
.The matches condition is used when the deserializer does a last effort to try and return an enum value from the specified input.
- Type Parameters:
E
- enum class- Parameters:
enumClass
- the enum class you want an advanced enum serializermatchesCondition
- matches condition- Returns:
- advanced enum serializer
- Throws:
NullPointerException
- if null enumClass or null matchesCondition specifiedIllegalArgumentException
- if the specified enumClass is not anEnum
-
deserialize
AnnotationConfig invokes this call-back method during deserialization when it encounters a field of the specified type.- Specified by:
deserialize
in interfaceFieldTypeSerializer<E extends Enum<E>>
- Parameters:
data
- the data we received from the configfield
- the field we will attach this information to later on- Returns:
- the generic value, the implementation of this interface has specified
-
serialize
AnnotationConfig invokes this call-back method during serialization when it encounters a field of the specified type.- Specified by:
serialize
in interfaceFieldTypeSerializer<E extends Enum<E>>
- Parameters:
value
- the data we need serializedfield
- the field we got the value from- Returns:
- a serialized object which is useful for dumping into a configuration file
-