Search in sources :

Example 1 with JsonSingleInput

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;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) JsonSingleInput(org.structr.core.JsonSingleInput) IJsonInput(org.structr.core.IJsonInput)

Example 2 with JsonSingleInput

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;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonInput(org.structr.core.JsonInput) IJsonInput(org.structr.core.IJsonInput) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonSingleInput(org.structr.core.JsonSingleInput) JsonElement(com.google.gson.JsonElement) IJsonInput(org.structr.core.IJsonInput)

Aggregations

JsonSyntaxException (com.google.gson.JsonSyntaxException)2 IJsonInput (org.structr.core.IJsonInput)2 JsonSingleInput (org.structr.core.JsonSingleInput)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 FrameworkException (org.structr.common.error.FrameworkException)1 JsonInput (org.structr.core.JsonInput)1 Tx (org.structr.core.graph.Tx)1