Search in sources :

Example 6 with GfJsonObject

use of org.apache.geode.management.internal.cli.json.GfJsonObject 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 7 with GfJsonObject

use of org.apache.geode.management.internal.cli.json.GfJsonObject 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 8 with GfJsonObject

use of org.apache.geode.management.internal.cli.json.GfJsonObject 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)

Example 9 with GfJsonObject

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

the class CompositeResultData method addSection.

public SectionResultData addSection(String keyToAccess) {
    GfJsonObject sectionData = new GfJsonObject();
    try {
        contentObject.putAsJSONObject(SectionResultData.generateSectionKey(keyToAccess), sectionData);
    } catch (GfJsonException e) {
        throw new ResultDataException(e.getMessage());
    }
    subsectionCount++;
    return new SectionResultData(sectionData);
}
Also used : GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException)

Example 10 with GfJsonObject

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

the class CompositeResultData method addSection.

public SectionResultData addSection(SectionResultData otherSection) {
    String keyToAccess = String.valueOf(subsectionCount);
    GfJsonObject sectionData = otherSection.getSectionGfJsonObject();
    try {
        contentObject.putAsJSONObject(SectionResultData.generateSectionKey(keyToAccess), sectionData);
    } catch (GfJsonException e) {
        throw new ResultDataException(e.getMessage());
    }
    subsectionCount++;
    return new SectionResultData(sectionData);
}
Also used : GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException)

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