use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.
the class JsonUtil method toMap.
private static Object toMap(Object value, Class<?> parameterType) throws GfJsonException {
try {
if (value instanceof JSONObject) {
JSONObject obj = (JSONObject) value;
Iterator iterator = obj.keys();
Map map = new HashMap();
while (iterator.hasNext()) {
String key = (String) iterator.next();
Object elem;
elem = obj.get(key);
if (isPrimitiveOrWrapper(elem.getClass())) {
map.put(key, elem);
} else
throw new GfJsonException("Only primitive types are supported in map type for input commands");
}
return map;
} else
throw new GfJsonException("Expected JSONObject for Map. Retrieved type is " + value.getClass());
} catch (JSONException e) {
throw new GfJsonException(e);
}
}
use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.
the class DataCommandResult method buildTable.
private int buildTable(TabularResultData table, int startCount, int endCount) {
// Three steps:
// 1a. Convert each row object to a Json object.
// 1b. Build a list of keys that are used for each object
// 2. Pad MISSING_VALUE into Json objects for those data that are missing any particular key
// 3. Build the table from these Json objects.
// 1.
int lastRowExclusive = Math.min(selectResult.size(), endCount + 1);
List<SelectResultRow> paginatedRows = selectResult.subList(startCount, lastRowExclusive);
List<GfJsonObject> tableRows = new ArrayList<>();
List<GfJsonObject> rowsWithRealJsonObjects = new ArrayList<>();
Set<String> columns = new HashSet<>();
for (SelectResultRow row : paginatedRows) {
GfJsonObject object = new GfJsonObject();
try {
if (row.value == null || MISSING_VALUE.equals(row.value)) {
object.put("Value", MISSING_VALUE);
} else if (row.type == ROW_TYPE_PRIMITIVE) {
object.put(RESULT_FLAG, row.value);
} else {
object = buildGfJsonFromRawObject(row.value);
rowsWithRealJsonObjects.add(object);
object.keys().forEachRemaining(columns::add);
}
tableRows.add(object);
} catch (GfJsonException e) {
JSONObject errJson = new JSONObject().put("Value", "Error getting bean properties " + e.getMessage());
tableRows.add(new GfJsonObject(errJson, false));
}
}
// 2.
for (GfJsonObject tableRow : rowsWithRealJsonObjects) {
for (String key : columns) {
if (!tableRow.has(key)) {
try {
tableRow.put(key, MISSING_VALUE);
} catch (GfJsonException e) {
// TODO: Address this unlikely possibility.
logger.warn("Ignored GfJsonException:", e);
}
}
}
}
// 3.
for (GfJsonObject jsonObject : tableRows) {
addJSONObjectToTable(table, jsonObject);
}
return paginatedRows.size();
}
use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.
the class GemfireDataCommandsDUnitTest method validateJSONGetResult.
private void validateJSONGetResult(CommandResult cmdResult, String[] expectedCols) {
CompositeResultData rd = (CompositeResultData) cmdResult.getResultData();
SectionResultData section = rd.retrieveSectionByIndex(0);
TabularResultData table = section.retrieveTableByIndex(0);
GfJsonArray array = table.getHeaders();
assertEquals(expectedCols.length, array.size());
try {
for (String col : expectedCols) {
boolean found = false;
getLogWriter().info("Validating column " + col);
for (int i = 0; i < array.size(); i++) {
String header = (String) array.get(i);
if (col.equals(header))
found = true;
}
assertEquals(true, found);
}
} catch (GfJsonException e) {
fail("Error accessing table data", e);
}
}
use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.
the class GemfireDataCommandsDUnitTest method validateJSONGetResultValues.
private void validateJSONGetResultValues(CommandResult cmdResult, String[] expectedCols) {
CompositeResultData rd = (CompositeResultData) cmdResult.getResultData();
SectionResultData section = rd.retrieveSectionByIndex(0);
TabularResultData table = section.retrieveTableByIndex(0);
GfJsonArray array = table.getHeaders();
assertEquals(expectedCols.length, array.size());
try {
for (String col : expectedCols) {
boolean found = false;
getLogWriter().info("Validating column " + col);
for (int i = 0; i < array.size(); i++) {
String header = (String) array.get(i);
if (col.equals(header))
found = true;
}
assertEquals(true, found);
List<String> values = table.retrieveAllValues(col);
for (String value : values) {
assertNotSame("null", value);
}
}
} catch (GfJsonException e) {
fail("Error accessing table data", e);
}
}
Aggregations