-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
d8d0f5c
commit b4b0968
Showing
3 changed files
with
78 additions
and
8 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.fasterxml.jackson.datatype.jdk8; | ||
|
||
import java.util.Optional; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import com.fasterxml.jackson.databind.util.StdConverter; | ||
|
||
// [modules-java#294]: Optional + converters | ||
public class OptionalConverterTest extends ModuleTestBase | ||
{ | ||
static class Point | ||
{ | ||
public int x, y; | ||
|
||
protected Point() { } | ||
public Point(int v1, int v2) { | ||
x = v1; | ||
y = v2; | ||
} | ||
} | ||
|
||
static class PointDeserConverter extends StdConverter<int[], Point> | ||
{ | ||
@Override | ||
public Point convert(int[] value) { | ||
return new Point(value[0], value[1]); | ||
} | ||
} | ||
|
||
static class PointSerConverter extends StdConverter<Point, int[]> | ||
{ | ||
@Override public int[] convert(Point value) { | ||
return new int[] { value.x, value.y }; | ||
} | ||
} | ||
|
||
static class PointReferenceBean { | ||
@JsonDeserialize(contentConverter=PointDeserConverter.class) | ||
@JsonSerialize(contentConverter=PointSerConverter.class) | ||
public Optional<Point> opt; | ||
|
||
protected PointReferenceBean() { } | ||
public PointReferenceBean(int x, int y) { | ||
opt = Optional.of(new Point(x, y)); | ||
} | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* Test methods | ||
/********************************************************** | ||
*/ | ||
|
||
private final ObjectMapper MAPPER = mapperWithModule(); | ||
|
||
// [modules-java#294]: Optional + converters, deser | ||
public void testDeserializeOptionalConverting() throws Exception { | ||
PointReferenceBean w = MAPPER.readerFor(PointReferenceBean.class) | ||
.readValue("{\"opt\": [1,2]}"); | ||
assertNotNull(w); | ||
assertNotNull(w.opt); | ||
Point p = w.opt.get(); | ||
assertNotNull(p); | ||
assertEquals(1, p.x); | ||
assertEquals(2, p.y); | ||
} | ||
|
||
// [modules-java#294]: Optional + converters, ser | ||
public void testSerializeOptionalConverting() throws Exception { | ||
assertEquals("{\"opt\":[3,4]}", | ||
MAPPER.writeValueAsString(new PointReferenceBean(3, 4))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters