Using the ReactiveMongo driver I am trying to serialize a Map of potentially unknown keys/fields of
mixed types Map[String, Any] into a BSONDocument. My question is quite similar to that asked here which refers to the ReactiveMongo documentation example of serializing maps here. Unfortunately the answer only seems to work for Maps of a consistent type whereas I need it for Any. My MapWriter is as follows:
implicit def ValueMapWriter[V](implicit vw: BSONWriter[V, _ <: BSONValue]): BSONDocumentWriter[Map[String, V]] =
new BSONDocumentWriter[Map[String, V]] {
def write(map: Map[String, V]): BSONDocument = {
val elements = map.toStream.map {
tuple =>
tuple._1 -> vw.write(tuple._2)
}
BSONDocument(elements)
}
}
The following compiles and serializes just fine as all types are Ints
val allInts = Map("someInt" -> 1, "someOtherInt" -> 2)
val docFromMap: BSONDocument = BSON.writeDocument(allInts)
However I cannot seem to massage it into something that will handle Map[String,Any]
val mixedTypes = Map("someDouble" -> 234.324, "someString" -> "abc", "someInt" -> 1)
val docFromMap: BSONDocument = BSON.writeDocument(mixedTypes)
results in:
Error:(134, 54) could not find implicit value for parameter writer: reactivemongo.bson.BSONWriter[scala.collection.immutable.Map[String,Any],reactivemongo.bson.BSONDocument]
val docFromMap: BSONDocument = BSON.writeDocument(mixedTypes)
^
There is likely some fundamental concept of implicits that I'm missing in order to accomplish this, without explicitly knowing the types of each field in the map at compile time.
Any. Either you can replace the map with something typesafe, or you can replace it byJsObject(kind of map with types that can be discovered from the JSON semantic) - cchantep