I'm using the Jackson JSON API to create JSON objects in Java and then print.
My code is similar to:
OracleResultSet rs = getMyResultSet();
OracleResultSetMetaData rsmd = (OracleResultSetMetaData)rs.getMetaData();
int columnCount = rsmd.getColumnCount();
String[] columnNames = new String[columnCount];
int[] columntypes = new int[columnCount];
for(int i = 1; i < columnCount; ++i) {
columnNames[i] = rsmd.getColumnName(i);
columnTypes[i] = rsmd.getColumnTypes(i);
}
ObjectMapper mapper = new ObjectMapper();
ObjectNode node;
String columnName;
while(rs.next()) {
node = mapper.createObjectNode();
for(int i = 1; i < columnCount; ++i) {
columnName = columnNames[i];
//I have a case defined for every OracleTypes.XXXX, but for brevity I'm only putting the one I'm having issues with here
switch (columnTypes[i]) {
case: OracleTypes.NUMBER:
node.put(columnName, rs.getBigDecimal(i)); //BigDecimal chosen based on: http://docs.oracle.com/cd/B19306_01/java.102/b14188/datamap.htm
break;
default:
node.put(columnName, rs.getString(i));
}
//UPDATE: the next line is the logic error that I had that was casuing the trouble
node.put(columnName, rs.getString(i));
}
System.out.println(mapper.writeValueAsString(node));
}
This all works great. The problem is when I go to print it.
Any values picked up from the OracleTypes.NUMBER
case are getting quotes, even when they should not be. A sample string that I am getting:
{"month":"1","year":"2013","modified":"2013-02-05 13:41:48.0","net":"294.68"}
What I want:
{"month":"1","year":"2013","modified":"2013-02-05 13:41:48.0","net":294.68}
Why is the BigDecimal (or any non-int from my testing) being printed with quotes around it? My understanding is that all real numeric values in JSON should be printed without quotes. I thought it might be a primitive/object issue and tried getting the double value from the BigDecimal but that did not help.
I have tested the program using the debugger and it DOES use the switch-case statement correctly, so that is not the issue.
I found the problem with my code. At the end, I had an extra node.put(columnName, rs.getString(i));
This was overwriting the field each time with a String of the value. Removing it fixed the problem. Sorry for the trouble all.