• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala Manifest类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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)
} 
开发者ID:SarathChandran,项目名称:InventoryTracker,代码行数:26,代码来源:JsonUtil.scala


示例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)
  }
} 
开发者ID:finagram,项目名称:finagram,代码行数:23,代码来源:MockitoSugar.scala


示例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))
  }
} 
开发者ID:Tapad,项目名称:sbt-marathon,代码行数:47,代码来源:ManifestSerialization.scala


示例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))
  }
} 
开发者ID:Tapad,项目名称:sbt-marathon,代码行数:33,代码来源:ManifestSerializationSpec.scala



注:本文中的scala.reflect.Manifest类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala AsResult类代码示例发布时间:2022-05-23
下一篇:
Scala AWSStaticCredentialsProvider类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap