Skip to content

Commit

Permalink
ignoring static fields (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
monitorjbl committed Jun 2, 2015
1 parent 6e2d2af commit b4c6e6d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
Expand Up @@ -8,6 +8,7 @@

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
Expand Down Expand Up @@ -224,6 +225,9 @@ void writeObject(Object obj) throws IOException {
boolean fieldAllowed(Field field, Class declaringClass) {
String name = field.getName();
String prefix = currentPath.length() > 0 ? currentPath + "." : "";
if(Modifier.isStatic(field.getModifiers())){
return false;
}

//search for matcher
Match match = null;
Expand Down
Expand Up @@ -454,4 +454,15 @@ public void testEnums() throws Exception {
assertNotNull(obj.get("testEnum"));
assertEquals(ref.getTestEnum().toString(), obj.get("testEnum"));
}

@Test
public void testStaticFieldsAreIgnored() throws Exception {
TestObject ref = new TestObject();
ref.setStr1("val1");

String serialized = sut.writeValueAsString(JsonView.with(ref));
Map<String, Object> obj = sut.readValue(serialized, HashMap.class);
assertNull(obj.get("PUBLIC_FIELD"));
assertNull(obj.get("PRIVATE_FIELD"));
}
}
Expand Up @@ -11,7 +11,10 @@

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

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

private String str1;
private String str2;
Expand Down

0 comments on commit b4c6e6d

Please sign in to comment.