Skip to content

Add key deserializers for Duration and Period classes #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public JodaModule()
addKeyDeserializer(LocalTime.class, new LocalTimeKeyDeserializer());
addKeyDeserializer(LocalDate.class, new LocalDateKeyDeserializer());
addKeyDeserializer(LocalDateTime.class, new LocalDateTimeKeyDeserializer());
addKeyDeserializer(Duration.class, new DurationKeyDeserializer());
addKeyDeserializer(Period.class, new PeriodKeyDeserializer());

// 26-Dec-2015, tatu: Joda has deprecated following types:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.fasterxml.jackson.datatype.joda.deser.key;

import com.fasterxml.jackson.databind.DeserializationContext;
import org.joda.time.Duration;

import java.io.IOException;

public class DurationKeyDeserializer extends JodaKeyDeserializer {
private static final long serialVersionUID = 1L;

@Override
protected Duration deserialize(String key, DeserializationContext ctxt) throws IOException {
return Duration.parse(key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.fasterxml.jackson.datatype.joda.deser.key;

import com.fasterxml.jackson.databind.DeserializationContext;
import org.joda.time.Period;

import java.io.IOException;

public class PeriodKeyDeserializer extends JodaKeyDeserializer {
private static final long serialVersionUID = 1L;

@Override
protected Period deserialize(String key, DeserializationContext ctxt) throws IOException {
return Period.parse(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,20 @@ public void testLocalDateTimeKeyDeserialize() throws IOException {
assertNotNull(map);
assertTrue(map.containsKey(LocalDateTime.parse("2014-05-23T00:00:00.000")));
}
public void testDurationKeyDeserialize() throws IOException {

final String json = "{" + quote("PT60s") + ":0}";
final Map<Duration, Long> map = MAPPER.readValue(json, new TypeReference<Map<Duration, String>>() { });
assertNotNull(map);
assertTrue(map.containsKey(Duration.standardMinutes(1L)));
}
public void testPeriodKeyDeserialize() throws IOException {

final String json = "{" + quote("PT1H2M3.004S") + ":0}";
final Map<Period, Long> map = MAPPER.readValue(json, new TypeReference<Map<Period, String>>() { });
assertNotNull(map);
assertTrue(map.containsKey(new Period(1, 2, 3, 4)));
}

public void testDeserMonthDay() throws Exception
{
Expand Down