bluelinelabs / LoganSquare

Screaming fast JSON parsing and serialization library for Android.
Apache License 2.0
3.21k stars 306 forks source link

Generic model parsing error #195

Open nicolashaan opened 7 years ago

nicolashaan commented 7 years ago

Hi, First of all, thank you for this wonderful library! Unfortunately, we are facing an issue with a Generic model. I've modified one of the library test to match the use case:

@JsonObject
public class SimpleGenericModel<T> {

    @JsonField
    public String string;

    @JsonField
    public Date date;

    @JsonField(name = "test_int")
    public int testInt;

    @JsonField(name = "test_long")
    public long testLong;

    @JsonField(name = "test_float")
    public float testFloat;

    @JsonField(name = "test_double")
    public double testDouble;

    @JsonField(name = "test_string")
    public String testString;

    @JsonField(name = "test_int_obj")
    public Integer testIntObj;

    @JsonField(name = "test_long_obj")
    public Long testLongObj;

    @JsonField(name = "test_float_obj")
    public Float testFloatObj;

    @JsonField(name = "test_double_obj")
    public Double testDoubleObj;

    @JsonField(name = "test_t")
    public T testT;

    @JsonField(name = "test_nested_generic")
    public SimpleGenericModel<String> testNestedGeneric;

}
@JsonObject
public class AnotherClass {
        @JsonField
        public int value;

        @JsonField
        public String name;
}

The json looks like this:

String json = "{\"date\":\"2015-02-21T18:45:50.748+0000\",\"string\":\"testString\",\"test_double\":342.0,\"test_double_obj\":345.0,\"test_float\":898.0,\"test_float_obj\":382.0,\"test_int\":32,\"test_int_obj\":323,\"test_long\":932,\"test_long_obj\":3920,\"test_string\":\"anotherTestString\",\"test_t\":{\"name\":\"generic string!\", \"value\":\"123\"}}";

Finally, I'm parsing this data using:

ParameterizedType<SimpleGenericModel<AnotherClass>> parameterizedType
           = new ParameterizedType<SimpleGenericModel<AnotherClass>>() {
};
SimpleGenericModel<AnotherClass> simpleModel
                    = LoganSquare.parse(json, parameterizedType);

This code give me a:

Class com.bluelinelabs.logansquare.processor.model.SimpleGenericModel could not be mapped to a JSON object. Perhaps it hasn't been annotated with @JsonObject?

Is it a supported use case?

Please find below a link to the patch I used to highlight my issue:

https://github.com/NicoEkino/LoganSquare/commit/e2277e0003dd863b6135fc4eba9b8085dfcaf0eb

Best regards, Nicolas