public void addListElement(Object listValue, HTable headers,
Table dataTable) throws JSONException {
if (listValue instanceof JSONObject) {
if (maxNumLines <= 0 || numObjects < maxNumLines) {
Row row = dataTable.addRow(factory);
numObjects++;
JSONObject o = (JSONObject) listValue;
Iterator<String> it = getSortedKeysIterator(o);
while (it.hasNext()) {
String key = it.next();
addObjectElement(key, o.get(key), headers, row);
}
}
} else if (isPrimitiveValue(listValue)) {
HNode hNode = addHNode(headers, HTable.VALUES_COLUMN, DataStructure.PRIMITIVE, factory, worksheet);
String hNodeId = hNode.getId();
Row row = dataTable.addRow(factory);
numObjects++;
// TODO, conserve the types of the primitive types.
String value = "";
if (listValue instanceof String || listValue instanceof Boolean) {
value = (String) listValue;
} else if (listValue instanceof Double) {
value = Double.toString((Double) listValue);
} else if (listValue instanceof Integer) {
value = Integer.toString((Integer) listValue);
} else if (listValue instanceof Long) {
value = Long.toString((Long) listValue);
} else {
// Pedro 2012/09/14
logger.error("Unexpected value in JSON array:"
+ listValue.toString());
}
row.setValue(hNodeId, value, factory);
} else if (listValue instanceof JSONArray) {
if (maxNumLines <= 0 || numObjects < maxNumLines) {
HNode hNode = addHNode(headers, "nested array", DataStructure.COLLECTION, factory, worksheet);
String hNodeId = hNode.getId();
Row row = dataTable.addRow(factory);
numObjects++;
if (maxNumLines > 0 && numObjects >= maxNumLines)
return;
HTable nestedHTable = addNestedHTable(hNode,
"nested array values", row);
Table nestedTable = row.getNode(hNodeId).getNestedTable();
JSONArray a = (JSONArray) listValue;
for (int i = 0; i < a.length(); i++) {
addListElement(a.get(i), nestedHTable, nestedTable);
}
}