private void addKeysAndValues(JSONObject object, HTable nestedHTable, Table nestedTable) throws JSONException { Row nestedRow = nestedTable.addRow(getFactory()); @SuppressWarnings("unchecked") Iterator<String> it = object.sortedKeys(); while (it.hasNext()) { String nestedKey = it.next(); addObjectElement(nestedKey, object.get(nestedKey), nestedHTable, nestedRow); } }
private void addListElement(Object listValue, HTable headers, Table dataTable) throws JSONException { if (listValue instanceof JSONObject) { Row row = dataTable.addRow(getFactory()); JSONObject o = (JSONObject) listValue; @SuppressWarnings("unchecked") Iterator<String> it = o.sortedKeys(); while (it.hasNext()) { String key = it.next(); addObjectElement(key, o.get(key), headers, row); } } else if (isPrimitiveValue(listValue)) { HNode hNode = addHNode(headers, "values"); String hNodeId = hNode.getId(); Row row = dataTable.addRow(getFactory()); // 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, getFactory()); } else if (listValue instanceof JSONArray) { HNode hNode = addHNode(headers, "nested array"); String hNodeId = hNode.getId(); Row row = dataTable.addRow(getFactory()); 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); } } else { logger.error("Cannot handle whatever case is not covered by the if statements. Sorry."); } }