本文整理汇总了Scala中scala.reflect.Manifest类的典型用法代码示例。如果您正苦于以下问题:Scala Manifest类的具体用法?Scala Manifest怎么用?Scala Manifest使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Manifest类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: JsonUtil
//设置package包名称以及导入依赖的类
package util
import java.io.StringWriter
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import scala.reflect.{ClassTag, Manifest}
object JsonUtil {
val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
def toJson[T](data: T): String = {
val out = new StringWriter()
mapper.writeValue(out, data)
out.toString
}
def fromJson[T: ClassTag](json: String)(implicit m: Manifest[T]): T =
mapper.readValue[T](json)
}
示例2: any
//设置package包名称以及导入依赖的类
package ru.finagram.test
import org.mockito.verification.VerificationWithTimeout
import org.mockito.{ ArgumentCaptor, Mockito }
import scala.concurrent.duration._
import scala.reflect.Manifest
trait MockitoSugar extends org.scalatest.mockito.MockitoSugar {
def any[T <: Any](implicit manifest: Manifest[T]): T = {
org.mockito.Matchers.any(manifest.runtimeClass.asInstanceOf[Class[T]])
}
def argumentCaptor[T <: Any](implicit manifest: Manifest[T]): ArgumentCaptor[T] = {
ArgumentCaptor.forClass(manifest.runtimeClass.asInstanceOf[Class[T]])
}
def timeout(duration: Duration): VerificationWithTimeout = {
Mockito.timeout(duration.toMillis.toInt)
}
}
示例3: ManifestSerialization
//设置package包名称以及导入依赖的类
package sbtmarathon
import scala.reflect.{Manifest, ManifestFactory}
import org.json4sbt.{Formats, JValue, JObject, JArray, JString}
object ManifestSerialization {
private val NameKey = "runtimeName"
private val TypeParamsKey = "typeArguments"
def manifestOf[A : Manifest]: Manifest[_] = manifest[A]
def emit(m: Manifest[_]): JValue = {
val head = NameKey -> JString(m.runtimeClass.getName)
val tail = m.typeArguments.map(emit) match {
case Nil => Nil
case jvalues => (TypeParamsKey -> JArray(jvalues)) :: Nil
}
JObject((head :: tail): _*)
}
def parse(jvalue: JValue)(implicit formats: Formats): Manifest[_] = {
val runtimeManifest = manifestFromString((jvalue \ NameKey).extract[String])
val typeArguments = jvalue \ TypeParamsKey match {
case JArray(jvalues) => jvalues.map(parse)
case _ => Nil
}
typeArguments match {
case Nil => runtimeManifest
case head :: tail => ManifestFactory.classType(runtimeManifest.runtimeClass, head, tail: _*)
}
}
private def manifestFromString(value: String): Manifest[_] = value match {
case "boolean" => manifestOf[Boolean]
case "byte" => manifestOf[Byte]
case "char" => manifestOf[Char]
case "short" => manifestOf[Short]
case "int" => manifestOf[Int]
case "long" => manifestOf[Long]
case "float" => manifestOf[Float]
case "double" => manifestOf[Double]
case _ => ManifestFactory.classType(Class.forName(value))
}
}
示例4: ManifestSerializationSpec
//设置package包名称以及导入依赖的类
package sbtmarathon
import scala.reflect.Manifest
import org.json4sbt._
import org.json4sbt.jackson.JsonMethods
import org.scalatest.{FlatSpec, Matchers}
class ManifestSerializationSpec extends FlatSpec with Matchers {
import ManifestSerialization._
behavior of "ManifestSerializer"
Seq(
manifestOf[String],
manifestOf[Int],
manifestOf[Array[String]],
manifestOf[List[String]],
manifestOf[Map[String, Seq[Int]]],
manifestOf[Map[String, Seq[Map[java.lang.Integer, String]]]]
).foreach { m =>
it should s"emit and parse JSON representation of Manifest[$m]" in {
ensureSuccessfulRoundTrip(m)
}
}
def ensureSuccessfulRoundTrip(manifest: Manifest[_]): Unit = {
implicit val formats = DefaultFormats
val json = JsonMethods.compact(JsonMethods.render(emit(manifest)))
manifest shouldBe parse(JsonMethods.parse(json))
}
}