Skip to content

Commit

Permalink
adding support got BigDecimal (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
monitorjbl committed Jun 7, 2016
1 parent a1d3ae9 commit f0ae43c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
Expand Up @@ -17,6 +17,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
Expand Down Expand Up @@ -91,6 +92,8 @@ boolean writePrimitive(Object obj) throws IOException {
jgen.writeBoolean((Boolean) obj);
} else if(obj == null) {
jgen.writeNull();
} else if(obj instanceof BigDecimal) {
jgen.writeNumber((BigDecimal) obj);
} else {
return false;
}
Expand Down
Expand Up @@ -42,7 +42,7 @@ public void sutRandomSingleObjectPerformance() throws Exception {
}
}

System.out.println(times / (REPETITIONS - 100));
//System.out.println(times / (REPETITIONS - 100));
}

@Test
Expand All @@ -60,7 +60,7 @@ public void compareRandomSingleObjectPerformance() throws Exception {
}
}

System.out.println(times / (REPETITIONS - 100));
//System.out.println(times / (REPETITIONS - 100));
}

TestObject testObject() {
Expand Down
Expand Up @@ -20,6 +20,7 @@
import org.junit.Test;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URL;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -563,7 +564,6 @@ public void testWriteNullValues_disabledGlobally() throws Exception {
String serialized = sut.writeValueAsString(JsonView.with(ref));
Map<String, Object> obj = sut.readValue(serialized, HashMap.class);

System.out.println(serialized);
assertFalse(obj.containsKey("str2"));
}

Expand Down Expand Up @@ -686,6 +686,19 @@ public void testBackReferenceSupport() throws Exception {
String serialized = sut.writeValueAsString(JsonView.with(forward));
Map<String, Map<String, Object>> obj = sut.readValue(serialized, HashMap.class);

System.out.println(obj);
assertNotNull(obj.get("parent"));
assertEquals("back", obj.get("parent").get("id"));
}

@Test
public void testBigDecimalSerialization() throws Exception {
TestObject ref = new TestObject();
ref.setBigDecimal(new BigDecimal(Math.PI));

String serialized = sut.writeValueAsString(JsonView.with(ref));
Map<String, Object> obj = sut.readValue(serialized, HashMap.class);

assertNotNull(obj.get("bigDecimal"));
assertEquals(3.141592653589793, obj.get("bigDecimal"));
}
}
10 changes: 10 additions & 0 deletions json-view/src/test/java/com/monitorjbl/json/model/TestObject.java
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.math.BigDecimal;
import java.net.URI;
import java.net.URL;
import java.util.Date;
Expand Down Expand Up @@ -39,6 +40,7 @@ public enum TestEnum {VALUE_A, VALUE_B}
private URL url;
private URI uri;
private Class cls;
private BigDecimal bigDecimal;

public String getStr1() {
return str1;
Expand Down Expand Up @@ -207,4 +209,12 @@ public TestSubobject getSubWithIgnores() {
public void setSubWithIgnores(TestSubobject subWithIgnores) {
this.subWithIgnores = subWithIgnores;
}

public BigDecimal getBigDecimal() {
return bigDecimal;
}

public void setBigDecimal(BigDecimal bigDecimal) {
this.bigDecimal = bigDecimal;
}
}
Expand Up @@ -121,7 +121,6 @@ public void testNoninterference() throws Exception {
String ret = Request.Post("http://localhost:8080/bean").bodyString(
"{\"date\":\"1433214360187\",\"str1\":\"test\",\"notReal\":\"asdfas\"}", ContentType.APPLICATION_JSON)
.execute().returnContent().asString();
System.out.println(ret);
assertEquals(5, ret.split("\n").length);
}

Expand Down
Expand Up @@ -25,7 +25,7 @@ public void run() {
Server server = new Server(port);

final XmlWebApplicationContext xmlBasedContext = new XmlWebApplicationContext();
System.out.println(xmlBasedContext.getEnvironment().getClass());
//System.out.println(xmlBasedContext.getEnvironment().getClass());
xmlBasedContext.setConfigLocation("classpath:context.xml");

final ServletHolder servletHolder = new ServletHolder(new DispatcherServlet(xmlBasedContext));
Expand Down

0 comments on commit f0ae43c

Please sign in to comment.