Skip to content

Commit

Permalink
fix none-static inner class reference detection error, for issue #1661
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Aug 14, 2023
1 parent 91a0c59 commit b852188
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,9 @@ public void writeWithFilter(JSONWriter jsonWriter, Object object, Object fieldNa
}

JSONWriter.Context context = jsonWriter.context;
boolean ignoreNonFieldGetter = ((context.getFeatures() | features) & IgnoreNonFieldGetter.mask) != 0;
long features2 = context.getFeatures() | features;
boolean refDetect = (features2 & ReferenceDetection.mask) != 0;
boolean ignoreNonFieldGetter = (features2 & IgnoreNonFieldGetter.mask) != 0;

BeforeFilter beforeFilter = context.getBeforeFilter();
if (beforeFilter != null) {
Expand Down Expand Up @@ -488,6 +490,10 @@ public void writeWithFilter(JSONWriter jsonWriter, Object object, Object fieldNa
continue;
}

if (!refDetect && ("this$0".equals(fieldWriterFieldName) || "this$1".equals(fieldWriterFieldName) || "this$2".equals(fieldWriterFieldName))) {
continue;
}

BeanContext beanContext = null;

// name filter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONB;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.filter.BeanContext;
import com.alibaba.fastjson2.filter.ContextValueFilter;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -23,6 +25,22 @@ public void test() throws Exception {
"}", JSONB.toJSONString(jsonbBytes));
}

@Test
public void test1() throws Exception {
Info info = new Info();
info.getCard();
String str = JSON.toJSONString(info, new Myfilter(), JSONWriter.Feature.FieldBased);
assertEquals("{\"card\":{\"cardNo\":\"98765\"}}", str);
}

public class Myfilter
implements ContextValueFilter {
@Override
public Object process(BeanContext context, Object object, String name, Object value) {
return value;
}
}

public class Info {
private Card card;

Expand Down

0 comments on commit b852188

Please sign in to comment.