diff --git a/src/main/scala/me/skymc/taboolib/common/serialize/DoNotSerialize.java b/src/main/scala/me/skymc/taboolib/common/serialize/DoNotSerialize.java new file mode 100644 index 0000000..cd72c13 --- /dev/null +++ b/src/main/scala/me/skymc/taboolib/common/serialize/DoNotSerialize.java @@ -0,0 +1,16 @@ +package me.skymc.taboolib.common.serialize; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @Author sky + * @Since 2018-10-05 12:11 + */ +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface DoNotSerialize { + +} \ No newline at end of file diff --git a/src/main/scala/me/skymc/taboolib/common/serialize/TSerializer.java b/src/main/scala/me/skymc/taboolib/common/serialize/TSerializer.java index d89ce4b..bbcf023 100644 --- a/src/main/scala/me/skymc/taboolib/common/serialize/TSerializer.java +++ b/src/main/scala/me/skymc/taboolib/common/serialize/TSerializer.java @@ -29,6 +29,9 @@ public class TSerializer { for (Map.Entry jsonElementEntry : serializeObject.entrySet()) { try { Field declaredField = serializable.getClass().getDeclaredField(jsonElementEntry.getKey()); + if (declaredField.isAnnotationPresent(DoNotSerialize.class)) { + continue; + } declaredField.setAccessible(true); Optional serializer = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(declaredField.getType())).findFirst(); if (serializer.isPresent()) { @@ -56,13 +59,13 @@ public class TSerializer { JsonObject serializeObject = new JsonObject(); for (Field declaredField : serializable.getClass().getDeclaredFields()) { try { - if (!Modifier.isStatic(declaredField.getModifiers())) { + if (!declaredField.isAnnotationPresent(DoNotSerialize.class) && !Modifier.isStatic(declaredField.getModifiers())) { declaredField.setAccessible(true); - Optional serializer = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(declaredField.getType())).findFirst(); Object o = declaredField.get(serializable); if (o == null) { continue; } + Optional serializer = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(declaredField.getType())).findFirst(); if (serializer.isPresent()) { serializeObject.addProperty(declaredField.getName(), serializer.get().getSerializer().write(o)); } else { diff --git a/src/main/scala/me/skymc/taboolib/common/serialize/TSerializerExample.java b/src/main/scala/me/skymc/taboolib/common/serialize/TSerializerExample.java index 1d5d8a5..f77d673 100644 --- a/src/main/scala/me/skymc/taboolib/common/serialize/TSerializerExample.java +++ b/src/main/scala/me/skymc/taboolib/common/serialize/TSerializerExample.java @@ -38,13 +38,20 @@ public class TSerializerExample { * 基本类型不需要手动进行序列化 * 包含: String、int、short、long、double、float、boolean、ItemStack、Location */ - private String text; - private int number; + private String text = "123"; + private int number = 100; + /** * 特殊类型需要进行手动序列化 * 本工具提供了基本容器的序列化方法 */ - private List list = Lists.newArrayList(); + private List list = Lists.newArrayList(1.0, 2.0, 3.0); + + /** + * 跳过序列化 + */ + @DoNotSerialize + private String ignoreSerialize = "aaa"; /** * 基本类型不会执行以下两个方法 @@ -79,6 +86,7 @@ public class TSerializerExample { "text='" + text + '\'' + ", number=" + number + ", list=" + list + + ", ignoreSerialize='" + ignoreSerialize + '\'' + '}'; } }