From 2be82383b726562a3a82335a9880871cecaa2975 Mon Sep 17 00:00:00 2001 From: zhaohuihua Date: Sun, 14 Jun 2020 00:14:17 +0800 Subject: [PATCH 1/3] =?UTF-8?q?@JSONField(serialize=3Dboolean)=E5=92=8C@JS?= =?UTF-8?q?ONField(deserialize=3Dboolean)=E5=A4=84=E7=90=86=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fastjson/easyjson/FastEasyJsons.java | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/fastjson-to-easyjson/src/main/java/com/alibaba/fastjson/easyjson/FastEasyJsons.java b/fastjson-to-easyjson/src/main/java/com/alibaba/fastjson/easyjson/FastEasyJsons.java index 29479f47..de505106 100644 --- a/fastjson-to-easyjson/src/main/java/com/alibaba/fastjson/easyjson/FastEasyJsons.java +++ b/fastjson-to-easyjson/src/main/java/com/alibaba/fastjson/easyjson/FastEasyJsons.java @@ -14,11 +14,13 @@ package com.alibaba.fastjson.easyjson; +import java.lang.reflect.Modifier; +import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.serializer.SerializerFeature; import com.jn.easyjson.core.JSONBuilder; import com.jn.easyjson.core.JSONBuilderProvider; - -import java.lang.reflect.Modifier; +import com.jn.easyjson.core.exclusion.Exclusion; +import com.jn.langx.util.reflect.FieldAttributes; public class FastEasyJsons { @@ -41,7 +43,36 @@ public class FastEasyJsons { } boolean prettyFormat = (SerializerFeature.PrettyFormat.getMask() & features) != 0; jsonBuilder.prettyFormat(prettyFormat); + + // @JSONField(serialize=boolean)和@JSONField(deserialize=boolean)的处理类 + FastJsonIgnoreAnnotationExclusion ignoreExclusion = new FastJsonIgnoreAnnotationExclusion(); + jsonBuilder.addDeserializationExclusion(ignoreExclusion); + jsonBuilder.addSerializationExclusion(ignoreExclusion); + return jsonBuilder; } + /** + * 注解@JSONField(serialize=boolean)和@JSONField(deserialize=boolean)的处理类 + * + * @author zhaohuihua + * @version 20200614 + */ + private static class FastJsonIgnoreAnnotationExclusion implements Exclusion { + + @Override + public boolean shouldSkipField(FieldAttributes f, boolean serialize) { + JSONField jsonField = f.getAnnotation(JSONField.class); + if (jsonField == null) { + return false; + } + return (serialize && jsonField.serialize()) || (!serialize && jsonField.deserialize()); + } + + @Override + public boolean shouldSkipClass(Class clazz, boolean serialize) { + return false; + } + } + } -- Gitee From 4ee720e02ae74070137c867d660262de7558cdd5 Mon Sep 17 00:00:00 2001 From: zhaohuihua Date: Sun, 14 Jun 2020 00:19:05 +0800 Subject: [PATCH 2/3] =?UTF-8?q?@JSONField(serialize=3Dboolean)=E5=92=8C@JS?= =?UTF-8?q?ONField(deserialize=3Dboolean)=E6=B5=8B=E8=AF=95=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fastjson/FastJsonIgnoreFieldTest.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 easyjson-examples/src/test/java/com/jn/easyjson/tests/examples/fastjson/FastJsonIgnoreFieldTest.java diff --git a/easyjson-examples/src/test/java/com/jn/easyjson/tests/examples/fastjson/FastJsonIgnoreFieldTest.java b/easyjson-examples/src/test/java/com/jn/easyjson/tests/examples/fastjson/FastJsonIgnoreFieldTest.java new file mode 100644 index 00000000..89c8763a --- /dev/null +++ b/easyjson-examples/src/test/java/com/jn/easyjson/tests/examples/fastjson/FastJsonIgnoreFieldTest.java @@ -0,0 +1,60 @@ +package com.jn.easyjson.tests.examples.fastjson; + +import org.junit.Assert; +import org.junit.Test; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.annotation.JSONField; + +public class FastJsonIgnoreFieldTest { + + public static void main(String[] args) { + testSerialize(); + testDeserialize(); + } + + @Test + public static void testSerialize() { + UserEntity user = new UserEntity(); + user.setId("1001"); + user.setName("Test1"); + String jsonString = JSON.toJSONString(user); + System.out.println(jsonString); + // 期望输出: {"name":"Test1"} + Assert.assertEquals("{\"name\":\"Test1\"}", jsonString); + } + + @Test + public static void testDeserialize() { + String jsonString = "{\"id\":\"1001\",\"name\":\"Test1\"}"; + UserEntity user = JSON.parseObject(jsonString, UserEntity.class); + System.out.println("id=" + user.getId()); + System.out.println("name=" + user.getName()); + // 期望输出: user.id=null + Assert.assertNull(user.getId()); + Assert.assertEquals("Test1", user.getName()); + } + + static class UserEntity { + + private String id; + private String name; + + @JSONField(serialize = false) + public String getId() { + return id; + } + + @JSONField(deserialize = false) + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } +} -- Gitee From e0a9991c39fe3853cb487fe2061fff71e929ab73 Mon Sep 17 00:00:00 2001 From: qdbp Date: Sun, 14 Jun 2020 07:43:48 +0800 Subject: [PATCH 3/3] gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 2b59d346..a1d08b30 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,10 @@ .idea/ *.iml target/ +.settings/ +# eclipse +.project +.classpath # Compiled class file *.class -- Gitee