2013-12-19 37 views
0

我的MongoDB的集合是這樣的:如何轉換MongoDBObject到JsonString

> db.FakeCollection.find().pretty() 
{ 
    "_id" : ObjectId("52b2d71c5c197846fd3a2737"), 
    "categories" : [ 
      { 
        "categoryname" : "entertainment", 
        "categoryId" : "d3ffca550ae44904aedf77cdcbd31d7a", 
        "displayname" : "Entertainment", 
        "subcategories" : [ 
          { 
            "subcategoryname" : "games", 
            "subcategoryId" : "ff3d0cbeb0eb4960b11b47d7fc64991b", 
            "displayname" : "Games" 
          } 
        ] 
      } 
    ] 
    } 

我想寫一個測試用例與MongodbCasbah階使用Specs2 JsonMatchers下面的集合。 如何將DBObjects轉換爲字符串?

回答

1

簡短的回答:

val doc: com.mongodb.DBObject = ??? 
pretty(render(net.liftweb.mongodb.JObjectParser.serialize(doc))) 

朗的答案,解釋發生了什麼事情。我包括爲了清晰完整的類型名稱:

import net.liftweb.mongodb.JObjectParser 
import net.liftweb.json.DefaultFormats 

// default JSON formats for `parse` and `serialize` below 
implicit val formats = DefaultFormats 

// Convert DBObject to JValue: 
val doc: com.mongodb.DBObject = ??? // get it somehow 
val jsonDoc: net.liftweb.json.JValue = JObjectParser.serialize(doc) 

// Convert JValue to DBObject: 
val doc2: net.liftweb.json.JObject = ??? 
val dbObj: com.mongodb.DBObject = JObjectParser.parse(doc2) 

// Render JSON as String: 
import net.liftweb.json._ 
pretty(render(jsonDoc)) 
// or use compactRender, compact(render(jsonDoc)), etc 

比較JSON文件有差異:val Diff(changed, added, deleted) = json1 diff json2

更多的信息在這裏:https://github.com/lift/lift/tree/master/framework/lift-base/lift-json/

您可以specs2測試和提起DIFF這種方式,例如:我們不使用電梯記錄

json1 diff json2 mustEqual Diff(changedJson, addedJson, JNothing) 
2

我相信你的方法在這裏有點不對。您的收藏應該是這樣的:

class Category extends BsonRecord[Category] { 
    def meta = Category 
    object categoryname extends StringField(this, 200) 
    object categoryId extends StringField(this, 64) 
    object displayname extends StringField(this, 100) 
    object subcategories extends BsonRecordListField(this, Category) 
} 
object Category extends Category with BsonMetaRecord[Category] { 
} 

class FakeCollection extends MongoRecord[FakeCollection] with ObjectIdPk[FakeCollection] { 
    def meta = FakeCollection 
    object categories extends BsonRecordListField(this, Category) 
} 
object FakeCollection extends FakeCollection with MongoMetaRecord[FakeCollection] { 
    override def collectionName = "fakecollection" 
    def getEntryByName: List[Category] = { 
    FakeCollection.find 
    } 
} 

根據該方法,你可以這樣做:

import net.liftweb.json.JsonAST.JValue; 
import net.liftweb.http.js.JsExp; 
import net.liftweb.http.js.JsExp._; 
import net.liftweb.json.JsonDSL.seq2jvalue 
val json: JsExp = seq2JValue(FakeColleciton.find.map(_.asJValue)) 
val stringContent = json.toJsCmd; // now it's here, you can match. 

看一看HERE,看你怎麼可以添加Foursquare的盜賊,讓您的生活更輕鬆。

+0

我們使用MongoDB的卡斯巴 – sagar

+2

我也喜歡盜賊,每當我處理一個穩定的和已知的架構。他們有非常安全的查詢數據庫的方式。然而,在我的一個項目中,我不得不使用casbah和構建具有任意字段的文檔並進行任意查詢。對於這些類型的東西,Rogue + Record不適合。儘管如此,這對我來說更是一個特例。 –

+0

@AlekseyIzmailov:我對這個插件表示歉意,但這是否意味着Rogue + Record也不適合改變架構? –