I have a snippet of code like this:
override def getOption[T: TypeTag: ClassTag](path: String): Option[T] = {
implicitly[scala.reflect.ClassTag[T]].toString() match {
case "java.lang.String" => HandleBlank(super.getOption[String](path)).asInstanceOf[Option[T]]
case _ => super.getOption[T](path)
}
}
I assume there must be a better way to do this than matching by string, but tried a few things (like classOf[String]
) and can't seem to find something that works.
For context, getOption (in the superclass that we are trying to override) gets an Option value from a json4s AST JValue object
def getOption[T: TypeTag: ClassTag](path: String): Option[T] = Try(getJValue(path).extract[T]).toOption
def getJValue(path: String): JValue = {
path.split('.').foldLeft(value)((a, b) => a \ b) match {
case JNull => JNothing
case j => j
}
}
The idea is that I want to shim it so that for only String types of that method, we want to handle blank strings in a special way.
You can do direct equality tests between
ClassTag
s like this: