From dd5c031e94744f2b71c5b3c594108a8977784a83 Mon Sep 17 00:00:00 2001 From: Rubix327 Date: Wed, 26 Oct 2022 03:06:37 +0300 Subject: [PATCH] @AutoSerialize update --- .../org/mineacademy/fo/SerializeUtil.java | 40 +++++++++---------- .../fo/annotation/AutoSerialize.java | 20 ++-------- 2 files changed, 21 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/mineacademy/fo/SerializeUtil.java b/src/main/java/org/mineacademy/fo/SerializeUtil.java index 88ab75143..537c325fa 100644 --- a/src/main/java/org/mineacademy/fo/SerializeUtil.java +++ b/src/main/java/org/mineacademy/fo/SerializeUtil.java @@ -23,7 +23,6 @@ import org.mineacademy.fo.annotation.AutoSerialize; import org.mineacademy.fo.collection.SerializedMap; import org.mineacademy.fo.collection.StrictCollection; -import org.mineacademy.fo.collection.StrictMap; import org.mineacademy.fo.exception.FoException; import org.mineacademy.fo.exception.InvalidWorldException; import org.mineacademy.fo.jsonsimple.*; @@ -131,7 +130,7 @@ else if (object instanceof Location) else if (object instanceof BoxedMessage) { final String message = ((BoxedMessage) object).getMessage(); - return message == null || "".equals(message) || "null".equals(message) ? null : message; + return "".equals(message) || "null".equals(message) ? null : message; } else if (object instanceof UUID) return object.toString(); @@ -139,15 +138,15 @@ else if (object instanceof BoxedMessage) { else if (object instanceof Enum) return object.toString(); + else if (object instanceof Entity) + return Remain.getName((Entity) object); + else if (object instanceof CommandSender) return ((CommandSender) object).getName(); else if (object instanceof World) return ((World) object).getName(); - else if (object instanceof Entity) - return Remain.getName((Entity) object); - else if (object instanceof PotionEffectType) return ((PotionEffectType) object).getName(); @@ -195,12 +194,12 @@ else if (object instanceof ClickEvent) { else if (object instanceof Path) throw new FoException("Cannot serialize Path " + object + ", did you mean to convert it into a name?"); - else if (object instanceof Iterable || object.getClass().isArray() || object instanceof IsInList) { + else if (object instanceof Iterable || object.getClass().isArray()) { if (isJson) { final JSONArray jsonList = new JSONArray(); - if (object instanceof Iterable || object instanceof IsInList) + if (object instanceof Iterable) for (final Object element : object instanceof IsInList ? ((IsInList) object).getList() : (Iterable) object) addJsonElement(element, jsonList); @@ -214,7 +213,7 @@ else if (object instanceof Iterable || object.getClass().isArray() || object ins else { final List serialized = new ArrayList<>(); - if (object instanceof Iterable || object instanceof IsInList) + if (object instanceof Iterable) for (final Object element : object instanceof IsInList ? ((IsInList) object).getList() : (Iterable) object) serialized.add(serialize(mode, element)); @@ -226,8 +225,8 @@ else if (object instanceof Iterable || object.getClass().isArray() || object ins } - } else if (object instanceof Map || object instanceof StrictMap) { - final Map oldMap = object instanceof StrictMap ? ((StrictMap) object).getSource() : (Map) object; + } else if (object instanceof Map) { + final Map oldMap = (Map) object; if (isJson) { final JSONObject json = new JSONObject(); @@ -241,8 +240,7 @@ else if (object instanceof Iterable || object.getClass().isArray() || object ins "JSON requires Map to be translated into keys that are String or Numbers, found " + key.getClass().getSimpleName() + " key: " + key + " with value '" + value + "'"); if (value != null) - Valid.checkBoolean(value instanceof String || value instanceof Boolean || value instanceof Character || value instanceof Number || value instanceof List - || value instanceof JSONObject || value instanceof JSONArray, + Valid.checkBoolean(value instanceof String || value instanceof Boolean || value instanceof Character || value instanceof Number || value instanceof List || value instanceof JSONObject, "JSON requires Map to be translated into values that are String or List only, found " + value.getClass().getSimpleName() + ": " + value + " for key " + key); if (value instanceof List) { @@ -259,7 +257,7 @@ else if (object instanceof Iterable || object.getClass().isArray() || object ins json.put(key == null ? null : key.toString(), array); } else - json.put(key == null ? null : key.toString(), value == null ? null : value); + json.put(key == null ? null : key.toString(), value); } return json; @@ -349,8 +347,7 @@ public static List getFieldsToAutoSerialize(Class classOf){ boolean hasAnnotation = field.isAnnotationPresent(AutoSerialize.class); boolean isEnabled = false; if (hasAnnotation){ - AutoSerialize ann = field.getAnnotation(AutoSerialize.class); - isEnabled = ann.value() && ann.autoSerialize(); + isEnabled = field.getAnnotation(AutoSerialize.class).value(); } if (Modifier.isStatic(field.getModifiers())) continue; @@ -493,19 +490,19 @@ public static T deserialize(@NonNull Mode mode, @NonNull final Class clas if (classOf == String.class) object = object.toString(); - else if (classOf == Integer.class) + else if (classOf == Integer.class || classOf == Integer.TYPE) object = Integer.parseInt(object.toString()); - else if (classOf == Long.class) + else if (classOf == Long.class || classOf == Long.TYPE) object = Long.decode(object.toString()); - else if (classOf == Double.class) + else if (classOf == Double.class || classOf == Double.TYPE) object = Double.parseDouble(object.toString()); - else if (classOf == Float.class) + else if (classOf == Float.class || classOf == Float.TYPE) object = Float.parseFloat(object.toString()); - else if (classOf == Boolean.class) + else if (classOf == Boolean.class || classOf == Boolean.TYPE) object = Boolean.parseBoolean(object.toString()); else if (classOf == SerializedMap.class) @@ -777,8 +774,7 @@ public static List getFieldsToAutoDeserialize(Class classOf){ boolean hasAnnotation = field.isAnnotationPresent(AutoSerialize.class); boolean isEnabled = false; if (hasAnnotation){ - AutoSerialize ann = field.getAnnotation(AutoSerialize.class); - isEnabled = ann.value() && ann.autoDeserialize(); + isEnabled = field.getAnnotation(AutoSerialize.class).value(); } if (Modifier.isStatic(field.getModifiers())) continue; diff --git a/src/main/java/org/mineacademy/fo/annotation/AutoSerialize.java b/src/main/java/org/mineacademy/fo/annotation/AutoSerialize.java index 19c05db76..31c358b3b 100644 --- a/src/main/java/org/mineacademy/fo/annotation/AutoSerialize.java +++ b/src/main/java/org/mineacademy/fo/annotation/AutoSerialize.java @@ -8,11 +8,11 @@ import java.lang.annotation.Target; /** - * This annotation automatically serialized and deserializes fields for your + * This annotation automatically serializes and deserializes fields for your * custom class implementing {@link org.mineacademy.fo.model.ConfigSerializable}.

* On class: *
    - *
  • Serialized and deserialized all non-static class fields
  • + *
  • Serializes and deserializes all non-static class fields
  • *
  • But skips fields that have disabled this feature by @AutoSerialize(false)
  • *
* When using on class, if you want to prevent one specific field from auto-serializing and auto-deserializing, @@ -20,9 +20,7 @@ *
* On field: *
    - *
  • Serialized and deserializes field if annotation is in enabled state
  • - *
  • Skips field serializing if @AutoSerialize(autoSerialize = false)
  • - *
  • Skips field deserializing if @AutoSerialize(autoDeserialize = false)
  • + *
  • Serializes and deserializes field if annotation is in enabled state
  • *
*/ @Target({ElementType.TYPE, ElementType.FIELD}) @@ -34,18 +32,6 @@ */ boolean value() default true; - /** - * When false, automatic serializing does not work for class or field above which is set.
- * You may manually serialize the disabled fields in serialize method if you want. - */ - boolean autoSerialize() default true; - - /** - * When false, automatic deserializing does not work for class or field above which is set.
- * You may manually set the disabled fields in deserialize method if you want. - */ - boolean autoDeserialize() default true; - /** * In what format should we convert your fields to SerializedMap.
* Only usable if set on class. You can only set one format for one class.