Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ghost opened this issue Nov 5, 2014 · 0 comments
Open

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

ghost opened this issue Nov 5, 2014 · 0 comments

Comments

@ghost
Copy link

ghost commented Nov 5, 2014

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;
}

...

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants