Skip to content

Commit 5612187

Browse files
committed
Merge branch '2.6'
2 parents 98abe64 + 8574ed4 commit 5612187

File tree

4 files changed

+147
-3
lines changed

4 files changed

+147
-3
lines changed

src/test/java/com/fasterxml/jackson/databind/deser/TestCollectionDeserialization.java

+21
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public CustomList deserialize(JsonParser jp, DeserializationContext ctxt)
3737

3838
static class XBean {
3939
public int x;
40+
41+
public XBean() { }
42+
public XBean(int x) { this.x = x; }
4043
}
4144

4245
// [Issue#199]
@@ -281,4 +284,22 @@ public void testWrapExceptions() throws Exception
281284
assertEquals("I want to catch this exception", exc.getMessage());
282285
}
283286
}
287+
288+
// And then a round-trip test for singleton collections
289+
public void testSingletonCollections() throws Exception
290+
{
291+
final TypeReference<?> xbeanListType = new TypeReference<List<XBean>>() { };
292+
293+
String json = MAPPER.writeValueAsString(Collections.singleton(new XBean(3)));
294+
Collection<XBean> result = MAPPER.readValue(json, xbeanListType);
295+
assertNotNull(result);
296+
assertEquals(1, result.size());
297+
assertEquals(3, result.iterator().next().x);
298+
299+
json = MAPPER.writeValueAsString(Collections.singletonList(new XBean(28)));
300+
result = MAPPER.readValue(json, xbeanListType);
301+
assertNotNull(result);
302+
assertEquals(1, result.size());
303+
assertEquals(28, result.iterator().next().x);
304+
}
284305
}

src/test/java/com/fasterxml/jackson/databind/deser/TestMapDeserialization.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.fasterxml.jackson.core.type.TypeReference;
1212
import com.fasterxml.jackson.databind.*;
1313
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
14+
import com.fasterxml.jackson.databind.deser.TestCollectionDeserialization.XBean;
1415
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
1516

1617
@SuppressWarnings("serial")
@@ -576,7 +577,21 @@ public void testReadProperties() throws Exception
576577
assertEquals("123", props.getProperty("b"));
577578
assertEquals("true", props.getProperty("c"));
578579
}
579-
580+
581+
// JDK singletonMap
582+
public void testSingletonMapRoundtrip() throws Exception
583+
{
584+
final TypeReference<?> type = new TypeReference<Map<String,IntWrapper>>() { };
585+
586+
String json = MAPPER.writeValueAsString(Collections.singletonMap("value", new IntWrapper(5)));
587+
Map<String,IntWrapper> result = MAPPER.readValue(json, type);
588+
assertNotNull(result);
589+
assertEquals(1, result.size());
590+
IntWrapper w = result.get("value");
591+
assertNotNull(w);
592+
assertEquals(5, w.i);
593+
}
594+
580595
/*
581596
/**********************************************************
582597
/* Error tests

src/test/java/com/fasterxml/jackson/failing/EnumCreatorTest929.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
public class EnumCreatorTest929 extends BaseMapTest
88
{
9-
private final ObjectMapper MAPPER = new ObjectMapper();
10-
119
static enum MyEnum
1210
{
1311
A, B, C;
@@ -20,6 +18,8 @@ static MyEnum forValues(@JsonProperty("id") int intProp,
2018
}
2119
}
2220

21+
private final ObjectMapper MAPPER = new ObjectMapper();
22+
2323
// for [databind#929]
2424
public void testMultiArgEnumCreator() throws Exception
2525
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.fasterxml.jackson.failing;
2+
3+
import java.util.*;
4+
5+
import com.fasterxml.jackson.annotation.JsonCreator;
6+
import com.fasterxml.jackson.databind.*;
7+
8+
// For [databind#936], losing parametric type information it seems
9+
public class PolymorphicListTest936 extends BaseMapTest
10+
{
11+
// note: would prefer using CharSequence, but while abstract, that's deserialized
12+
// just fine as ... String
13+
static class StringyList<T extends java.io.Serializable> implements Collection<T> {
14+
private Collection<T> _stuff;
15+
16+
@JsonCreator
17+
public StringyList(Collection<T> src) {
18+
_stuff = new ArrayList<T>(src);
19+
}
20+
21+
public StringyList() {
22+
_stuff = new ArrayList<T>();
23+
}
24+
25+
@Override
26+
public boolean add(T arg) {
27+
return _stuff.add(arg);
28+
}
29+
30+
@Override
31+
public boolean addAll(Collection<? extends T> args) {
32+
return _stuff.addAll(args);
33+
}
34+
35+
@Override
36+
public void clear() {
37+
_stuff.clear();
38+
}
39+
40+
@Override
41+
public boolean contains(Object arg) {
42+
return _stuff.contains(arg);
43+
}
44+
45+
@Override
46+
public boolean containsAll(Collection<?> args) {
47+
return _stuff.containsAll(args);
48+
}
49+
50+
@Override
51+
public boolean isEmpty() {
52+
return _stuff.isEmpty();
53+
}
54+
55+
@Override
56+
public Iterator<T> iterator() {
57+
return _stuff.iterator();
58+
}
59+
60+
@Override
61+
public boolean remove(Object arg) {
62+
return _stuff.remove(arg);
63+
}
64+
65+
@Override
66+
public boolean removeAll(Collection<?> args) {
67+
return _stuff.removeAll(args);
68+
}
69+
70+
@Override
71+
public boolean retainAll(Collection<?> args) {
72+
return _stuff.retainAll(args);
73+
}
74+
75+
@Override
76+
public int size() {
77+
return _stuff.size();
78+
}
79+
80+
@Override
81+
public Object[] toArray() {
82+
return _stuff.toArray();
83+
}
84+
85+
@Override
86+
public <X> X[] toArray(X[] arg) {
87+
return _stuff.toArray(arg);
88+
}
89+
}
90+
91+
private final ObjectMapper MAPPER = new ObjectMapper();
92+
93+
public void testPolymorphicWithOverride() throws Exception
94+
{
95+
JavaType type = MAPPER.getTypeFactory().constructCollectionType(StringyList.class, String.class);
96+
97+
StringyList<String> list = new StringyList<String>();
98+
list.add("value 1");
99+
list.add("value 2");
100+
101+
String serialized = MAPPER.writeValueAsString(list);
102+
System.out.println(serialized);
103+
104+
StringyList<String> deserialized = MAPPER.readValue(serialized, type);
105+
System.out.println(deserialized);
106+
107+
}
108+
}

0 commit comments

Comments
 (0)