如何在Scala中解析这个“漂亮”的BSON?

时间:2013-12-17 14:42:08

标签: scala bson case-class reactivemongo

在我们的项目中,我们使用的是Scala和Reactivemongo。 (我对两者都很新) 当您向控制台打印“漂亮”的Bson时,它看起来像这样:

{  _id: BSONObjectID("52b006fe0100000100d47242"),
desc: BSONString({"_id:"BSONObjectID(52af03a5010000010036194d),"desc:"BSONString(www.foo.com"hits),"domains:"["0:"BSONString(www.foo.com)"],"func:"BSONString(Count),"maxr:"BSONInteger(5),"props:"["]"} hits),
domains: [
0: BSONString(jsonString)
],
func: BSONString(Count),
maxr: BSONInteger(5),
props: [
]
}

我需要能够从控制台将其解析为一个相应的Case Class。

请帮忙吗?

3 个答案:

答案 0 :(得分:1)

取自typesafe的激活器模板,你可以简单地使用Json.format作为case类的伴随对象(ReactiveMongo 0.9,scala 2.10.2)中的隐式val。例如:

package models

import play.api.libs.json.Json
import reactivemongo.bson.BSONObjectID
import play.modules.reactivemongo.json.BSONFormats._

/**
 * A message class
 *
 * @param _id The BSON object id of the message
 * @param message The message
 */
case class Message(_id: BSONObjectID, message: String)

object Message {
  /**
   * Format for the message.
   *
   * Used both by JSON library and reactive mongo to serialise/deserialise a message.
   */
  implicit val messageFormat = Json.format[Message]
}

我正在使用它,只要JSON知道如何格式化它们就可以使用更多参数,或者,如果你有成员是你创建的案例类,如果它们具有相同的:

package models

import play.api.libs.json.Json
import reactivemongo.bson.BSONObjectID
import play.modules.reactivemongo.json.BSONFormats._

/**
 * A message class
 *
 * @param _id The BSON object id of the message
 * @param message The message
 */

case class Name(fName: String, lName: String, mInitial: String)

object Name {
  implicit val nameFormat = Json.format[Name]
}

case class Message(_id: BSONObjectID, message: String, name: Name)

object Message {
  /**
   * Format for the message.
   *
   * Used both by JSON library and reactive mongo to serialise/deserialise a message.
   */
  implicit val messageFormat = Json.format[Message]
}

如果你有辅助构造函数,或者你在同伴中实现了apply(...),我仍然没有想办法。但是,编译器会提醒您。

答案 1 :(得分:0)

您可以在Reactivemongo文档的此链接中查看他们是如何做到的:http://stephane.godbillon.com/2012/10/18/writing-a-simple-app-with-reactivemongo-and-play-framework-pt-1.html

implicit object ArticleBSONReader extends BSONReader[Article] {
def fromBSON(document: BSONDocument) :Article = {
  val doc = document.toTraversable
  Article(
    doc.getAs[BSONObjectID]("_id"),
    doc.getAs[BSONString]("title").get.value,
    doc.getAs[BSONString]("content").get.value,
    doc.getAs[BSONString]("publisher").get.value,
    doc.getAs[BSONDateTime]("creationDate").map(dt => new DateTime(dt.value)),
    doc.getAs[BSONDateTime]("updateDate").map(dt => new DateTime(dt.value)))
  }
}

答案 2 :(得分:0)

这是我正在使用的解决方案。希望对您有所帮助->

    def bsonToString(bson: BSONDocument): String = {
        val bsonAsKeyValueList = bson.toMap.toList.map {
          case (key, document: BSONDocument) =>
            (key, bsonToString(document))
          case (key, value: BSONValue) =>
            (key, value.toString)
        }
        val bsonAsKeyValueStrings = bsonAsKeyValueList.map {
          case (key, value) =>
            s"""("${key}" -> ${value})"""
        }
        bsonAsKeyValueStrings.mkString("BSONDocument", ", ", "")
      }