Skip to content

Commit

Permalink
bug fix for process jackson annotation JsonIgnore, for issue #716
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Aug 31, 2022
1 parent c62016f commit 3aa25db
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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":
Expand Down
22 changes: 22 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1758,4 +1759,25 @@ public static <T> T checkNotNull(T obj) {
}
return obj;
}

public static void processJacksonJsonJsonIgnore(FieldInfo fieldInfo, Annotation annotation) {
fieldInfo.ignore = true;
Class<? extends Annotation> 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
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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":
Expand Down
22 changes: 22 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues/Issue716.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 3aa25db

Please sign in to comment.