use of org.apache.geode.management.internal.cli.json.GfJsonObject in project geode by apache.
the class CLIMultiStepHelper method getStepArgs.
public static GfJsonObject getStepArgs() {
Map<String, String> args = null;
if (CliUtil.isGfshVM) {
args = Gfsh.getCurrentInstance().getEnv();
} else {
args = CommandExecutionContext.getShellEnv();
}
if (args == null)
return null;
String stepArg = args.get(CLIMultiStepHelper.STEP_ARGS);
if (stepArg == null)
return null;
GfJsonObject object;
try {
object = new GfJsonObject(stepArg);
} catch (GfJsonException e) {
throw new RuntimeException("Error converting arguments section into json object");
}
return object;
}
use of org.apache.geode.management.internal.cli.json.GfJsonObject in project geode by apache.
the class JsonUtil method jsonToObject.
/**
* Converts given JSON String in to a Object. Refer http://www.json.org/ to construct a JSON
* format.
*
* @param jsonString jsonString to be converted in to a Map.
* @return an object constructed from given JSON String
*
* @throws IllegalArgumentException if the specified JSON string can not be converted in to an
* Object
*/
public static <T> T jsonToObject(String jsonString, Class<T> klass) {
T objectFromJson = null;
try {
GfJsonObject jsonObject = new GfJsonObject(jsonString);
objectFromJson = klass.newInstance();
Method[] declaredMethods = klass.getDeclaredMethods();
Map<String, Method> methodsMap = new HashMap<String, Method>();
for (Method method : declaredMethods) {
methodsMap.put(method.getName(), method);
}
int noOfFields = jsonObject.size();
Iterator<String> keys = jsonObject.keys();
while (keys.hasNext()) {
String key = keys.next();
Method method = methodsMap.get("set" + capitalize(key));
if (method != null) {
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length == 1) {
Class<?> parameterType = parameterTypes[0];
Object value = jsonObject.get(key);
if (isPrimitiveOrWrapper(parameterType)) {
value = getPrimitiveOrWrapperValue(parameterType, value);
} else // Bug #51175
if (isArray(parameterType)) {
value = toArray(value, parameterType);
} else if (isList(parameterType)) {
value = toList(value, parameterType);
} else if (isMap(parameterType)) {
value = toMap(value, parameterType);
} else if (isSet(parameterType)) {
value = toSet(value, parameterType);
} else {
value = jsonToObject(value.toString(), parameterType);
}
method.invoke(objectFromJson, new Object[] { value });
noOfFields--;
}
}
}
if (noOfFields != 0) {
throw new IllegalArgumentException("Not enough setter methods for fields in given JSON String : " + jsonString + " in class : " + klass);
}
} catch (InstantiationException e) {
throw new IllegalArgumentException("Couldn't convert JSON to Object of type " + klass, e);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException("Couldn't convert JSON to Object of type " + klass, e);
} catch (GfJsonException e) {
throw new IllegalArgumentException("Couldn't convert JSON to Object of type " + klass, e);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Couldn't convert JSON to Object of type " + klass, e);
} catch (InvocationTargetException e) {
throw new IllegalArgumentException("Couldn't convert JSON to Object of type " + klass, e);
}
return objectFromJson;
}
use of org.apache.geode.management.internal.cli.json.GfJsonObject 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.GfJsonObject in project geode by apache.
the class DataCommandResult method addJSONStringToTable.
private void addJSONStringToTable(TabularResultData table, Object object) {
if (object == null || MISSING_VALUE.equals(object)) {
table.accumulate("Value", MISSING_VALUE);
} else {
try {
GfJsonObject jsonObject = buildGfJsonFromRawObject(object);
addJSONObjectToTable(table, jsonObject);
} catch (Exception e) {
table.accumulate("Value", "Error getting bean properties " + e.getMessage());
}
}
}
use of org.apache.geode.management.internal.cli.json.GfJsonObject in project geode by apache.
the class DataCommandFunction method getJSONForStruct.
private GfJsonObject getJSONForStruct(StructImpl impl, AtomicInteger ai) throws GfJsonException {
String[] fields = impl.getFieldNames();
Object[] values = impl.getFieldValues();
GfJsonObject jsonObject = new GfJsonObject();
for (int i = 0; i < fields.length; i++) {
Object value = values[i];
if (value != null) {
if (JsonUtil.isPrimitiveOrWrapper(value.getClass())) {
jsonObject.put(fields[i], value);
} else {
jsonObject.put(fields[i], toJson(value));
}
} else {
jsonObject.put(fields[i], "null");
}
}
return jsonObject;
}
Aggregations