簡體   English   中英

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

[英]How to parse this “pretty” BSON in Scala?

在我們的項目中,我們使用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類。

有什么幫助嗎?

取自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(...),我仍然沒有想辦法。 但是,編譯器會提醒您。

您可以在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)))
  }
}

這是我正在使用的解決方案。 希望對您有所幫助->

    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", ", ", "")
      }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM