Skip to content

Commit

Permalink
serialize skip thrift generate isSetXXX method
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Aug 25, 2022
1 parent 765ae9e commit d01da80
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
27 changes: 27 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 @@ -641,6 +641,33 @@ boolean record = isRecord(objectClass);

String methodName = method.getName();

// skip thrift isSetXXX
if (methodName.startsWith("isSet") && returnClass == boolean.class) {
boolean setterFound = false, unsetFound = false, getterFound = false;
String setterName = BeanUtils.getterName(methodName, null);
String getterName = "g" + setterName.substring(1);

String unsetName = "un" + setterName;
for (Method m : methods) {
if (m.getName().equals(setterName)
&& m.getParameterCount() == 1
&& m.getReturnType() == void.class) {
setterFound = true;
} else if (m.getName().equals(getterName)
&& m.getParameterCount() == 0) {
getterFound = true;
} else if (m.getName().equals(unsetName)
&& m.getParameterCount() == 0
&& m.getReturnType() == void.class) {
unsetFound = true;
}
}

if (setterFound && unsetFound && getterFound && !method.isAnnotationPresent(JSONField.class)) {
continue;
}
}

if (record) {
boolean match = false;
for (String recordFieldName : recordFieldNames) {
Expand Down
37 changes: 37 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues/Issue707.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.alibaba.fastjson2.issues;

import com.alibaba.fastjson2.JSON;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue707 {
@Test
public void test() {
Bean bean = new Bean();
String str = JSON.toJSONString(bean);
assertEquals("{\"id\":0}", str);
}

public static class Bean {
private int flags;
private int id;

public int getId() {
return id;
}

public void setId(int id) {
flags |= 1;
this.id = id;
}

public boolean isSetId() {
return (flags & 1) == 0;
}

public void unsetId() {
flags = 0;
}
}
}

0 comments on commit d01da80

Please sign in to comment.