vavr-io / vavr-jackson

Jackson datatype module for Vavr
Apache License 2.0
99 stars 38 forks source link

Cannot deserialize a JSON list on to IndexedSeq type #125

Closed kennymacleod closed 6 years ago

kennymacleod commented 6 years ago

When deserializing a JSON list structure on to a Java class with a property of type IndexedSeq, you get a JsonMappingException because it's trying to coerce a List$Cons on to IndexedSeq. It works fine if the java type is Array or Seq.

Test case:

public class IndexedSeqTest {
    private final ObjectMapper objectMapper = new ObjectMapper()
            .registerModule(new VavrModule());

    @Test
    public void test() throws Exception {
        final String json = "{\"items\":[\"foo\"]}";

        final Model model = objectMapper.readValue(json, Model.class);
        assertEquals(Array.of("foo"), model.getItems());
    }

    @JsonDeserialize
    static class Model {
        IndexedSeq<String> items;

        public Seq<String> getItems() {
            return items;
        }
    }
}

The problem here appears to be SeqDeserializer, which is checking to see if the property is one of Array, Queue, Stream or Vector, and if it's none of those then it creates a List. Effectively, it's assuming that if the type is neither of those options, then it must be a Seq.

The fix could be to add a clause to SeqDeserializer so that if the type is Array or IndexedSeq, then then create an Array.