Search in sources :

Example 26 with GfJsonException

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;
}
Also used : GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException)

Example 27 with GfJsonException

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;
}
Also used : GfJsonArray(org.apache.geode.management.internal.cli.json.GfJsonArray) ResultDataException(org.apache.geode.management.internal.cli.result.ResultDataException) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException)

Example 28 with GfJsonException

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);
    }
}
Also used : JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) JSONException(org.json.JSONException) ArrayList(java.util.ArrayList) List(java.util.List) JSONObject(org.json.JSONObject) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject)

Example 29 with GfJsonException

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;
}
Also used : HashMap(java.util.HashMap) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) JSONObject(org.json.JSONObject) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject)

Example 30 with GfJsonException

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);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) JSONArray(org.json.JSONArray) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) HashSet(java.util.HashSet)

Aggregations

GfJsonException (org.apache.geode.management.internal.cli.json.GfJsonException)34 GfJsonObject (org.apache.geode.management.internal.cli.json.GfJsonObject)27 GfJsonArray (org.apache.geode.management.internal.cli.json.GfJsonArray)14 JSONObject (org.json.JSONObject)9 ArrayList (java.util.ArrayList)6 ResultDataException (org.apache.geode.management.internal.cli.result.ResultDataException)4 TabularResultData (org.apache.geode.management.internal.cli.result.TabularResultData)4 JSONArray (org.json.JSONArray)4 JSONException (org.json.JSONException)4 HashMap (java.util.HashMap)3 SelectResultRow (org.apache.geode.management.internal.cli.domain.DataCommandResult.SelectResultRow)3 CompositeResultData (org.apache.geode.management.internal.cli.result.CompositeResultData)3 SectionResultData (org.apache.geode.management.internal.cli.result.CompositeResultData.SectionResultData)3 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 List (java.util.List)2 TreeMap (java.util.TreeMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 CliJsonSerializable (org.apache.geode.management.internal.cli.result.CliJsonSerializable)2 Table (org.apache.geode.management.internal.cli.result.TableBuilder.Table)2