Skip to content

Commit

Permalink
respecting interfaces on matchers (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
Taylor Jones committed Apr 19, 2016
1 parent c88feb7 commit 9ccf0e6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@
*.ipr
target
.DS_Store
.idea
Expand Up @@ -253,11 +253,21 @@ boolean fieldAllowed(Field field, Class declaringClass) {
return false;
}

//search for matcher
//search for matching class
Match match = null;
Class cls = declaringClass;
while(!cls.equals(Object.class) && match == null) {
match = result.getMatch(cls);

//search for any matching interfaces as well, stopping on the first one
if(match == null && cls.getInterfaces() != null) {
for(Class iface : cls.getInterfaces()) {
match = result.getMatch(iface);
if(match != null) {
break;
}
}
}
cls = cls.getSuperclass();
}
if(match == null) {
Expand Down
Expand Up @@ -8,6 +8,7 @@
import com.google.common.io.BaseEncoding;
import com.google.common.primitives.Ints;
import com.monitorjbl.json.model.TestChildObject;
import com.monitorjbl.json.model.TestInterface;
import com.monitorjbl.json.model.TestNonNulls;
import com.monitorjbl.json.model.TestNulls;
import com.monitorjbl.json.model.TestObject;
Expand Down Expand Up @@ -633,4 +634,20 @@ public void testIncludesForList() throws Exception {
assertNotNull(list.get(0).get("otherVal"));
assertNotNull(list.get(1).get("otherVal"));
}

// @Test
// public void testMatchingOnInterfaces() throws Exception {
// TestObject ref = new TestObject();
// ref.setStr1("asdf");
// ref.setDate(new Date());
//
// String serialized = sut.writeValueAsString(JsonView.with(ref)
// .onClass(TestInterface.class,
// match().exclude("date")));
// Map<String, Object> obj = sut.readValue(serialized, HashMap.class);
//
// assertNotNull(obj.get("str1"));
// assertEquals( ref.getStr1(),obj.get("str1"));
// assertNull(obj.get("date"));
// }
}
@@ -0,0 +1,7 @@
package com.monitorjbl.json.model;

import java.util.Date;

public interface TestInterface {
Date getDate();
}
Expand Up @@ -10,8 +10,8 @@
import java.util.Map;

@JsonIgnoreProperties({"ignoredIndirect"})
public class TestObject {
public static enum TestEnum {VALUE_A, VALUE_B}
public class TestObject implements TestInterface {
public enum TestEnum {VALUE_A, VALUE_B}

public static final String PUBLIC_FIELD = "public";
private static final String PRIVATE_FIELD = "private";
Expand Down

0 comments on commit 9ccf0e6

Please sign in to comment.