EntryDeserializer.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package me.lortseam.completeconfig.serialization;
  2. import com.google.gson.JsonDeserializationContext;
  3. import com.google.gson.JsonDeserializer;
  4. import com.google.gson.JsonElement;
  5. import com.google.gson.JsonParseException;
  6. import com.google.gson.reflect.TypeToken;
  7. import me.lortseam.completeconfig.data.Entry;
  8. import org.apache.logging.log4j.LogManager;
  9. import org.apache.logging.log4j.Logger;
  10. import java.lang.reflect.Type;
  11. public class EntryDeserializer<T> implements JsonDeserializer<Entry<T>> {
  12. private static final Logger LOGGER = LogManager.getLogger();
  13. public static final Type TYPE = new TypeToken<Entry<?>>() {}.getType();
  14. private final Entry<T> configEntry;
  15. public EntryDeserializer(Entry<T> configEntry) {
  16. this.configEntry = configEntry;
  17. }
  18. @Override
  19. public Entry<T> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
  20. try {
  21. T value = context.deserialize(json, configEntry.getType());
  22. configEntry.setValue(value);
  23. } catch (JsonParseException e) {
  24. LOGGER.warn("[CompleteConfig] An error occurred while trying to load the config entry's value of field " + configEntry.getField() + ": " + e.getMessage());
  25. }
  26. return configEntry;
  27. }
  28. }