Skip to content

Instantly share code, notes, and snippets.

@cvogt
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvogt/e9da3ebb8790fe2716ee to your computer and use it in GitHub Desktop.
Save cvogt/e9da3ebb8790fe2716ee to your computer and use it in GitHub Desktop.
ReactiveMongo reader writer handler to serialize nested seqs
implicit def MapReader[V,B <: BSONValue](implicit vr: BSONReader[B,V]) = new BSONDocumentReader[Seq[(String, V)]] {
def read(bson: BSONDocument): Seq[(String, V)] = {
val elements = bson.elements.map { tuple =>
tuple._1 -> tuple._2.seeAsTry(vr).get
}
elements
}
}
implicit def MapWriter[V,B <: BSONValue](implicit vw: BSONWriter[V,B]) = new BSONDocumentWriter[Seq[(String, V)]] {
def write(map: Seq[(String, V)]): BSONDocument = {
val elements = map.toStream.map { tuple =>
tuple._1 -> vw.write(tuple._2)
}
BSONDocument(elements)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment