use of org.structr.core.JsonSingleInput in project structr by structr.
the class JsonRestServlet method cleanAndParseJsonString.
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="private methods">
private IJsonInput cleanAndParseJsonString(final App app, final String input) throws FrameworkException {
IJsonInput jsonInput = null;
// isolate input parsing (will include read and write operations)
try (final Tx tx = app.tx()) {
jsonInput = gson.get().fromJson(input, IJsonInput.class);
tx.success();
} catch (JsonSyntaxException jsx) {
logger.warn("", jsx);
throw new FrameworkException(400, jsx.getMessage());
}
if (jsonInput == null) {
if (StringUtils.isBlank(input)) {
try (final Tx tx = app.tx()) {
jsonInput = gson.get().fromJson("{}", IJsonInput.class);
tx.success();
}
} else {
// throw new JsonParseException("Invalid or empty JSON string, must at least contain {} to be valid!");
jsonInput = new JsonSingleInput();
}
}
return jsonInput;
}
use of org.structr.core.JsonSingleInput in project structr by structr.
the class JsonInputGSONAdapter method deserialize.
@Override
public IJsonInput deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
IJsonInput jsonInput = null;
JsonInput wrapper = null;
if (json.isJsonObject()) {
jsonInput = new JsonSingleInput();
wrapper = deserialize(json, context);
jsonInput.add(wrapper);
} else if (json.isJsonArray()) {
jsonInput = new JsonSingleInput();
JsonArray array = json.getAsJsonArray();
for (final JsonElement elem : array) {
wrapper = deserialize(elem, context);
jsonInput.add(wrapper);
}
} else {
// not one of the expected types => error
throw new JsonSyntaxException("Invalid JSON, expecting object or array");
}
return jsonInput;
}
Aggregations