Skip to content

Commit 8ce41bb

Browse files
committed
Add serializers and deserializers for MonthDay and YearMonth.
1 parent d39e595 commit 8ce41bb

5 files changed

Lines changed: 155 additions & 0 deletions

File tree

src/main/java/com/fasterxml/jackson/datatype/joda/JodaModule.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public JodaModule()
2727
addDeserializer(ReadableDateTime.class, DateTimeDeserializer.forType(ReadableDateTime.class));
2828
addDeserializer(ReadableInstant.class, DateTimeDeserializer.forType(ReadableInstant.class));
2929
addDeserializer(Interval.class, new IntervalDeserializer());
30+
addDeserializer(MonthDay.class, new MonthDayDeserializer());
31+
addDeserializer(YearMonth.class, new YearMonthDeserializer());
3032

3133
// then serializers:
3234
addSerializer(DateMidnight.class, new DateMidnightSerializer());
@@ -38,5 +40,7 @@ public JodaModule()
3840
addSerializer(LocalTime.class, new LocalTimeSerializer());
3941
addSerializer(Period.class, ToStringSerializer.instance);
4042
addSerializer(Interval.class, new IntervalSerializer());
43+
addSerializer(MonthDay.class, ToStringSerializer.instance);
44+
addSerializer(YearMonth.class, ToStringSerializer.instance);
4145
}
4246
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fasterxml.jackson.datatype.joda.deser;
2+
3+
import java.io.IOException;
4+
5+
import org.joda.time.MonthDay;
6+
7+
import com.fasterxml.jackson.core.JsonParser;
8+
import com.fasterxml.jackson.core.JsonToken;
9+
import com.fasterxml.jackson.databind.DeserializationContext;
10+
11+
/**
12+
* A Jackson deserializer for Joda MonthDay objects.
13+
* <p>
14+
* Expects a string value compatible with MonthDay's parse operation.
15+
*/
16+
public class MonthDayDeserializer extends JodaDeserializerBase<MonthDay>
17+
{
18+
19+
private static final long serialVersionUID = -2360834248497553111L;
20+
21+
public MonthDayDeserializer()
22+
{
23+
super(MonthDay.class);
24+
}
25+
26+
@Override
27+
public MonthDay deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException
28+
{
29+
JsonToken t = jp.getCurrentToken();
30+
if (t == JsonToken.VALUE_STRING)
31+
{
32+
String str = jp.getText().trim();
33+
if (str.isEmpty())
34+
{
35+
return null;
36+
}
37+
return MonthDay.parse(str);
38+
}
39+
throw ctxt.wrongTokenException(jp, JsonToken.VALUE_STRING, "expected JSON String");
40+
}
41+
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fasterxml.jackson.datatype.joda.deser;
2+
3+
import java.io.IOException;
4+
5+
import org.joda.time.YearMonth;
6+
7+
import com.fasterxml.jackson.core.JsonParser;
8+
import com.fasterxml.jackson.core.JsonToken;
9+
import com.fasterxml.jackson.databind.DeserializationContext;
10+
11+
/**
12+
* A Jackson deserializer for Joda YearMonth objects.
13+
* <p>
14+
* Expects a string value compatible with YearMonth's parse operation.
15+
*/
16+
public class YearMonthDeserializer extends JodaDeserializerBase<YearMonth>
17+
{
18+
19+
private static final long serialVersionUID = -3830851040664795250L;
20+
21+
public YearMonthDeserializer()
22+
{
23+
super(YearMonth.class);
24+
}
25+
26+
@Override
27+
public YearMonth deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException
28+
{
29+
JsonToken t = jp.getCurrentToken();
30+
if (t == JsonToken.VALUE_STRING)
31+
{
32+
String str = jp.getText().trim();
33+
if (str.isEmpty())
34+
{
35+
return null;
36+
}
37+
return YearMonth.parse(str);
38+
}
39+
throw ctxt.wrongTokenException(jp, JsonToken.VALUE_STRING, "expected JSON String");
40+
}
41+
42+
}

src/test/java/com/fasterxml/jackson/datatype/joda/JodaDeserializationTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,4 +386,54 @@ public void testDeserInstant() throws IOException
386386
assertNull(MAPPER.readValue(quote(""), Instant.class));
387387
}
388388

389+
public void testDeserMonthDay() throws Exception
390+
{
391+
String monthDayString = new MonthDay(7, 23).toString();
392+
MonthDay monthDay = MAPPER.readValue(quote(monthDayString), MonthDay.class);
393+
assertEquals(new MonthDay(7, 23), monthDay);
394+
}
395+
396+
public void testDeserMonthDayFromEmptyString() throws Exception
397+
{
398+
MonthDay monthDay = MAPPER.readValue(quote(""), MonthDay.class);
399+
assertNull(monthDay);
400+
}
401+
402+
public void testDeserMonthDayFailsForUnexpectedType() throws IOException
403+
{
404+
try
405+
{
406+
MAPPER.readValue("{\"month\":8}", MonthDay.class);
407+
fail();
408+
} catch (JsonMappingException e)
409+
{
410+
assertTrue(e.getMessage().contains("expected JSON String"));
411+
}
412+
}
413+
414+
public void testDeserYearMonth() throws Exception
415+
{
416+
String yearMonthString = new YearMonth(2013, 8).toString();
417+
YearMonth yearMonth = MAPPER.readValue(quote(yearMonthString), YearMonth.class);
418+
assertEquals(new YearMonth(2013, 8), yearMonth);
419+
}
420+
421+
public void testDeserYearMonthFromEmptyString() throws Exception
422+
{
423+
YearMonth yearMonth = MAPPER.readValue(quote(""), YearMonth.class);
424+
assertNull(yearMonth);
425+
}
426+
427+
public void testDeserYearMonthFailsForUnexpectedType() throws IOException
428+
{
429+
try
430+
{
431+
MAPPER.readValue("{\"year\":2013}", YearMonth.class);
432+
fail();
433+
} catch (JsonMappingException e)
434+
{
435+
assertTrue(e.getMessage().contains("expected JSON String"));
436+
}
437+
}
438+
389439
}

src/test/java/com/fasterxml/jackson/datatype/joda/JodaSerializationTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,21 @@ public void testInstantSer() throws IOException {
239239
m.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
240240
assertEquals(quote("1970-01-01T00:00:00.000Z"), m.writeValueAsString(instant));
241241
}
242+
243+
public void testMonthDaySer() throws Exception
244+
{
245+
MonthDay monthDay = new MonthDay(7, 23);
246+
ObjectMapper mapper = jodaMapper();
247+
String json = mapper.writeValueAsString(monthDay);
248+
assertEquals(quote("--07-23"), json);
249+
}
250+
251+
public void testYearMonthSer() throws Exception
252+
{
253+
YearMonth yearMonth = new YearMonth(2013, 8);
254+
ObjectMapper mapper = jodaMapper();
255+
String json = mapper.writeValueAsString(yearMonth);
256+
assertEquals(quote("2013-08"), json);
257+
}
258+
242259
}

0 commit comments

Comments
 (0)