Created
November 20, 2012 11:02
-
-
Save dmorgantini/4117302 to your computer and use it in GitHub Desktop.
Joda Datetime with dropwizard (0.6.0-SNAPSHOT)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DateTimeDeserializer extends JsonDeserializer<DateTime> { | |
@Override | |
public DateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { | |
if (jp.getCurrentToken() == JsonToken.VALUE_NUMBER_INT) { | |
return new DateTime(jp.getLongValue()); | |
} | |
throw ctxt.mappingException("Expected JSON Number"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DateTimeSerializer extends JsonSerializer<DateTime> { | |
@Override | |
public void serialize(DateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { | |
jgen.writeNumber(value.getMillis()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JodaDateTimeModule extends SimpleModule { | |
public JodaDateTimeModule() { | |
addDeserializer(DateTime.class, new DateTimeDeserializer()); | |
addSerializer(DateTime.class, new DateTimeSerializer()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void initialize(Bootstrap<RouterConfiguration> bootstrap) { | |
// ... | |
bootstrap.getObjectMapperFactory().registerModule(new JodaDateTimeModule()); | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment