use of org.apache.geode.management.internal.cli.json.GfJsonException 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.GfJsonException in project geode by apache.
the class JsonUtil method getByteArray.
public static byte[] getByteArray(GfJsonObject jsonObject, String byName) {
byte[] byteArray = null;
try {
GfJsonArray jsonArray = jsonObject.getJSONArray(byName);
byteArray = GfJsonArray.toByteArray(jsonArray);
} catch (GfJsonException e) {
throw new ResultDataException(e.getMessage());
}
return byteArray;
}
use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.
the class JsonUtil method toList.
private static Object toList(Object value, Class<?> parameterType) throws GfJsonException {
try {
JSONArray array = (JSONArray) value;
List list = new ArrayList();
for (int i = 0; i < array.length(); i++) {
Object element = array.get(i);
if (isPrimitiveOrWrapper(element.getClass())) {
list.add(element);
} else
throw new GfJsonException("Only primitive types are supported in set type for input commands");
}
return list;
} catch (JSONException e) {
throw new GfJsonException(e);
}
}
use of org.apache.geode.management.internal.cli.json.GfJsonException 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.GfJsonException in project geode by apache.
the class JsonUtil method toSet.
/**
* This is used in Put command this method uses HashSet as default implementation
*
* @param value
* @param parameterType
* @return setValue
* @throws GfJsonException
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private static Object toSet(Object value, Class<?> parameterType) throws GfJsonException {
try {
JSONArray array = (JSONArray) value;
Set set = new HashSet();
for (int i = 0; i < array.length(); i++) {
Object element = array.get(i);
if (isPrimitiveOrWrapper(element.getClass())) {
set.add(element);
} else
throw new GfJsonException("Only primitive types are supported in set type for input commands");
}
return set;
} catch (JSONException e) {
throw new GfJsonException(e);
}
}
Aggregations