diff --git a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderBaseModule.java b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderBaseModule.java index 06177821a6..8a86b8a039 100644 --- a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderBaseModule.java +++ b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderBaseModule.java @@ -28,6 +28,8 @@ import java.util.concurrent.atomic.*; import java.util.function.Function; +import static com.alibaba.fastjson2.util.BeanUtils.processJacksonJsonJsonIgnore; + public class ObjectReaderBaseModule implements ObjectReaderModule { final ObjectReaderProvider provider; @@ -668,7 +670,7 @@ public void getFieldInfo(FieldInfo fieldInfo, Class objectClass, Method method) switch (annotationTypeName) { case "com.fasterxml.jackson.annotation.JsonIgnore": if (useJacksonAnnotation) { - fieldInfo.ignore = true; + processJacksonJsonJsonIgnore(fieldInfo, annotation); } break; case "com.fasterxml.jackson.annotation.JsonAnySetter": @@ -729,7 +731,7 @@ private void processAnnotation(FieldInfo fieldInfo, Annotation[] annotations) { switch (annotationTypeName) { case "com.fasterxml.jackson.annotation.JsonIgnore": if (useJacksonAnnotation) { - fieldInfo.ignore = true; + processJacksonJsonJsonIgnore(fieldInfo, annotation); } break; case "com.fasterxml.jackson.annotation.JsonAnyGetter": diff --git a/core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java b/core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java index 2dce5109d7..a659a505d4 100644 --- a/core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java +++ b/core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java @@ -5,6 +5,7 @@ import com.alibaba.fastjson2.TypeReference; import com.alibaba.fastjson2.annotation.JSONField; import com.alibaba.fastjson2.codec.BeanInfo; +import com.alibaba.fastjson2.codec.FieldInfo; import com.alibaba.fastjson2.modules.ObjectCodecProvider; import java.io.Serializable; @@ -1758,4 +1759,25 @@ public static T checkNotNull(T obj) { } return obj; } + + public static void processJacksonJsonJsonIgnore(FieldInfo fieldInfo, Annotation annotation) { + fieldInfo.ignore = true; + Class annotationClass = annotation.getClass(); + BeanUtils.annotationMethods(annotationClass, m -> { + String name = m.getName(); + try { + Object result = m.invoke(annotation); + switch (name) { + case "value": + boolean value = (Boolean) result; + fieldInfo.ignore = value; + break; + default: + break; + } + } catch (Throwable ignored) { + // ignored + } + }); + } } diff --git a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterBaseModule.java b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterBaseModule.java index 0ee5e4a792..05ccfafa37 100644 --- a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterBaseModule.java +++ b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterBaseModule.java @@ -25,6 +25,8 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.*; +import static com.alibaba.fastjson2.util.BeanUtils.processJacksonJsonJsonIgnore; + public class ObjectWriterBaseModule implements ObjectWriterModule { static ObjectWriterAdapter STACK_TRACE_ELEMENT_WRITER; @@ -243,7 +245,7 @@ public void getFieldInfo(BeanInfo beanInfo, FieldInfo fieldInfo, Class objectTyp switch (annotationTypeName) { case "com.fasterxml.jackson.annotation.JsonIgnore": if (useJacksonAnnotation) { - fieldInfo.ignore = true; + processJacksonJsonJsonIgnore(fieldInfo, annotation); } break; case "com.fasterxml.jackson.annotation.JsonAnyGetter": @@ -627,7 +629,7 @@ private void processAnnotations(FieldInfo fieldInfo, Annotation[] annotations) { switch (annotationTypeName) { case "com.fasterxml.jackson.annotation.JsonIgnore": if (useJacksonAnnotation) { - fieldInfo.ignore = true; + processJacksonJsonJsonIgnore(fieldInfo, annotation); } break; case "com.fasterxml.jackson.annotation.JsonAnyGetter": diff --git a/core/src/test/java/com/alibaba/fastjson2/issues/Issue716.java b/core/src/test/java/com/alibaba/fastjson2/issues/Issue716.java new file mode 100644 index 0000000000..98e636e8f8 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues/Issue716.java @@ -0,0 +1,22 @@ +package com.alibaba.fastjson2.issues; + +import com.alibaba.fastjson2.JSON; +import com.fasterxml.jackson.annotation.JsonIgnore; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class Issue716 { + @Test + public void test() { + Bean bean = new Bean(); + bean.id = 123; + String str = JSON.toJSONString(bean); + assertEquals(str, "{\"id\":123}"); + } + + public static class Bean { + @JsonIgnore(value = false) + public int id; + } +}