Last active
November 17, 2016 00:02
-
-
Save francisdb/4509934 to your computer and use it in GitHub Desktop.
Scala Jackson json deserializer for Microsoft json date format to joda DateTime
This file contains hidden or 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
import com.fasterxml.jackson.databind.{DeserializationContext, JsonDeserializer} | |
import org.joda.time.{DateTimeZone, DateTime} | |
import com.fasterxml.jackson.core.JsonParser | |
import java.io.IOException | |
class MicrosoftJsonDateDeserializer extends JsonDeserializer[DateTime] { | |
val Pattern = """\/Date\((\d+)([-+]\d+)?\)\/""".r | |
def deserialize(jsonParser: JsonParser, deserializationContext: DeserializationContext): DateTime = { | |
val dateString = jsonParser.getText | |
dateString match { | |
case Pattern(millis, null) => new DateTime(millis.toLong, DateTimeZone.forOffsetHours(0)) | |
case Pattern(millis, tz) => new DateTime(millis.toLong, DateTimeZone.forID(tz)) | |
case null => null | |
case _ => throw new IOException(s"wrong date time format: $dateString") | |
} | |
} | |
} |
This file contains hidden or 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
import org.specs2.mutable._ | |
import org.specs2.mock.Mockito | |
import com.fasterxml.jackson.databind.DeserializationContext | |
import com.fasterxml.jackson.core._ | |
import org.joda.time.{DateTimeZone, DateTime} | |
import java.io.IOException | |
class MicrosoftJsonDateDeserializerTest extends Specification with Mockito{ | |
"deserializing Some(/Date(1357232623030+0500)/)" should { | |
"return the correct date" in { | |
val deserializer = new MicrosoftJsonDateDeserializer | |
val parser = mock[JsonParser] | |
parser.getText returns "/Date(1357232623030+0500)/" | |
val ctx = mock[DeserializationContext] | |
val result = deserializer.deserialize(parser, ctx) | |
result should beEqualTo(new DateTime(1357232623030L, DateTimeZone.forOffsetHours(5))) | |
} | |
} | |
"deserializing Some(/Date(1357232623030)/)" should { | |
"return the correct date" in { | |
val deserializer = new MicrosoftJsonDateDeserializer | |
val parser = mock[JsonParser] | |
parser.getText returns "/Date(1357232623030)/" | |
val ctx = mock[DeserializationContext] | |
val result = deserializer.deserialize(parser, ctx) | |
result should beEqualTo(new DateTime(1357232623030L, DateTimeZone.forOffsetHours(0))) | |
} | |
} | |
"deserializing null" should { | |
"return null" in { | |
val deserializer = new MicrosoftJsonDateDeserializer | |
val parser = mock[JsonParser] | |
val ctx = mock[DeserializationContext] | |
val result = deserializer.deserialize(parser, ctx) | |
result should beNull | |
} | |
} | |
"deserializing 'invaliddate'" should { | |
"throw an exception" in { | |
val deserializer = new MicrosoftJsonDateDeserializer | |
val parser = mock[JsonParser] | |
parser.getText returns "invaliddate" | |
val ctx = mock[DeserializationContext] | |
deserializer.deserialize(parser, ctx) must throwA[IOException] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment