Improved support for custom collections in Lift JSON
It is currently not possible to provide a custom serializer for collection types. Adding support for IndexedSeq's for instance does not work now:
class IndexedSeqSerializer extends Serializer[IndexedSeq[_]] {
def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), IndexedSeq[_]] = {
case (TypeInfo(clazz, ptype), json) if classOf[IndexedSeq[_]].isAssignableFrom(clazz) => json match {
case JArray(xs) =>
val t = ptype.getOrElse(throw new MappingException("parameterized type not known"))
xs.map(x => Extraction.extract(x, TypeInfo(t.getActualTypeArguments()(0).asInstanceOf[Class[_]], None))).toIndexedSeq
case x => throw new MappingException("Can't convert " + x + " to IndexedSeq")
}
}
def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
case i: IndexedSeq[_] => JArray(i.map(Extraction.decompose).toList)
}
}
Leave a comment
(In revision:0745393f9828f571a2ddb147325ceab6aa4950d8) Improve custom serializers to enable interception of collections (fixes #1079)
Branch: master
Branch: master