Parsing number as string with Jackson 2 in Google HTTP Java Client

558 views Asked by At

I am using Jackson 2 as parser in google-http-java-client. When a number is mapped to a field of String class I get an IllegalArgumentException. In this case integer 1234 is about to be mapped to Model.b which is a String. However, the a string can be mapped to an integer field, it is "5678" to Model.c which is an int.

Caused by: java.lang.IllegalArgumentException: expected numeric type but got class java.lang.String
at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:843)
... 22 more

The JSON is like this:

{
    "a": "Something",
    "b": 1234,
    "c": "5678"
}

The POJO is like this:

public class Model {
    @Key String a;
    @Key String b;
    @Key int c;
}

How to solve it? Or can Jackson do customized parsing to achieve this?

UPDATE

I use it in Android with RoboSpice library. This is the gradle configuration:

...
compile 'com.octo.android.robospice:robospice-google-http-client:1.4.14'
compile('com.google.http-client:google-http-client-jackson2:1.19.0') {
    exclude module: 'xpp3'
    exclude group: 'stax'
}
...

This the output of ./gradlew dependencies:

WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.1 is ignored for de
bug as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class
 packages
WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.1 is ignored for re
lease as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class
 packages
WARNING [Project: :app] variant.getProcessResources() is deprecated. Call it on 
one of variant.getOutputs() instead.
WARNING [Project: :app] variant.getProcessResources() is deprecated. Call it on 
one of variant.getOutputs() instead.
:dependencies

------------------------------------------------------------
Root project
------------------------------------------------------------

No configurations

BUILD SUCCESSFUL

Total time: 13.873 secs
0

There are 0 answers