Skip to content

Commit

Permalink
Cleaning up #40 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 21, 2014
1 parent 1c4b468 commit 0f0e6ce
Showing 1 changed file with 44 additions and 58 deletions.
@@ -1,67 +1,53 @@
package com.fasterxml.jackson.datatype.joda.deser;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;
import java.io.IOException;

import com.fasterxml.jackson.core.*;

import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.node.IntNode;
import com.fasterxml.jackson.databind.node.TextNode;
import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.Minutes;
import org.joda.time.Months;
import org.joda.time.ReadablePeriod;
import org.joda.time.Seconds;
import org.joda.time.Weeks;
import org.joda.time.Years;
import com.fasterxml.jackson.databind.JsonNode;

import java.io.IOException;
import org.joda.time.*;

public class ReadablePeriodDeserializer extends JodaDeserializerBase<ReadablePeriod>
{
public ReadablePeriodDeserializer()
{
super( ReadablePeriod.class );
}
private static final long serialVersionUID = 1L;

public ReadablePeriodDeserializer() {
super(ReadablePeriod.class);
}

@Override
public ReadablePeriod deserialize( JsonParser jsonParser, DeserializationContext deserializationContext ) throws IOException, JsonProcessingException
{
TreeNode treeNode = jsonParser.getCodec().readTree( jsonParser );
String periodType = ((TextNode)treeNode.get( "fieldType" ).get( "name" )).textValue();
String periodName = ((TextNode)treeNode.get( "periodType" ).get( "name" )).textValue();
int periodValue = ((IntNode)treeNode.get( periodType )).intValue();
if (periodName.equals( "Seconds" ))
{
return Seconds.seconds( periodValue );
}
else if (periodName.equals( "Minutes" ))
{
return Minutes.minutes( periodValue );
}
else if (periodName.equals( "Hours" ))
{
return Hours.hours( periodValue );
}
else if (periodName.equals( "Days" ))
{
return Days.days( periodValue );
}
else if (periodName.equals( "Weeks" ))
{
return Weeks.weeks( periodValue );
}
else if (periodName.equals( "Months" ))
{
return Months.months( periodValue );
}
else if (periodName.equals( "Years" ))
{
return Years.years( periodValue );
}
else
{
return null;
}
}
@Override
public ReadablePeriod deserialize(JsonParser jsonParser, DeserializationContext ctxt)
throws IOException
{
JsonNode treeNode = jsonParser.readValueAsTree();
String periodType = treeNode.path("fieldType").path("name").asText();
String periodName = treeNode.path("periodType").path("name").asText();
// any "weird" numbers we should worry about?
int periodValue = treeNode.path(periodType).asInt();
if (periodName.equals( "Seconds" )) {
return Seconds.seconds( periodValue );
}
if (periodName.equals( "Minutes" )) {
return Minutes.minutes( periodValue );
}
if (periodName.equals( "Hours" )) {
return Hours.hours( periodValue );
}
if (periodName.equals( "Days" )) {
return Days.days( periodValue );
}
if (periodName.equals( "Weeks" )) {
return Weeks.weeks( periodValue );
}
if (periodName.equals( "Months" )) {
return Months.months( periodValue );
}
if (periodName.equals( "Years" )) {
return Years.years( periodValue );
}
throw ctxt.mappingException("Don't know how to deserialize ReadablePeriod using periodName '"
+periodName+"'");
}
}

0 comments on commit 0f0e6ce

Please sign in to comment.