Search in sources :

Example 31 with GfJsonObject

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

the class DataCommandFunction method select_NonSelectResults.

private void select_NonSelectResults(Object results, List<SelectResultRow> list) {
    if (logger.isDebugEnabled()) {
        logger.debug("BeanResults : Bean Results class is {}", results.getClass());
    }
    String str = toJson(results);
    GfJsonObject jsonBean;
    try {
        jsonBean = new GfJsonObject(str);
    } catch (GfJsonException e) {
        logger.info("Exception occurred:", e);
        jsonBean = new GfJsonObject();
        try {
            jsonBean.put("msg", e.getMessage());
        } catch (GfJsonException e1) {
            logger.warn("Ignored GfJsonException:", e1);
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("BeanResults : Adding bean json string : {}", jsonBean);
    }
    list.add(new SelectResultRow(DataCommandResult.ROW_TYPE_BEAN, jsonBean.toString()));
}
Also used : GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) SelectResultRow(org.apache.geode.management.internal.cli.domain.DataCommandResult.SelectResultRow) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException)

Example 32 with GfJsonObject

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

the class DataCommandFunction method getJSONForNonPrimitiveObject.

@SuppressWarnings({ "rawtypes" })
public static Object[] getJSONForNonPrimitiveObject(Object obj) {
    Object[] array = new Object[2];
    if (obj == null) {
        array[0] = null;
        array[1] = "<NULL>";
        return array;
    } else {
        array[0] = obj.getClass().getCanonicalName();
        Class klass = obj.getClass();
        if (JsonUtil.isPrimitiveOrWrapper(klass)) {
            array[1] = obj;
        } else if (obj instanceof PdxInstance) {
            String str = pdxToJson((PdxInstance) obj);
            array[1] = str;
        } else {
            GfJsonObject object = new GfJsonObject(obj, true);
            Iterator keysIterator = object.keys();
            while (keysIterator.hasNext()) {
                String key = (String) keysIterator.next();
                Object value = object.get(key);
                if (GfJsonObject.isJSONKind(value)) {
                    GfJsonObject jsonVal = new GfJsonObject(value);
                    // System.out.println("Re-wrote inner object");
                    try {
                        if (jsonVal.has("type-class")) {
                            object.put(key, jsonVal.get("type-class"));
                        } else {
                            // Its Map Value
                            object.put(key, "a Map");
                        }
                    } catch (GfJsonException e) {
                        throw new RuntimeException(e);
                    }
                } else if (value instanceof JSONArray) {
                    // Its a collection either a set or list
                    try {
                        object.put(key, "a Collection");
                    } catch (GfJsonException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
            String str = object.toString();
            array[1] = str;
        }
        return array;
    }
}
Also used : PdxInstance(org.apache.geode.pdx.PdxInstance) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) Iterator(java.util.Iterator) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) JSONArray(org.json.JSONArray) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject)

Example 33 with GfJsonObject

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

the class DataCommandFunctionWithPDXJUnitTest method testVerifyDataDoesNotShift.

@Test
public void testVerifyDataDoesNotShift() throws Exception {
    TabularResultData rawTable = getTableFromQuery("select * from /" + PARTITIONED_REGION + " order by id");
    // Verify any table built
    assertThat(rawTable.getGfJsonObject()).isNotNull();
    assertThat(rawTable.getGfJsonObject().getJSONObject("content")).isNotNull();
    GfJsonObject tableJson = rawTable.getGfJsonObject().getJSONObject("content");
    // Table only contains correct keys
    assertThat(tableJson.getJSONArray("missingKey")).isNull();
    // Table contains correct data
    assertThatRowIsBuiltCorrectly(tableJson, 0, alice);
    assertThatRowIsBuiltCorrectly(tableJson, 1, bob);
    assertThatRowIsBuiltCorrectly(tableJson, 2, charlie);
    assertThatRowIsBuiltCorrectly(tableJson, 3, dan);
}
Also used : TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 34 with GfJsonObject

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

the class DataCommandFunctionWithPDXJUnitTest method testFilteredQueryWithoutPhone.

@Test
public void testFilteredQueryWithoutPhone() throws Exception {
    TabularResultData rawTable = getTableFromQuery("select * from /" + PARTITIONED_REGION + " c where IS_UNDEFINED ( c.phone ) order by id");
    assertThat(rawTable.getGfJsonObject()).isNotNull();
    assertThat(rawTable.getGfJsonObject().getJSONObject("content")).isNotNull();
    GfJsonObject tableJson = rawTable.getGfJsonObject().getJSONObject("content");
    for (String k : new String[] { "id", "firstName", "lastName" }) {
        assertThat(tableJson.getJSONArray(k).size()).isEqualTo(2);
    }
    assertThatRowIsBuiltCorrectly(tableJson, 0, alice);
    assertThatRowIsBuiltCorrectly(tableJson, 1, bob);
}
Also used : TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

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