简体   繁体   English

Scala Salat反序列化:如何获取Map [String,Number]?

[英]Scala Salat Deserialization: how to get a Map[String, Number]?

My database looks like 我的数据库看起来像

[
  {
    name: "domenic",
    records: {
      today: 5,
      yesterday: 1.5
    }
  },
  {
    name: "bob",
    records: { ... }
  }
]

When I try queries like 当我尝试查询

val result: Option[DBObject] = myCollection.findOne(
  MongoDBObject("name" -> "domenic")
  MongoDBObject("records" -> 1),
)

val records = result.get.getAs[BasicDBObject]("records").get
grater[Map[String, Number]].asObject(records)

it fails (at runtime!) with 它失败(在运行时!)与

GRATER GLITCH - unable to find or instantiate a grater using supplied path name

REASON: Class scala.collection.immutable.Map is an interface

Context: 'global'
Path from pickled Scala sig: 'scala.collection.immutable.Map'

I think I could make this work by creating a case class whose only field is a Map[String, Number] and then getting its property. 我想我可以通过创建一个case类(其唯一的字段是一个Map[String, Number] ,然后获取其属性来完成这项工作。 Is that really necessary? 那真的有必要吗?

grater doesn't take a collection as a type argument, only a case class or a trait/abstract class whose concrete representations are case classes. grater不会将集合作为类型参数,而只会将其具体表示形式为case类的case类或trait / abstract类作为参数。 Since you're just querying for a map, just extract the values you need out of the DBObject using getAs[T] . 由于您只是在查询地图,因此只需使用getAs[T]DBObject提取所需的值getAs[T]

Number may not be a supported type in Salat - I've certainly never tried it. Number可能无法在礼拜支持的类型-我当然从来没有尝试过。 If you need Number you can write a custom transformer or send a pull request to add real support to Salat. 如果您需要Number ,则可以编写自定义转换器或发送拉动请求以向Salat添加真正的支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM