1

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. Is that really necessary?

Domenic
  • 110,262
  • 41
  • 219
  • 271

1 Answers1

0

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. Since you're just querying for a map, just extract the values you need out of the DBObject using getAs[T].

Number may not be a supported type in Salat - I've certainly never tried it. If you need Number you can write a custom transformer or send a pull request to add real support to Salat.

prasinous
  • 798
  • 3
  • 6
  • `Number` is the only way to work with JSON in my short experience, since otherwise it throws an exception whenever you have non-fractional numbers (e.g. `[45.4, 45.6, 45.8, 46, 46.2]`). – Domenic Feb 08 '14 at 20:54
  • Can you show what the `getAs` code would look like? – Domenic Feb 08 '14 at 20:55