I have a small Scala / Neo4j app that connects people and topics through an expertAt and interest relationship. It has REST / Json Api (using Scalatra), and I ran into a typical type-erase problem when I wanted to add the asJson method for List [Person] and List [Topic]. I would like to implement different Json serialization behavior for different types of content, but of course the types are erased. The best thing I've been able to come up with so far is the following runtime trick:
implicit def topicsOrPeopleAsJson[T](list: List[T]) = new {
def asJson: String = {
list match {
case head :: tail if (head.isInstanceOf[Topic]) => topicsToJson(list.asInstanceOf[List[Topic]])
case head :: tail if (head.isInstanceOf[Person]) => peopleToJson(list.asInstanceOf[List[Person]])
case _ => "[]"
}
}
private def peopleToJson(people: List[Person]) = {
...
}
private def topicsToJson(topics: List[Topic]) = {
...
}
}
This works fine, but I was wondering if there is a better solution, perhaps something, including type classes, a topic that I am not very familiar with (yet).