Skip to content

Commit 74be8ab

Browse files
committed
Merge branch '2.9' into 2.10
2 parents 0b1757d + 05d8780 commit 74be8ab

File tree

7 files changed

+112
-23
lines changed

7 files changed

+112
-23
lines changed

datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/YearDeserializer.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.fasterxml.jackson.core.JsonParser;
2020
import com.fasterxml.jackson.core.JsonToken;
2121
import com.fasterxml.jackson.databind.DeserializationContext;
22+
import com.fasterxml.jackson.databind.JsonDeserializer;
2223

2324
import java.io.IOException;
2425
import java.time.DateTimeException;
@@ -31,22 +32,24 @@
3132
* @author Nick Williams
3233
* @since 2.2
3334
*/
34-
public class YearDeserializer extends JSR310DeserializerBase<Year>
35+
public class YearDeserializer extends JSR310DateTimeDeserializerBase<Year>
3536
{
3637
private static final long serialVersionUID = 1L;
3738

3839
public static final YearDeserializer INSTANCE = new YearDeserializer();
3940

40-
private final DateTimeFormatter _formatter;
41-
4241
private YearDeserializer()
4342
{
4443
this(null);
4544
}
4645

4746
public YearDeserializer(DateTimeFormatter formatter) {
48-
super(Year.class);
49-
_formatter = formatter;
47+
super(Year.class, formatter);
48+
}
49+
50+
@Override
51+
protected JsonDeserializer<Year> withDateFormat(DateTimeFormatter dtf) {
52+
return new YearDeserializer(dtf);
5053
}
5154

5255
@Override

datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalDateTimeDeserialization.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.core.JsonParser;
44
import com.fasterxml.jackson.core.JsonProcessingException;
55
import com.fasterxml.jackson.core.JsonToken;
6+
67
import com.fasterxml.jackson.databind.DeserializationContext;
78
import com.fasterxml.jackson.databind.DeserializationFeature;
89
import com.fasterxml.jackson.databind.ObjectMapper;

datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/YearDeserTest.java

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.time.Year;
2020
import java.time.temporal.Temporal;
2121

22+
import com.fasterxml.jackson.annotation.JsonFormat;
2223
import com.fasterxml.jackson.databind.ObjectMapper;
2324
import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration;
2425
import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase;
@@ -30,29 +31,57 @@
3031

3132
public class YearDeserTest extends ModuleTestBase
3233
{
34+
static class FormattedYear {
35+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "'Y'yyyy")
36+
public Year value;
37+
38+
protected FormattedYear() {}
39+
public FormattedYear(Year year) {
40+
value = year;
41+
}
42+
}
43+
3344
private final ObjectMapper MAPPER = newMapper();
3445

3546
@Test
36-
public void testDeserialization01() throws Exception
47+
public void testDefaultDeserialization() throws Exception
3748
{
3849
Year value = MAPPER.readValue("1986", Year.class);
3950
assertEquals("The value is not correct.", Year.of(1986), value);
40-
}
41-
42-
@Test
43-
public void testDeserialization02() throws Exception
44-
{
45-
Year value = MAPPER.readValue("2013", Year.class);
51+
value = MAPPER.readValue("2013", Year.class);
4652
assertEquals("The value is not correct.", Year.of(2013), value);
4753
}
4854

4955
@Test
50-
public void testDeserializationWithTypeInfo01() throws Exception
56+
public void testDeserializationWithTypeInfo() throws Exception
5157
{
5258
ObjectMapper mapper = newMapper()
5359
.addMixIn(Temporal.class, MockObjectConfiguration.class);
5460
Temporal value = mapper.readValue("[\"" + Year.class.getName() + "\",2005]", Temporal.class);
5561
assertTrue("The value should be a Year.", value instanceof Year);
5662
assertEquals("The value is not correct.", Year.of(2005), value);
5763
}
64+
65+
@Test
66+
public void testWithCustomFormat() throws Exception
67+
{
68+
FormattedYear input = new FormattedYear(Year.of(2018));
69+
String json = MAPPER.writeValueAsString(input);
70+
assertEquals("{\"value\":\"Y2018\"}", json);
71+
FormattedYear result = MAPPER.readValue(json, FormattedYear.class);
72+
assertEquals(input.value, result.value);
73+
}
74+
75+
@Test
76+
public void testWithFormatViaConfigOverride() throws Exception
77+
{
78+
ObjectMapper mapper = newMapper();
79+
mapper.configOverride(Year.class)
80+
.setFormat(JsonFormat.Value.forPattern("'X'yyyy"));
81+
Year input = Year.of(2018);
82+
String json = mapper.writeValueAsString(input);
83+
assertEquals("\"X2018\"", json);
84+
Year result = mapper.readValue(json, Year.class);
85+
assertEquals(input, result);
86+
}
5887
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2013 FasterXML.com
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
* not use this file except in compliance with the License. You may obtain
6+
* a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the license for the specific language governing permissions and
14+
* limitations under the license.
15+
*/
16+
17+
package com.fasterxml.jackson.datatype.jsr310.failing;
18+
19+
import java.time.Year;
20+
21+
import com.fasterxml.jackson.annotation.JsonFormat;
22+
import com.fasterxml.jackson.databind.ObjectMapper;
23+
import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase;
24+
25+
import org.junit.Test;
26+
27+
import static org.junit.Assert.assertEquals;
28+
29+
public class YearDeser78Test extends ModuleTestBase
30+
{
31+
// [module-java8#78]
32+
final static class ObjectTest {
33+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "'Y'yyyy")
34+
public Year value;
35+
36+
protected ObjectTest() { }
37+
public ObjectTest(Year y) {
38+
value = y;
39+
}
40+
}
41+
42+
private final ObjectMapper MAPPER = newMapper();
43+
44+
// [module-java8#78]
45+
@Test
46+
public void testWithCustomFormat() throws Exception
47+
{
48+
ObjectTest input = new ObjectTest(Year.of(2018));
49+
String json = MAPPER.writeValueAsString(input);
50+
assertEquals("{\"value\":\"Y2018\"}", json);
51+
ObjectTest result = MAPPER.readValue(json, ObjectTest.class);
52+
assertEquals(input, result);
53+
}
54+
}

datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/YearSerTest.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,16 @@ public class YearSerTest extends ModuleTestBase
3232
private final ObjectMapper MAPPER = newMapper();
3333

3434
@Test
35-
public void testSerialization01() throws Exception
36-
{
37-
Year year = Year.of(1986);
38-
String value = MAPPER.writeValueAsString(year);
39-
assertEquals("The value is not correct.", "1986", value);
40-
}
41-
42-
@Test
43-
public void testSerialization02() throws Exception
35+
public void testDefaultSerialization() throws Exception
4436
{
37+
assertEquals("The value is not correct.", "1986",
38+
MAPPER.writeValueAsString(Year.of(1986)));
4539
assertEquals("The value is not correct.", "2013",
4640
MAPPER.writeValueAsString(Year.of(2013)));
4741
}
4842

4943
@Test
50-
public void testSerializationWithTypeInfo01() throws Exception
44+
public void testSerializationWithTypeInfo() throws Exception
5145
{
5246
ObjectMapper mapper = newMapper()
5347
.addMixIn(Temporal.class, MockObjectConfiguration.class);

release-notes/CREDITS-2.x

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,8 @@ Kezhu Wang (kezhuw@github)
4141
#75: Use `SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS` for configuring
4242
`Duration` serialization
4343
(2.10.0)
44+
45+
Adrian Palanques (devdevx@github)
46+
#78: Year deserialization ignores `@JsonFormat` pattern
47+
(2.9.7)
48+

release-notes/VERSION-2.x

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Modules:
1616

1717
2.9.7 (not yet released)
1818

19+
#78: Year deserialization ignores `@JsonFormat` pattern
20+
(reported, fixed by Adrian P)
21+
1922
2.9.6 (12-Jun-2018)
2023

2124
#65: Use `DeserializationContext.handleWeirdXxxValue()` for datetime deserializers

0 commit comments

Comments
 (0)