Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make boolean getter with 'is' prefix a valid getter for serializer #78

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions json-view/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.monitorjbl</groupId>
<artifactId>json-view</artifactId>
<version>1.1.0</version>
<version>1.1.1-beta</version>
<name>json-view</name>
<description>Provides programmatic exclusion/inclusion for Jackson JSON serialization</description>
<url>https://github.com/monitorjbl/json-view</url>
Expand Down Expand Up @@ -125,13 +125,11 @@
</profiles>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
<id>hrs.repo</id>
<url>https://artifactory.int.hrs.com/artifactory/releases-local</url>
<!-- <url>https://maven.int.hrs.com/artifactory/hrsRepo</url>-->
<!-- <url>http://detukr1-repo-update-981.data.det/repo</url>-->
</repository>
</distributionManagement>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
import static java.util.Arrays.asList;

public class JsonViewSerializer extends JsonSerializer<JsonView> {

private static final String GETTER_PREFIX = "get";
private static final String BOOLEAN_GETTER_PREFIX = "is";
public static boolean log = false;
/**
* Cached results from expensive (pure) methods
Expand Down Expand Up @@ -638,7 +641,7 @@ private List<AccessibleProperty> getAccessibleProperties(Class cls) {
.map(f -> new AccessibleProperty(f.getName(), f.getAnnotations(), f))
.forEach(p -> accessibleProperties.put(p.name, p));
getDeclaredMethods(cls).stream()
.filter(m -> m.getName().startsWith("get") && !m.getReturnType().equals(Void.class) && m.getParameters().length == 0)
.filter(this::isValidGetter)
.map(m -> new AccessibleProperty(getFieldNameFromGetter(m), m.getAnnotations(), m))
.forEach(p -> {
AccessibleProperty field = accessibleProperties.get(p.name);
Expand All @@ -662,6 +665,13 @@ private List<AccessibleProperty> getAccessibleProperties(Class cls) {
});
}

private boolean isValidGetter(Method m) {
boolean isGetter = m.getName().startsWith(GETTER_PREFIX) && !m.getReturnType().equals(Void.class) && m.getParameters().length == 0;
boolean isBooleanGetter = m.getName().startsWith(BOOLEAN_GETTER_PREFIX)
&& m.getReturnType().getSimpleName().equalsIgnoreCase(Boolean.class.getSimpleName()) && m.getParameters().length == 0;
return isGetter || isBooleanGetter;
}

private List<Field> getDeclaredFields(Class cls) {
List<Field> fields = new ArrayList<>();
Stack<Class> parents = new Stack<>();
Expand Down Expand Up @@ -791,10 +801,18 @@ private String getFieldName(AccessibleProperty property) {
}

private String getFieldNameFromGetter(Method method) {
if (method.getName().equals("get")) {
String methodName = method.getName();
if (methodName.equals(GETTER_PREFIX) || methodName.equals(BOOLEAN_GETTER_PREFIX)) {
return method.getName();
}
String name = method.getName().replaceFirst("get", "");
String name;
if (methodName.startsWith(GETTER_PREFIX)) {
name = methodName.replaceFirst(GETTER_PREFIX, "");
} else if (methodName.startsWith(BOOLEAN_GETTER_PREFIX)){
name = methodName.replaceFirst(BOOLEAN_GETTER_PREFIX, "");
} else {
throw new IllegalArgumentException(String.format("'%s' method is not a valid getter", methodName));
}
return name.substring(0, 1).toLowerCase() + name.substring(1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,8 @@ public void testDeepNestedObjects() throws Exception {
TestSubobject subobject = new TestSubobject();
subobject.setVal("someval");
subobject.setOtherVal("otherval");
subobject.setBooleanVal(true);
subobject.setBooleanBoxedVal(true);
TestObject ref = new TestObject();
ref.setStr1("somestr");
ref.setSub(subobject);
Expand All @@ -848,6 +850,9 @@ public void testDeepNestedObjects() throws Exception {
Map<String, Object> subMap = (Map<String, Object>) obj.get("sub");
assertNotNull(subMap.get("val"));
assertEquals(subobject.getVal(), subMap.get("val"));
assertNotNull(subMap.get("booleanVal"));
assertEquals(subobject.isBooleanVal(), subMap.get("booleanVal"));
assertEquals(subobject.isBooleanBoxedVal(), subMap.get("booleanBoxedVal"));
assertNull(subMap.get("otherVal"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public class TestSubobject {
private String val;
private String otherVal;
private TestSubobject sub;
private boolean booleanVal;
private Boolean booleanBoxedVal;

public TestSubobject(String val) {
this.val = val;
Expand All @@ -17,6 +19,22 @@ public TestSubobject(String val, TestSubobject sub) {
public TestSubobject() {
}

public boolean isBooleanVal() {
return booleanVal;
}

public void setBooleanVal(boolean booleanVal) {
this.booleanVal = booleanVal;
}

public Boolean isBooleanBoxedVal() {
return booleanBoxedVal;
}

public void setBooleanBoxedVal(Boolean booleanBoxedVal) {
this.booleanBoxedVal = booleanBoxedVal;
}

public String getVal() {
return val;
}
Expand Down
2 changes: 1 addition & 1 deletion spring-json-view/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.monitorjbl</groupId>
<artifactId>spring-json-view</artifactId>
<version>1.1.0</version>
<version>1.1.1-beta</version>
<name>spring-json-view</name>
<description>Provides programmatic JSON response manipulation for Spring MVC</description>
<url>https://github.com/monitorjbl/spring-json-view</url>
Expand Down