Skip to content

Commit c2b266c

Browse files
committed
Fix #2693
1 parent f2127c8 commit c2b266c

4 files changed

Lines changed: 127 additions & 4 deletions

File tree

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ Project: jackson-databind
6161
(requested by Rafal K)
6262
#2668: `IllegalArgumentException` thrown for mismatched subclass deserialization
6363
(reported by nbruno@github)
64+
#2693: Add convenience methods for creating `List`, `Map` valued `ObjectReader`s
65+
(ObjectMapper.readerForListOf())
6466
- Add `SerializerProvider.findContentValueSerializer()` methods
6567

6668
2.10.4 (not yet released)

src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3951,6 +3951,54 @@ public ObjectReader readerFor(TypeReference<?> type) {
39513951
null, _injectableValues);
39523952
}
39533953

3954+
/**
3955+
* Factory method for constructing {@link ObjectReader} that will
3956+
* read values of a type {@code List<type>}.
3957+
* Functionally same as:
3958+
*<pre>
3959+
* readerFor(type[].class);
3960+
*</pre>
3961+
*
3962+
* @since 2.11
3963+
*/
3964+
public ObjectReader readerForArrayOf(Class<?> type) {
3965+
return _newReader(getDeserializationConfig(),
3966+
_typeFactory.constructArrayType(type), null,
3967+
null, _injectableValues);
3968+
}
3969+
3970+
/**
3971+
* Factory method for constructing {@link ObjectReader} that will
3972+
* read or update instances of a type {@code List<type>}.
3973+
* Functionally same as:
3974+
*<pre>
3975+
* readerFor(new TypeReference&lt;List&lt;type>>() { });
3976+
*</pre>
3977+
*
3978+
* @since 2.11
3979+
*/
3980+
public ObjectReader readerForListOf(Class<?> type) {
3981+
return _newReader(getDeserializationConfig(),
3982+
_typeFactory.constructCollectionType(List.class, type), null,
3983+
null, _injectableValues);
3984+
}
3985+
3986+
/**
3987+
* Factory method for constructing {@link ObjectReader} that will
3988+
* read or update instances of a type {@code Map<String, type>}
3989+
* Functionally same as:
3990+
*<pre>
3991+
* readerFor(new TypeReference&lt;Map&lt;String, type>>() { });
3992+
*</pre>
3993+
*
3994+
* @since 2.11
3995+
*/
3996+
public ObjectReader readerForMapOf(Class<?> type) {
3997+
return _newReader(getDeserializationConfig(),
3998+
_typeFactory.constructMapType(Map.class, String.class, type), null,
3999+
null, _injectableValues);
4000+
}
4001+
39544002
/**
39554003
* Factory method for constructing {@link ObjectReader} that will
39564004
* use specified {@link JsonNodeFactory} for constructing JSON trees.

src/test/java/com/fasterxml/jackson/databind/ObjectReaderTest.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
import java.io.IOException;
44
import java.io.StringWriter;
5-
import java.util.Collections;
6-
import java.util.List;
7-
import java.util.Map;
8-
import java.util.Set;
5+
import java.util.*;
96

107
import com.fasterxml.jackson.annotation.JsonCreator;
118
import com.fasterxml.jackson.annotation.JsonProperty;
9+
1210
import com.fasterxml.jackson.core.*;
1311
import com.fasterxml.jackson.core.json.JsonReadFeature;
12+
1413
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
1514
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
1615
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
@@ -60,6 +59,36 @@ public void testSimpleAltSources() throws Exception
6059
}
6160
}
6261

62+
// [databind#2693]: convenience read methods:
63+
public void testReaderForArrayOf() throws Exception
64+
{
65+
Object value = MAPPER.readerForArrayOf(ABC.class)
66+
.readValue("[ \"A\", \"C\" ]");
67+
assertEquals(ABC[].class, value.getClass());
68+
ABC[] abcs = (ABC[]) value;
69+
assertEquals(2, abcs.length);
70+
assertEquals(ABC.A, abcs[0]);
71+
assertEquals(ABC.C, abcs[1]);
72+
}
73+
74+
// [databind#2693]: convenience read methods:
75+
public void testReaderForListOf() throws Exception
76+
{
77+
Object value = MAPPER.readerForListOf(ABC.class)
78+
.readValue("[ \"B\", \"C\" ]");
79+
assertEquals(ArrayList.class, value.getClass());
80+
assertEquals(Arrays.asList(ABC.B, ABC.C), value);
81+
}
82+
83+
// [databind#2693]: convenience read methods:
84+
public void testReaderForMapOf() throws Exception
85+
{
86+
Object value = MAPPER.readerForMapOf(ABC.class)
87+
.readValue("{\"key\" : \"B\" }");
88+
assertEquals(LinkedHashMap.class, value.getClass());
89+
assertEquals(Collections.singletonMap("key", ABC.B), value);
90+
}
91+
6392
public void testParserFeatures() throws Exception
6493
{
6594
final String JSON = "[ /* foo */ 7 ]";
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.fasterxml.jackson.failing;
2+
3+
import java.util.*;
4+
5+
import com.fasterxml.jackson.annotation.JsonCreator;
6+
import com.fasterxml.jackson.annotation.JsonProperty;
7+
import com.fasterxml.jackson.databind.BaseMapTest;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
10+
public class SetterlessList2692Test extends BaseMapTest
11+
{
12+
static class DataBean {
13+
14+
final String val;
15+
16+
@JsonCreator
17+
public DataBean(@JsonProperty(value = "val") String val) {
18+
super();
19+
this.val = val;
20+
}
21+
22+
public String getVal() {
23+
return val;
24+
}
25+
26+
public List<String> getList(){
27+
return new ArrayList<>();
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return "DataBean [val=" + val + "]";
33+
}
34+
}
35+
36+
public void testIssue2692() throws Exception {
37+
ObjectMapper om = newJsonMapper();
38+
String json;
39+
DataBean out;
40+
json = "{\"list\":[\"11\"],\"val\":\"VAL2\"}";
41+
out = om.readerFor(DataBean.class).readValue(json);
42+
System.out.println("this is ko" + out);
43+
}
44+
}

0 commit comments

Comments
 (0)