0

I am rewriting the question since I figured out the actual error in the code.

This is a fully functional example of my issue (I am using Jackson 2.9.0):

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import java.io.IOException;
import java.net.URL;
import java.util.List;

public class MainClass {

    public static class SubClass<TYPE> {
        private List<TYPE> values;

        public List<TYPE> getValues() {
            return values;
        }

        public void setValues(List<TYPE> values) {
            this.values = values;
        }
    }

    public static class Foo {
        private String name;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }

    private SubClass<Foo> propertyFoo;

    public SubClass<Foo> getPropertyFoo() {
        return propertyFoo;
    }

    public void setPropertyFoo(SubClass propertyFoo) {
        this.propertyFoo = propertyFoo;
    }

    public static void main(String args[]) throws IOException {
        URL url = System.class.getResource("/testFoo.json");
        ObjectMapper mapper = new ObjectMapper();
        ObjectReader reader = mapper.readerFor(MainClass.class);
        MainClass mainClass = reader.readValue(url);
        mainClass.getPropertyFoo().getValues().forEach(foo -> {
           System.out.println(String.format("name: %s", foo.getName()));
        });
    }
}

Note the missing type parameter:

public void setPropertyFoo(SubClass propertyFoo)

instead of

public void setPropertyFoo(SubClass<Foo> propertyFoo)

The first form compiles but produces the following exception when run

Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to MainClass$Foo
hvm2hvm
  • 63
  • 7
  • Does this answer you? https://stackoverflow.com/a/41567297/460557 ? – Jorge Campos Sep 18 '17 at 17:36
  • I can't reproduce your behavior. Please provide a [mcve]. What is `reader`? How did you initialize it? – Sotirios Delimanolis Sep 18 '17 at 17:40
  • Also, which version of Jackson are you working with? The latest Jackson 2 should handle this type and object hierarchy just fine. – Sotirios Delimanolis Sep 18 '17 at 17:50
  • Type erasure is not a limitation here. Jackson can tell the field is typed as `SubClass` and propagate that parameterized type to the deserialization of that part of the JSON. – Sotirios Delimanolis Sep 18 '17 at 17:51
  • Possible duplicate of [Jackson - Deserialize using generic class](https://stackoverflow.com/questions/11664894/jackson-deserialize-using-generic-class) – Naman Sep 18 '17 at 17:53
  • @nullpointer That's wrong as well. Their `MainClass` is not a generic type. – Sotirios Delimanolis Sep 18 '17 at 17:54
  • @SotiriosDelimanolis From the comments, the `propertyFoo` should be by default treated as a Map by jackson which is further mapped to a specific class type. – Naman Sep 18 '17 at 17:55
  • I was about to reply that that is wrong. Jackson will only use `LinkedHashMap` for fields it can't determine a type for, like `Object` or a generic `T` with no type information to resolve it (or the field was literally of type `Map`). But here the field is very clearly of type `SubClass`. Also, how would they apply the answers from that solution? How's the `TypeReference` parameterized? – Sotirios Delimanolis Sep 18 '17 at 17:57
  • I will provide a better example a bit later. Right now I can only say that the minimal example here also works for me – hvm2hvm Sep 18 '17 at 18:11
  • I finally figured it out. I was refactoring and missed the generic parameter in the setter. That was making Jackson confused I guess. – hvm2hvm Sep 19 '17 at 15:46

1 Answers1

0

As I wrote in the newly edited post, my issue was with that setter. Although it compiles it was throwing an exception since Jackson couldn't find the right type to use for the inner objects.

Fixing

public void setPropertyFoo(SubClass propertyFoo)

to

public void setPropertyFoo(SubClass<Foo> propertyFoo)

I know it's a trivial error but since I already spent all this time on finding the issue, maybe it will help somebody. If a mod/admin thinks this is not helpful, I can delete it whole.

hvm2hvm
  • 63
  • 7