Skip to content

Commit b4b0968

Browse files
authored
Fix #294 (via databind), add tests to show fix works (#300)
1 parent d8d0f5c commit b4b0968

File tree

3 files changed

+78
-8
lines changed

3 files changed

+78
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.fasterxml.jackson.datatype.jdk8;
2+
3+
import java.util.Optional;
4+
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
7+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
8+
import com.fasterxml.jackson.databind.util.StdConverter;
9+
10+
// [modules-java#294]: Optional + converters
11+
public class OptionalConverterTest extends ModuleTestBase
12+
{
13+
static class Point
14+
{
15+
public int x, y;
16+
17+
protected Point() { }
18+
public Point(int v1, int v2) {
19+
x = v1;
20+
y = v2;
21+
}
22+
}
23+
24+
static class PointDeserConverter extends StdConverter<int[], Point>
25+
{
26+
@Override
27+
public Point convert(int[] value) {
28+
return new Point(value[0], value[1]);
29+
}
30+
}
31+
32+
static class PointSerConverter extends StdConverter<Point, int[]>
33+
{
34+
@Override public int[] convert(Point value) {
35+
return new int[] { value.x, value.y };
36+
}
37+
}
38+
39+
static class PointReferenceBean {
40+
@JsonDeserialize(contentConverter=PointDeserConverter.class)
41+
@JsonSerialize(contentConverter=PointSerConverter.class)
42+
public Optional<Point> opt;
43+
44+
protected PointReferenceBean() { }
45+
public PointReferenceBean(int x, int y) {
46+
opt = Optional.of(new Point(x, y));
47+
}
48+
}
49+
50+
/*
51+
/**********************************************************
52+
/* Test methods
53+
/**********************************************************
54+
*/
55+
56+
private final ObjectMapper MAPPER = mapperWithModule();
57+
58+
// [modules-java#294]: Optional + converters, deser
59+
public void testDeserializeOptionalConverting() throws Exception {
60+
PointReferenceBean w = MAPPER.readerFor(PointReferenceBean.class)
61+
.readValue("{\"opt\": [1,2]}");
62+
assertNotNull(w);
63+
assertNotNull(w.opt);
64+
Point p = w.opt.get();
65+
assertNotNull(p);
66+
assertEquals(1, p.x);
67+
assertEquals(2, p.y);
68+
}
69+
70+
// [modules-java#294]: Optional + converters, ser
71+
public void testSerializeOptionalConverting() throws Exception {
72+
assertEquals("{\"opt\":[3,4]}",
73+
MAPPER.writeValueAsString(new PointReferenceBean(3, 4)));
74+
}
75+
}

datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalTest.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,7 @@ public String deserialize(JsonParser p, DeserializationContext ctxt)
118118
}
119119
}
120120

121-
private ObjectMapper MAPPER;
122-
123-
@Override
124-
protected void setUp() throws Exception
125-
{
126-
super.setUp();
127-
MAPPER = mapperWithModule();
128-
}
121+
private final ObjectMapper MAPPER = mapperWithModule();
129122

130123
/*
131124
/**********************************************************

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Modules:
1414
error (`0`->`Jan`,`11`->`Dec`), because it's an enum
1515
(reported by Christoffer H)
1616
(fix contributed Emanuel T)
17+
#294: `Optional` deserialization, serialization ignore `contentConverter`
18+
(reported by @richardsonwk)
1719

1820
2.16.2 (not yet released)
1921

0 commit comments

Comments
 (0)