Search in sources :

Example 26 with GfJsonObject

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

Example 27 with GfJsonObject

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;
}
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 28 with GfJsonObject

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

Example 29 with GfJsonObject

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

Example 30 with GfJsonObject

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

Aggregations

GfJsonObject (org.apache.geode.management.internal.cli.json.GfJsonObject)34 GfJsonException (org.apache.geode.management.internal.cli.json.GfJsonException)23 GfJsonArray (org.apache.geode.management.internal.cli.json.GfJsonArray)9 JSONObject (org.json.JSONObject)5 TabularResultData (org.apache.geode.management.internal.cli.result.TabularResultData)4 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 Row (org.apache.geode.management.internal.cli.result.TableBuilder.Row)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 SelectResultRow (org.apache.geode.management.internal.cli.domain.DataCommandResult.SelectResultRow)2 CliJsonSerializable (org.apache.geode.management.internal.cli.result.CliJsonSerializable)2 ResultDataException (org.apache.geode.management.internal.cli.result.ResultDataException)2 RowGroup (org.apache.geode.management.internal.cli.result.TableBuilder.RowGroup)2 Table (org.apache.geode.management.internal.cli.result.TableBuilder.Table)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1