-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c4b468
commit 0f0e6ce
Showing
1 changed file
with
44 additions
and
58 deletions.
There are no files selected for viewing
102 changes: 44 additions & 58 deletions
102
src/main/java/com/fasterxml/jackson/datatype/joda/deser/ReadablePeriodDeserializer.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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+"'"); | ||
} | ||
} |