diff --git a/.gitignore b/.gitignore index 2b59d34625259a2e960e2271076c99f7407542b7..a1d08b30f4ab348b2281525b5d3da0753f2712dc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,10 @@ .idea/ *.iml target/ +.settings/ +# eclipse +.project +.classpath # Compiled class file *.class 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 0000000000000000000000000000000000000000..89c8763adc985b9916e6d5d32ea6efa4d40fabe9 --- /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; + } + } +} 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 29479f4777ceaafdf335dc1289d795a577cdb4e5..de505106abbcf5e9ab0514746a4b1e825e7dfe24 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; + } + } + }