从 Scala 中的类名获取类型

编程入门 行业动态 更新时间:2024-10-09 22:17:05
本文介绍了从 Scala 中的类名获取类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想做如下事情:

val factoryType = typeOf[Class. forName("com.menith.amw.worksheets." + params("problem") + "ProblemFactory")] val factory = parse(params("args")).extract[factoryType]

parse 方法允许我通过给它一个 JSON 字符串来获取 case 类的实例,然后我可以通过将预期类型传递给它来使用提取方法.但是,我在从 Class.forName 获取类型时遇到了一些问题.

The parse method allows me to obtain an instance of a case class by giving it a JSON string and I can then use the extract method by passing it the expected type. However I'm having some issues getting the type from Class.forName.

推荐答案

一个可能有效的很好的解决方案是进行多态反序列化.这允许您向 json 添加一个字段(如类型")并允许 Jackson(假设您重新使用一个很棒的 json 解析器(如 Jackson)来代表您找出正确的类型.

A great solution that might work would be to do polymorphic deserialization. This allows you to add a field (like "type") to your json and allow Jackson (assuming you're using an awesome json parser like Jackson) to figure out the proper type on your behalf.

这篇帖子提供了很好的介绍到多态类型.它涵盖了许多有用的情况,包括您无法修改第 3 方代码的情况(这里您添加了一个 Mixin 来注释类型层次结构).

This post gives a great introduction to polymorphic types. It covers many useful cases including the case where you can't modify 3rd party code (here you add a Mixin to annotate the type hierarchy).

最简单的情况最终看起来像这样(所有这些也适用于 Scala 对象——jackson 甚至有一个很棒的 Scala 模块):

The simplest case ends up looking like this (and all of this works great with Scala objects too -- jackson even has a great scala module):

object Test { @JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type" ) @JsonSubTypes(Array( new Type(value = classOf[Cat], name = "cat"), new Type(value = classOf[Dog], name = "dog") )) trait Animal case class Dog(name: String, breed: String, leash_color: String) extends Animal case class Cat(name: String, favorite_toy: String) extends Animal def main(args: Array[String]): Unit = { val objectMapper = new ObjectMapper with ScalaObjectMapper objectMapper.registerModule(DefaultScalaModule) val dogStr = """{"type": "dog", "name": "Spike", "breed": "mutt", "leash_color": "red"}""" val catStr = """{"type": "cat", "name": "Fluffy", "favorite_toy": "spider ring"}""" val animal1 = objectMapper.readValue[Animal](dogStr) val animal2 = objectMapper.readValue[Animal](catStr) println(animal1) println(animal2) } }

这会生成以下输出:

// Dog(Spike,mutt,red) // Cat(Fluffy,spider ring)

您也可以避免列出子类型映射,但它要求json类型"字段稍微复杂一些.试验一下;你可能会喜欢.像这样定义动物:

You can also avoid listing the subtype mapping, but it requires that the json "type" field is a bit more complex. Experiment with it; you might like it. Define Animal like this:

@JsonTypeInfo( use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "type" ) trait Animal

它会像这样产生(并消耗)json:

And it produces (and consumes) json like this:

/* { "breed": "mutt", "leash_color": "red", "name": "Spike", "type": "classpath.to.Test$Dog" } { "favorite_toy": "spider ring", "name": "Fluffy", "type": "classpath.to.Test$Cat" } */

更多推荐

从 Scala 中的类名获取类型

本文发布于:2023-11-25 17:57:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630750.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   Scala

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!