Skip to content

Instantly share code, notes, and snippets.

@meiwin
Created November 12, 2012 07:26
Show Gist options
  • Save meiwin/4057991 to your computer and use it in GitHub Desktop.
Save meiwin/4057991 to your computer and use it in GitHub Desktop.
fasterxml/jerkson custom json serializers
import java.sql.{Timestamp, Date}
import org.codehaus.jackson.map._
import org.codehaus.jackson.{Version, JsonGenerator, JsonParser}
import java.util.TimeZone
import java.text.SimpleDateFormat
import com.codahale.jerkson.Json
import org.codehaus.jackson.map.module.SimpleModule
import org.codehaus.jackson.map.annotate.JsonSerialize
object CustomJson extends Json {
val module = new SimpleModule("CustomSerializer", Version.unknownVersion())
// --- (SERIALIZERS) ---
module.addSerializer(classOf[Enumeration#Value], EnumerationSerializer)
module.addSerializer(classOf[java.util.Date], DateSerializer)
// --- (DESERIALIZERS) ---
module.addDeserializer(classOf[UserType], new EnumerationDeserializer[UserType](UserTypes))
module.addDeserializer(classOf[MediaType], new EnumerationDeserializer[MediaType](MediaTypes))
module.addDeserializer(classOf[java.sql.Timestamp], new DateDeserializer[java.sql.Timestamp](classOf[java.sql.Timestamp]))
module.addDeserializer(classOf[java.sql.Date], new DateDeserializer[java.sql.Date](classOf[java.sql.Date]))
module.addDeserializer(classOf[java.util.Date], new DateDeserializer[java.util.Date](classOf[java.util.Date]))
mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL)
mapper.setSerializationConfig(mapper.getSerializationConfig.without(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES))
mapper.registerModule(module)
def write(obj: Object) = generate(obj)
}
object EnumerationSerializer extends JsonSerializer[Enumeration#Value] {
override def serialize(id: Enumeration#Value, json: JsonGenerator, provider: SerializerProvider) {
json.writeString(id.toString)
}
}
class EnumerationDeserializer[T <: Enumeration#Value](enum: Enumeration) extends JsonDeserializer[T] {
override def deserialize(jp: JsonParser, context: DeserializationContext): T = {
return fetchEnumValue(jp.getText)
}
private def fetchEnumValue(value: String): T = {
enum.values.find(_.toString == value).getOrElse(throw new Exception("Invalid enum value => " + value)).asInstanceOf[T]
}
}
object DateSerializer extends JsonSerializer[java.util.Date] {
val DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z")
DATE_FORMATTER.setTimeZone(TimeZone.getTimeZone("UTC"))
override def serialize(value: java.util.Date, json: JsonGenerator, provider: SerializerProvider) {
json.writeString(DATE_FORMATTER.format(value))
}
}
class DateDeserializer[T <: java.util.Date](klass: Class[T]) extends JsonDeserializer[T] {
override def deserialize(jp: JsonParser, context: DeserializationContext): T = {
toDate(DateSerializer.DATE_FORMATTER.parse(jp.getText), klass).asInstanceOf[T]
}
def toDate(d: java.util.Date, klass: Class[_]) = {
klass match {
case t: java.sql.Timestamp => new Timestamp(d.getTime)
case t: java.sql.Date => new Date(d.getTime)
case t: java.util.Date => d
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment