Skip to content

Commit

Permalink
@AutoSerialize update
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubix327 committed Oct 26, 2022
1 parent 4e7c07e commit dd5c031
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 39 deletions.
40 changes: 18 additions & 22 deletions src/main/java/org/mineacademy/fo/SerializeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -131,23 +130,23 @@ 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();

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();

Expand Down Expand Up @@ -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);

Expand All @@ -214,7 +213,7 @@ else if (object instanceof Iterable || object.getClass().isArray() || object ins
else {
final List<Object> 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));

Expand All @@ -226,8 +225,8 @@ else if (object instanceof Iterable || object.getClass().isArray() || object ins

}

} else if (object instanceof Map || object instanceof StrictMap) {
final Map<Object, Object> oldMap = object instanceof StrictMap ? ((StrictMap<Object, Object>) object).getSource() : (Map<Object, Object>) object;
} else if (object instanceof Map) {
final Map<Object, Object> oldMap = (Map<Object, Object>) object;

if (isJson) {
final JSONObject json = new JSONObject();
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -349,8 +347,7 @@ public static List<Field> 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;
Expand Down Expand Up @@ -493,19 +490,19 @@ public static <T> T deserialize(@NonNull Mode mode, @NonNull final Class<T> 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)
Expand Down Expand Up @@ -777,8 +774,7 @@ public static List<Field> 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;
Expand Down
20 changes: 3 additions & 17 deletions src/main/java/org/mineacademy/fo/annotation/AutoSerialize.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@
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}.<br><br>
* <b>On class</b>:
* <ul>
* <li>Serialized and deserialized all non-static class fields</li>
* <li>Serializes and deserializes all non-static class fields</li>
* <li>But skips fields that have disabled this feature by <i>@AutoSerialize(false)</i></li>
* </ul>
* When using on class, if you want to prevent one specific field from auto-serializing and auto-deserializing,
* use <i>@AutoSerialize(false)</i> on that field.<br>
* <br>
* <b>On field</b>:
* <ul>
* <li>Serialized and deserializes field if annotation is in enabled state</li>
* <li>Skips field serializing if <i>@AutoSerialize(autoSerialize = false)</i></li>
* <li>Skips field deserializing if <i>@AutoSerialize(autoDeserialize = false)</i></li>
* <li>Serializes and deserializes field if annotation is in enabled state</li>
* </ul>
*/
@Target({ElementType.TYPE, ElementType.FIELD})
Expand All @@ -34,18 +32,6 @@
*/
boolean value() default true;

/**
* When false, automatic serializing does not work for class or field above which is set.<br>
* You may manually serialize the disabled fields in <i>serialize</i> method if you want.
*/
boolean autoSerialize() default true;

/**
* When false, automatic deserializing does not work for class or field above which is set.<br>
* You may manually set the disabled fields in <i>deserialize</i> method if you want.
*/
boolean autoDeserialize() default true;

/**
* In what format should we convert your fields to SerializedMap.<br>
* Only usable if set on class. You can only set one format for one class.<br>
Expand Down

0 comments on commit dd5c031

Please sign in to comment.