Skip to content

Commit

Permalink
adding support for classes (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
monitorjbl committed Jun 2, 2015
1 parent f29b6d5 commit 0f53b9d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
Expand Up @@ -87,6 +87,8 @@ boolean writeSpecial(Object obj) throws IOException {
jgen.writeString(obj.toString());
} else if (obj instanceof URI) {
jgen.writeString(obj.toString());
} else if (obj instanceof Class) {
jgen.writeString(((Class) obj).getCanonicalName());
} else {
return false;
}
Expand Down Expand Up @@ -225,7 +227,7 @@ 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())){
if (Modifier.isStatic(field.getModifiers())) {
return false;
}

Expand Down
Expand Up @@ -444,6 +444,16 @@ public void testURIs() throws Exception {
assertEquals(ref.getUri().toString(), obj.get("uri"));
}

@Test
public void testClass() throws Exception {
TestObject ref = new TestObject();
ref.setCls(TestSubobject.class);

String serialized = sut.writeValueAsString(JsonView.with(ref));
Map<String, Object> obj = sut.readValue(serialized, HashMap.class);
assertEquals(ref.getCls().getCanonicalName(), obj.get("cls"));
}

@Test
public void testEnums() throws Exception {
TestObject ref = new TestObject();
Expand Down
8 changes: 8 additions & 0 deletions json-view/src/test/java/com/monitorjbl/json/WriterTest.java
Expand Up @@ -194,6 +194,14 @@ public void testWriteSpecial_uri() throws Exception {
verify(jgen, times(1)).writeString(uri.toString());
}

@Test
public void testWriteSpecial_class() throws Exception {
Class cls = TestObject.class;
sut.writeSpecial(cls);
verify(jgen, times(1)).writeString(cls.getCanonicalName());
}


@Test
public void testWriteList_stringList() throws Exception {
assertTrue(sut.writeList(newArrayList("val1", "val2")));
Expand Down
Expand Up @@ -36,6 +36,7 @@ public static enum TestEnum {VALUE_A, VALUE_B}
private TestEnum testEnum;
private URL url;
private URI uri;
private Class cls;

public String getStr1() {
return str1;
Expand Down Expand Up @@ -188,4 +189,12 @@ public URI getUri() {
public void setUri(URI uri) {
this.uri = uri;
}

public Class getCls() {
return cls;
}

public void setCls(Class cls) {
this.cls = cls;
}
}

0 comments on commit 0f53b9d

Please sign in to comment.