diff --git a/pom.xml b/pom.xml index 60ea8c5..bf1d2c1 100644 --- a/pom.xml +++ b/pom.xml @@ -30,7 +30,7 @@ jar - Provides support for serializing and deserializing Joda objects as ISO strings using Jackson. + Provides support for serializing and deserializing Joda objects as ISO strings using Jackson. @@ -44,17 +44,31 @@ com.fasterxml.jackson.core jackson-core - 2.2.2 + 2.10.4 com.fasterxml.jackson.core jackson-databind - 2.2.2 + 2.10.4 joda-time joda-time - 2.2 + 2.10.4 + + + org.hamcrest + hamcrest-core + + + org.mockito + mockito-core + compile + + + junit + junit + compile \ No newline at end of file diff --git a/src/main/java/com/allogy/json/jackson/joda/ISODateTimeDeserializer.java b/src/main/java/com/allogy/json/jackson/joda/ISODateTimeDeserializer.java index 0fcd051..f842dd9 100644 --- a/src/main/java/com/allogy/json/jackson/joda/ISODateTimeDeserializer.java +++ b/src/main/java/com/allogy/json/jackson/joda/ISODateTimeDeserializer.java @@ -22,6 +22,7 @@ import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.exc.InvalidFormatException; import org.joda.time.DateTime; +import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.ISODateTimeFormat; @@ -34,7 +35,8 @@ */ public class ISODateTimeDeserializer extends JsonDeserializer { - private final DateTimeFormatter dateTimeFormatter; + private DateTimeFormatter dateTimeFormatter; + private static final String WITHOUTMILLISFORMAT = "yyyy-MM-dd'T'HH:mm:ssZ"; public ISODateTimeDeserializer() { @@ -44,14 +46,20 @@ public ISODateTimeDeserializer() @Override public DateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { + String text = jsonParser.getText(); try { - return dateTimeFormatter.parseDateTime(text); + return dateTimeFormatter.parseDateTime(text); } catch (Throwable throwable) { - throw new InvalidFormatException(throwable.getMessage(), text, String.class); + try{ + dateTimeFormatter = DateTimeFormat.forPattern(WITHOUTMILLISFORMAT).withOffsetParsed(); + return dateTimeFormatter.parseDateTime(text); + }catch(Throwable throwable2){ + throw new InvalidFormatException(throwable2.getMessage(), text, String.class); + } } } } diff --git a/src/main/java/test/com/allogy/json/jackson/joda/ISODateTimeDeserializerTest.java b/src/main/java/test/com/allogy/json/jackson/joda/ISODateTimeDeserializerTest.java new file mode 100644 index 0000000..73423b2 --- /dev/null +++ b/src/main/java/test/com/allogy/json/jackson/joda/ISODateTimeDeserializerTest.java @@ -0,0 +1,71 @@ +package test.com.allogy.json.jackson.joda; + +import com.allogy.json.jackson.joda.ISODateTimeDeserializer; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import org.joda.time.DateTime; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * @author Payal Pandey + * @since Jun 18, 2020 + */ +@RunWith(Parameterized.class) +public class ISODateTimeDeserializerTest +{ + + private JsonParser jsonParser; + private DeserializationContext deserializationContext; + + @Parameterized.Parameter() + public String datePattern; + + @Parameterized.Parameter(1) + public String inputDate; + + @Parameterized.Parameter(2) + public String expected; + + @Before + public void setUp() + { + jsonParser = mock(JsonParser.class); + deserializationContext = mock(DeserializationContext.class); + } + + @Parameterized.Parameters(name = "{index}: Test with datePattern ={0}, inputDate ={1}, expected ={2}") + public static Collection data() + { + Object[][] data = new Object[][]{ + {"yyyy-MM-dd'T'HH:mm:ss.SSSZ", "2001-07-04T12:08:56.235-07:00", "2001-07-04T12:08:56.235-07:00"}, + {"yyyy-MM-dd'T'HH:mm:ssZ", "2001-07-04T12:08:56-07:00", "2001-07-04T12:08:56.000-07:00"}}; + return Arrays.asList(data); + } + + public ISODateTimeDeserializer createObjectUnderTest() + { + return new ISODateTimeDeserializer(); + } + + @Test + public void test_deserialize() throws IOException + { + + when(jsonParser.getText()).thenReturn(inputDate); + + DateTime result = createObjectUnderTest().deserialize(jsonParser, deserializationContext); + assertThat(result.toString(), equalTo(expected)); + } +} \ No newline at end of file