2013-12-19 42 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也不适合改变架构? –