com.fasterxml.jackson.module.scala.ser.ScalaMapSerializer.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jackson-module-scala Show documentation
Show all versions of jackson-module-scala Show documentation
Add-on module for Jackson (http://jackson.codehaus.org) to support
Scala (http://www.scala-lang.org/) data types.
package com.fasterxml.jackson.module.scala.ser
import org.codehaus.jackson.map._
import org.codehaus.jackson._
import ser.ContainerSerializerBase
import collection.Map
/**
* The implementation is taken from the code written by Greg Zoller, found here:
* http://jira.codehaus.org/browse/JACKSON-211
*/
class ScalaMapSerializer extends ContainerSerializerBase[Map[String,Any]](classOf[Map[String,Any]]) {
override def serialize(value:Map[String,Any], jgen:JsonGenerator, provider:SerializerProvider) {
jgen.writeStartObject();
if( value.size > 0 ) {
val keySerializer = provider.getKeySerializer
value.foreach( mapElem => {
val (k,v) = mapElem
keySerializer.serialize(k, jgen, provider)
provider.findValueSerializer(v.asInstanceOf[AnyRef].getClass).serialize(v.asInstanceOf[AnyRef], jgen, provider)
})
}
jgen.writeEndObject();
}
override def _withValueTypeSerializer(vts:TypeSerializer) = new ScalaIterableSerializer()
override def getSchema(provider:SerializerProvider, typeHint:java.lang.reflect.Type) = createSchemaNode("string", true)
}