use of org.apache.geode.rest.internal.web.exception.MalformedJsonException in project geode by apache.
the class AbstractBaseController method jsonToObjectArray.
Object[] jsonToObjectArray(final String arguments) {
final JSONTypes jsonType = validateJsonAndFindType(arguments);
if (JSONTypes.JSON_ARRAY.equals(jsonType)) {
try {
JSONArray jsonArray = new JSONArray(arguments);
Object[] args = new Object[jsonArray.length()];
for (int index = 0; index < jsonArray.length(); index++) {
args[index] = jsonToObject(jsonArray.get(index).toString());
}
return args;
} catch (JSONException je) {
throw new MalformedJsonException("Json document specified in request body is not valid!", je);
}
} else if (JSONTypes.JSON_OBJECT.equals(jsonType)) {
return new Object[] { jsonToObject(arguments) };
} else {
throw new MalformedJsonException("Json document specified in request body is not valid!");
}
}
use of org.apache.geode.rest.internal.web.exception.MalformedJsonException in project geode by apache.
the class AbstractBaseController method updateMultipleKeys.
ResponseEntity<String> updateMultipleKeys(final String region, final String[] keys, final String json) {
JSONArray jsonArr = null;
try {
jsonArr = new JSONArray(json);
} catch (JSONException e) {
throw new MalformedJsonException("JSON document specified in the request is incorrect", e);
}
if (jsonArr.length() != keys.length) {
throw new MalformedJsonException("Each key must have corresponding value (JSON document) specified in the request");
}
Map<Object, PdxInstance> map = new HashMap<Object, PdxInstance>();
for (int i = 0; i < keys.length; i++) {
if (logger.isDebugEnabled()) {
logger.debug("Updating (put) Json document ({}) having key ({}) in Region ({})", json, keys[i], region);
}
try {
PdxInstance pdxObj = convert(jsonArr.getJSONObject(i).toString());
map.put(keys[i], pdxObj);
} catch (JSONException e) {
throw new MalformedJsonException(String.format("JSON document at index (%1$s) in the request body is incorrect", i), e);
}
}
if (!CollectionUtils.isEmpty(map)) {
putPdxValues(region, map);
}
HttpHeaders headers = new HttpHeaders();
headers.setLocation(toUri(region, StringUtils.arrayToCommaDelimitedString(keys)));
return new ResponseEntity<String>(headers, HttpStatus.OK);
}
use of org.apache.geode.rest.internal.web.exception.MalformedJsonException in project geode by apache.
the class AbstractBaseController method convertJsonArrayIntoPdxCollection.
Collection<PdxInstance> convertJsonArrayIntoPdxCollection(final String jsonArray) {
JSONArray jsonArr = null;
try {
jsonArr = new JSONArray(jsonArray);
Collection<PdxInstance> pdxInstances = new ArrayList<PdxInstance>();
for (int index = 0; index < jsonArr.length(); index++) {
// String element = jsonArr.getJSONObject(i).toString();
// String element = jsonArr.getString(i);
Object object = jsonArr.get(index);
String element = object.toString();
PdxInstance pi = convert(element);
pdxInstances.add(pi);
}
return pdxInstances;
} catch (JSONException je) {
throw new MalformedJsonException("Json document specified in request body is not valid!", je);
}
}
use of org.apache.geode.rest.internal.web.exception.MalformedJsonException in project geode by apache.
the class AbstractBaseController method casValue.
@SuppressWarnings("unchecked")
private <T> T casValue(String regionNamePath, String key, String jsonData) {
JSONObject jsonObject;
try {
jsonObject = new JSONObject(jsonData);
String oldValue = jsonObject.get("@old").toString();
String newValue = jsonObject.get("@new").toString();
return (T) casValue(regionNamePath, key, convert(oldValue), convert(newValue));
} catch (JSONException je) {
throw new MalformedJsonException("Json doc specified in request body is invalid!", je);
}
}
Aggregations