1 package com.fasterxml.jackson.databind.deser; 2 3 import java.util.*; 4 5 import com.fasterxml.jackson.annotation.JsonCreator; 6 import com.fasterxml.jackson.annotation.JsonValue; 7 import com.fasterxml.jackson.databind.*; 8 import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 9 10 @SuppressWarnings("serial") 11 public class TestGenericCollectionDeser 12 extends BaseMapTest 13 { 14 static class ListSubClass extends ArrayList<StringWrapper> { } 15 16 /** 17 * Map class that should behave like {@link ListSubClass}, but by 18 * using annotations. 19 */ 20 @JsonDeserialize(contentAs=StringWrapper.class) 21 static class AnnotatedStringList extends ArrayList<Object> { } 22 23 @JsonDeserialize(contentAs=BooleanElement.class) 24 static class AnnotatedBooleanList extends ArrayList<Object> { } 25 26 protected static class BooleanElement { 27 public Boolean b; 28 29 @JsonCreator BooleanElement(Boolean value)30 public BooleanElement(Boolean value) { b = value; } 31 value()32 @JsonValue public Boolean value() { return b; } 33 } 34 35 /* 36 /********************************************************** 37 /* Tests for sub-classing 38 /********************************************************** 39 */ 40 41 /** 42 * Verifying that sub-classing works ok wrt generics information 43 */ testListSubClass()44 public void testListSubClass() throws Exception 45 { 46 ObjectMapper mapper = new ObjectMapper(); 47 ListSubClass result = mapper.readValue("[ \"123\" ]", ListSubClass.class); 48 assertEquals(1, result.size()); 49 Object value = result.get(0); 50 assertEquals(StringWrapper.class, value.getClass()); 51 StringWrapper bw = (StringWrapper) value; 52 assertEquals("123", bw.str); 53 } 54 55 /* 56 /********************************************************** 57 /* Tests for annotations 58 /********************************************************** 59 */ 60 61 // Verifying that sub-classing works ok wrt generics information testAnnotatedLStringist()62 public void testAnnotatedLStringist() throws Exception 63 { 64 ObjectMapper mapper = new ObjectMapper(); 65 AnnotatedStringList result = mapper.readValue("[ \"...\" ]", AnnotatedStringList.class); 66 assertEquals(1, result.size()); 67 Object ob = result.get(0); 68 assertEquals(StringWrapper.class, ob.getClass()); 69 assertEquals("...", ((StringWrapper) ob).str); 70 } 71 testAnnotatedBooleanList()72 public void testAnnotatedBooleanList() throws Exception 73 { 74 ObjectMapper mapper = new ObjectMapper(); 75 AnnotatedBooleanList result = mapper.readValue("[ false ]", AnnotatedBooleanList.class); 76 assertEquals(1, result.size()); 77 Object ob = result.get(0); 78 assertEquals(BooleanElement.class, ob.getClass()); 79 assertFalse(((BooleanElement) ob).b); 80 } 81 } 82