Skip to content

There´s a lack of flexibility on GsonDeserialization. #621

@ghost

Description

There´s a lack of flexibility on GsonDeserialization.

"deserialize" method with Json Array as input in "inputStream", will throw "IllegalArgumentException" if there are more than 1 parameter in the "method".

So, if i try to use REST with an additional parameter (let´s say an additional "AuthentitcationToken" parameter with @HeaderParam("token")), it will throw "IllegalArgumentException".


Code (i´ve commented the "if" sentence):

@Deserializes({ "application/json", "json" })
@SuppressWarnings("rawtypes")
public class GsonDeserialization implements Deserializer {
...
...

public Object[] deserialize(InputStream inputStream, ResourceMethod method) {
Class<?>[] types = getTypes(method);

    if (types.length == 0) {
        throw new IllegalArgumentException(
                "Methods that consumes representations must receive at least one argument");
    }

    Gson gson = getGson();

    Object[] params = new Object[types.length];
    String[] parameterNames = paramNameProvider.parameterNamesFor(method.getMethod());

    try {
        String content = getContentOfStream(inputStream);

        if (Strings.isNullOrEmpty(content)) {
            logger.debug("json with no content");
            return params;
        }

        logger.debug("json retrieved: {}", content);

        JsonParser parser = new JsonParser();
        JsonElement jsonElement = parser.parse(content);

        if (jsonElement.isJsonObject()) {
            JsonObject root = jsonElement.getAsJsonObject();

            for (int i = 0; i < types.length; i++) {
                String name = parameterNames[i];
                JsonElement node = root.get(name);

                if (isWithoutRoot(parameterNames, root)) {
                    params[i] = gson.fromJson(root, types[i]);
                    logger.info("json without root deserialized");
                    break;
                } else if (node != null) {
                    if (node.isJsonArray()) {
                        JsonArray jsonArray = node.getAsJsonArray();

                        Type type = method.getMethod().getGenericParameterTypes()[i];
                        if (type instanceof ParameterizedType) {
                            params[i] = gson.fromJson(jsonArray, type);
                        } else {
                            params[i] = gson.fromJson(jsonArray, types[i]);
                        }
                    } else {
                        params[i] = gson.fromJson(node, types[i]);
                    }
                }
            }
        } else if (jsonElement.isJsonArray()) {
        /*  
            if ((parameterNames.length != 1) || (!(method.getMethod().getGenericParameterTypes()[0] instanceof ParameterizedType))) {
                throw new IllegalArgumentException("Methods that consumes an array representation must receive only just one collection generic typed argument");
            }
        */  
            JsonArray jsonArray= jsonElement.getAsJsonArray();
            params[0] = gson.fromJson(jsonArray, method.getMethod().getGenericParameterTypes()[0]);

        }
    } catch (Exception e) {
        throw new ResultException("Unable to deserialize data", e);
    }

    logger.debug("json deserialized: {}", (Object) params);
    return params;
}

...

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions