Skip to content

Commit

Permalink
Fix #294 (via databind), add tests to show fix works (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 24, 2024
1 parent d8d0f5c commit b4b0968
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 8 deletions.
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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,7 @@ public String deserialize(JsonParser p, DeserializationContext ctxt)
}
}

private ObjectMapper MAPPER;

@Override
protected void setUp() throws Exception
{
super.setUp();
MAPPER = mapperWithModule();
}
private final ObjectMapper MAPPER = mapperWithModule();

/*
/**********************************************************
Expand Down
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Modules:
error (`0`->`Jan`,`11`->`Dec`), because it's an enum
(reported by Christoffer H)
(fix contributed Emanuel T)
#294: `Optional` deserialization, serialization ignore `contentConverter`
(reported by @richardsonwk)

2.16.2 (not yet released)

Expand Down

0 comments on commit b4b0968

Please sign in to comment.