Reputation: 1137
I have data coming back from MongoDB that looks like this:
> db.foo.findOne()
[
{
"_id" : "some string",
"bar" : [
[
14960265,
0.5454545454545454
],
[
30680,
0.36363636363636365
],
[
12852625,
0.09090909090909091
]
],
}
]
The bar
property contains a list of unknown size, each item of which is a list of length two containing an Int and a Double. In Scala, I would represent this as List[(Int, Double)].
How would I write the model for this structure to use with Salat?
Salat doesn't do tuples, so I tried:
case class FooEntry(a: Int, b: Double)
case class Foo(_id: String, bar: List[FooEntry])
but got:
java.lang.IllegalArgumentException: BasicBSONList can only work with numeric keys, not: [a]
Also tried:
case class Foo(_id: String, sps: List[Any])
but got:
java.lang.ClassCastException: com.mongodb.BasicDBList cannot be cast to scala.collection.immutable.List
Obviously, the data could be stored in a better form, with an object instead of the length-two arrays. But given that's what I've got, is there a good way to use Salat to deserialize it? Thanks!
Upvotes: 0
Views: 601
Reputation: 798
Salat project lead here. No matter what your data structure, you would need to specify a type for the list. Salat doesn't support tuples yet, and while Salat supports polymorphic collections (this requires type hints!), it doesn't support lists of heterogeneous types like yours.
Can you restructure your data so that the array members are not lists but instead
[
{x: 123, y: 123.0},
{x: 456, y: 456.0}
]
Then you could use
case class Bar(x: Long, y: Double)
case class Foo(_id: String, sps: List[Bar])
Alternately, consider trying to use Miles Sabin's Shapeless project or Alois Cochard's Sherpa project to deserialize your data.
Upvotes: 3