Mario Stoilov
Mario Stoilov

Reputation: 3447

Error while trying to deserialize object

I have this json: {"backwards":false,"idFrom":-1}

And a method:

private Object[] instantiateFromRequest(Class[] paramTypes) throws IOException {
        Object[] params = new Object[paramTypes.length];
        if (paramTypes.length == 0) {
            return params;
        }

        HttpServletRequest req = RequestSupplier.request();
        InputStream is = req.getInputStream();
        ObjectMapper m = new ObjectMapper();
        if (paramTypes.length == 1) {
            params[0] = m.readValue(is, paramTypes[0]);
        } else {
            String[] vals = m.readValue(is, String[].class);//trouble here
            for (int i = 0; i < paramTypes.length; i++) {
                if (paramTypes[i] == String.class) {
                    params[i] = vals[i];
                } else {
                    params[i] = m.readValue(vals[i], paramTypes[i]);
                }

            }
        }
        return params;
    }

(params[0] is long and params params[1] is boolean)
Which should return the objects in the json. The trouble comes on the marked line. I get an Exception: Can not deserialize instance of java.lang.String[] out of START_OBJECT token Which I have no idea why it is happening. Can anybody help me?

EDIT: It actually seems, that I'm trying to get an array, not an object here. Chaning the JSON to :[-1, false] solved my issue

Upvotes: 0

Views: 421

Answers (1)

GingerHead
GingerHead

Reputation: 8240

The problem is that you are trying to serialize through an array of string, where you have another generic type object in your json.

Try the following:

String val = m.readValue(is, String.class);

OR:

ContentBean myContentBean = m.readValue(is, ContentBean.class);

Where ContentBean has two properties:

  1. backwards
  2. idFrom

With their getters and setters.

And it should work perfectly.

EDIT:

Are you sure about:

params[1] is long and params params[0] is boolean

Cause it seems the reverse to me . . .

Upvotes: 1

Related Questions