Search in sources :

Example 6 with GfJsonException

use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.

the class JsonUtil method jsonToMap.

/**
   * Converts given JSON String in to a Map. Refer http://www.json.org/ to construct a JSON format.
   * 
   * @param jsonString jsonString to be converted in to a Map.
   * @return a Map created from
   * 
   * @throws IllegalArgumentException if the specified JSON string can not be converted in to a Map
   */
public static Map<String, String> jsonToMap(String jsonString) {
    Map<String, String> jsonMap = new TreeMap<String, String>();
    try {
        GfJsonObject jsonObject = new GfJsonObject(jsonString);
        Iterator<String> keys = jsonObject.keys();
        while (keys.hasNext()) {
            String key = keys.next();
            jsonMap.put(key, jsonObject.getString(key));
        }
    } catch (GfJsonException e) {
        throw new IllegalArgumentException("Could not convert jsonString : '" + jsonString + "' to map.");
    }
    return jsonMap;
}
Also used : GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) TreeMap(java.util.TreeMap)

Example 7 with GfJsonException

use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.

the class JsonUtil method getSet.

public static Set<CliJsonSerializable> getSet(GfJsonObject jsonObject, String byName) {
    Set<CliJsonSerializable> cliJsonSerializables = Collections.emptySet();
    try {
        GfJsonArray cliJsonSerializableArray = jsonObject.getJSONArray(byName);
        int size = cliJsonSerializableArray.size();
        if (size > 0) {
            cliJsonSerializables = new HashSet<CliJsonSerializable>();
        }
        for (int i = 0; i < size; i++) {
            GfJsonObject cliJsonSerializableState = cliJsonSerializableArray.getJSONObject(i);
            int jsId = cliJsonSerializableState.getInt(CliJsonSerializable.JSID);
            CliJsonSerializable cliJsonSerializable = CliJsonSerializableFactory.getCliJsonSerializable(jsId);
            cliJsonSerializable.fromJson(cliJsonSerializableState);
            cliJsonSerializables.add(cliJsonSerializable);
        }
    } catch (GfJsonException e) {
        throw new ResultDataException(e.getMessage());
    }
    return cliJsonSerializables;
}
Also used : GfJsonArray(org.apache.geode.management.internal.cli.json.GfJsonArray) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) ResultDataException(org.apache.geode.management.internal.cli.result.ResultDataException) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) CliJsonSerializable(org.apache.geode.management.internal.cli.result.CliJsonSerializable)

Example 8 with GfJsonException

use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.

the class JsonUtil method jsonToObject.

public static Object jsonToObject(String jsonString) {
    Object objectFromJson = null;
    try {
        GfJsonObject jsonObject = new GfJsonObject(jsonString);
        Iterator<String> keys = jsonObject.keys();
        Object[] arr = new Object[jsonObject.size()];
        int i = 0;
        while (keys.hasNext()) {
            String key = keys.next();
            Class<?> klass = ClassPathLoader.getLatest().forName(key);
            arr[i++] = jsonToObject((String) jsonObject.get(key).toString(), klass);
        }
        if (arr.length == 1) {
            objectFromJson = arr[0];
        } else {
            objectFromJson = arr;
        }
    } catch (GfJsonException e) {
        throw new IllegalArgumentException("Couldn't convert JSON to Object.", e);
    } catch (ClassNotFoundException e) {
        throw new IllegalArgumentException("Couldn't convert JSON to Object.", e);
    }
    return objectFromJson;
}
Also used : GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) JSONObject(org.json.JSONObject) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject)

Example 9 with GfJsonException

use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.

the class JsonUtil method toArray.

private static Object toArray(Object value, Class<?> parameterType) throws GfJsonException {
    Class arrayComponentType = parameterType.getComponentType();
    if (isPrimitiveOrWrapper(arrayComponentType)) {
        if (value instanceof JSONArray) {
            try {
                JSONArray jsonArray = (JSONArray) value;
                Object jArray = Array.newInstance(arrayComponentType, jsonArray.length());
                for (int i = 0; i < jsonArray.length(); i++) {
                    Array.set(jArray, i, jsonArray.get(i));
                }
                return jArray;
            } catch (ArrayIndexOutOfBoundsException e) {
                throw new GfJsonException(e);
            } catch (IllegalArgumentException e) {
                throw new GfJsonException(e);
            } catch (JSONException e) {
                throw new GfJsonException(e);
            }
        } else {
            throw new GfJsonException("Expected JSONArray for array type");
        }
    } else
        throw new GfJsonException("Array contains non-primitive element. Non-primitive elements are not supported in json array");
}
Also used : 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)

Example 10 with GfJsonException

use of org.apache.geode.management.internal.cli.json.GfJsonException in project geode by apache.

the class CommandResult method buildComposite.

/* private */
void buildComposite() {
    try {
        GfJsonObject content = getContent();
        if (content != null) {
            Table resultTable = TableBuilder.newTable();
            resultTable.setColumnSeparator(" : ");
            addHeaderInTable(resultTable, getGfJsonObject());
            for (Iterator<String> it = content.keys(); it.hasNext(); ) {
                String key = it.next();
                if (key.startsWith(CompositeResultData.SECTION_DATA_ACCESSOR)) {
                    GfJsonObject subSection = content.getJSONObject(key);
                    buildSection(resultTable, null, subSection, 0);
                } else if (key.equals(CompositeResultData.SEPARATOR)) {
                    String separatorString = content.getString(key);
                    resultTable.newRowGroup().newRowSeparator(separatorString.charAt(0), true);
                }
            }
            addFooterInTable(resultTable, getGfJsonObject());
            resultLines.addAll(resultTable.buildTableList());
        }
    } catch (GfJsonException e) {
        resultLines.add("Error occurred while processing Command Result. Internal Error - Invalid Result.");
        LogWrapper.getInstance().info("Error occurred while processing Command Result. Internal Error - Invalid Result.", e);
    } finally {
        isDataBuilt = true;
    }
}
Also used : Table(org.apache.geode.management.internal.cli.result.TableBuilder.Table) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException)

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