All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.bloodshura.ignitium.cfg.ValueSerializer Maven / Gradle / Ivy

package com.github.bloodshura.ignitium.cfg;

import com.github.bloodshura.ignitium.color.Color;
import com.github.bloodshura.ignitium.enumeration.Enumerations;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public interface ValueSerializer {
	ValueSerializer COLOR = new ValueSerializer() {
		@Nullable
		@Override
		public Color deserialize(@Nonnull Class requestedType, @Nonnull Object value) {
			return Color.fromHex(value.toString());
		}

		@Nonnull
		@Override
		public Class getAcceptableType() {
			return Color.class;
		}

		@Nonnull
		@Override
		public Object serialize(@Nonnull Color value) {
			return "#" + value.toHash();
		}
	};
	ValueSerializer ENUM = new ValueSerializer() {
		@Nullable
		@Override
		public Enum deserialize(@Nonnull Class requestedType, @Nonnull Object value) {
			if (value instanceof Number) {
				Enum[] values = requestedType.getEnumConstants();
				int index = ((Number) value).intValue();

				if (index >= 0 && index < values.length) {
					return values[index];
				}
			} else if (value instanceof String) {
				Enum result = Enumerations.search(requestedType, value.toString());

				if (result != null) {
					return result;
				}
			}

			return null;
		}

		@Nonnull
		@Override
		public Class getAcceptableType() {
			return Enum.class;
		}

		@Nonnull
		@Override
		public Object serialize(@Nonnull Enum value) {
			return value.name().toLowerCase();
		}
	};

	@Nullable
	E deserialize(@Nonnull Class requestedType, @Nonnull Object value);

	@Nonnull
	Class getAcceptableType();

	@Nonnull
	Object serialize(@Nonnull E value);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy