Skip to content

Commit

Permalink
Merge pull request #41 from bkenned4/master
Browse files Browse the repository at this point in the history
add KeyDeserializers for LocalTime, LocalDate, & LocalDateTime
  • Loading branch information
cowtowncoder committed Aug 27, 2014
2 parents c65be20 + 277a287 commit bdf6c97
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.fasterxml.jackson.datatype.joda;

import org.joda.time.*;

import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.joda.deser.*;
import com.fasterxml.jackson.datatype.joda.deser.key.*;
import com.fasterxml.jackson.datatype.joda.ser.*;
import org.joda.time.*;

public class JodaModule extends SimpleModule
{
Expand All @@ -19,7 +19,7 @@ public JodaModule()
addDeserializer(DateMidnight.class, new DateMidnightDeserializer());
addDeserializer(DateTime.class, DateTimeDeserializer.forType(DateTime.class));
addDeserializer(DateTimeZone.class, new DateTimeZoneDeserializer());

addDeserializer(Duration.class, new DurationDeserializer());
addDeserializer(Instant.class, new InstantDeserializer());
addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
Expand Down Expand Up @@ -49,8 +49,11 @@ public JodaModule()
addSerializer(MonthDay.class, stringSer);
addSerializer(YearMonth.class, stringSer);

// then key deserializers - only one included for DateTime here.
// then key deserializers
addKeyDeserializer(DateTime.class, new DateTimeKeyDeserializer());
addKeyDeserializer(LocalTime.class, new LocalTimeKeyDeserializer());
addKeyDeserializer(LocalDate.class, new LocalDateKeyDeserializer());
addKeyDeserializer(LocalDateTime.class, new LocalDateTimeKeyDeserializer());
}

@Override
Expand Down

This file was deleted.

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

import java.io.IOException;

import org.joda.time.*;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;

public class DateTimeKeyDeserializer extends JodaKeyDeserializer {

@Override
protected DateTime deserialize(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException{
return new DateTime(key, DateTimeZone.forTimeZone(ctxt.getTimeZone()));
}

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.KeyDeserializer;

import java.io.IOException;

abstract class JodaKeyDeserializer extends KeyDeserializer {
private static final long serialVersionUID = 1L;

@Override
public final Object deserializeKey(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException {
if (key.length() == 0) { // [JACKSON-360]
return null;
}
return deserialize(key, ctxt);
}

protected abstract Object deserialize(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.fasterxml.jackson.datatype.joda.deser.key;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

import java.io.IOException;

public class LocalDateKeyDeserializer extends JodaKeyDeserializer {
private static final DateTimeFormatter parser = ISODateTimeFormat.localDateParser();

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

import java.io.IOException;

public class LocalDateTimeKeyDeserializer extends JodaKeyDeserializer {
private static final DateTimeFormatter parser = ISODateTimeFormat.localDateOptionalTimeParser();

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import org.joda.time.LocalTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

import java.io.IOException;

public class LocalTimeKeyDeserializer extends JodaKeyDeserializer {
private static final DateTimeFormatter parser = ISODateTimeFormat.localTimeParser();

@Override
protected LocalTime deserialize(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return parser.parseLocalTime(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,30 @@ public void testDateTimeKeyDeserialize() throws IOException {
assertTrue(map.containsKey(DateTime.parse("1970-01-01T00:00:00.000Z")));
}

public void testLocalDateKeyDeserialize() throws IOException {

final String json = "{" + quote("2014-05-23") + ":0}";
final Map<LocalDate, Long> map = MAPPER.readValue(json, new TypeReference<Map<LocalDate, String>>() { });

assertNotNull(map);
assertTrue(map.containsKey(LocalDate.parse("2014-05-23")));
}

public void testLocalTimeKeyDeserialize() throws IOException {

final String json = "{" + quote("00:00:00.000") + ":0}";
final Map<LocalTime, Long> map = MAPPER.readValue(json, new TypeReference<Map<LocalTime, String>>() { });
assertNotNull(map);
assertTrue(map.containsKey(LocalTime.parse("00:00:00.000")));
}
public void testLocalDateTimeKeyDeserialize() throws IOException {

final String json = "{" + quote("2014-05-23T00:00:00.000") + ":0}";
final Map<LocalDateTime, Long> map = MAPPER.readValue(json, new TypeReference<Map<LocalDateTime, String>>() { });
assertNotNull(map);
assertTrue(map.containsKey(LocalDateTime.parse("2014-05-23T00:00:00.000")));
}

public void testDeserMonthDay() throws Exception
{
String monthDayString = new MonthDay(7, 23).toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package com.fasterxml.jackson.datatype.joda;

import java.io.*;

import com.fasterxml.jackson.databind.ObjectMapper;
import junit.framework.TestCase;

import static org.junit.Assert.*;
import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;
import static org.junit.Assert.*;

public abstract class JodaTestBase extends TestCase
{
protected static ObjectMapper jodaMapper()
{
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
return mapper;
return new JodaMapper();
}

/*
Expand Down

0 comments on commit bdf6c97

Please sign in to comment.