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;
}
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;
}
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;
}
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");
}
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;
}
}
Aggregations