如何使用lift-json的类提取器构造函数mongo bson数组? [英] How to use lift-json's class extractor constructor mongo bson array?

查看:134
本文介绍了如何使用lift-json的类提取器构造函数mongo bson数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用lift-json通过类提取器渲染bson字符串,然后,使用mongo Document类构造函数使用该bson字符串创建文档实例.

I'm use lift-json render a bson string with class extractor, after that, use mongo Document class constructor a document instance with that bson string.

一个问题是如何表示$or bson.这似乎不是经典的json数组.

A problem is how about represent $or bson.It seems not a classic json array.

 {"$or": [
      {"username": "administrator"},
      {"phone":"110"},
      {"email":"123@xxx.com"},
      {"pen_name":"lorancechen"}
 ]}

如何使用Lift类提取器来表示此bson数组?

How to use lift class extractor represent this bson array?

此外,在app和mongo之间使用字符串的原因是它们是在一个简单的套接字下进行通信的.

Besides, the reason of use string between app and mongo is they are communicate under a simple socket.

更新添加示例
提取正常的数组类,如下所示:

UPDATE add a example
extractor a normal array class as follow:

import net.liftweb.json._
import net.liftweb.json.Extraction._

case class Name(name: String)
case class JsonArray(array:List[Name])

object JsonClient extends App {
  implicit val formats = DefaultFormats

  val names = Name("jone01") :: Name("jone02") :: Nil
  val array = JsonArray(names)
  val jsonString = prettyRender(decompose(array))
  println(jsonString)
}

输出:

{
  "array":[
    {
      "name":"jone01"
    },
    {
      "name":"jone02"
    }
  ]
}

如何表示这一点

{"$or": [
      {"username": "administrator"},
      {"phone":"110"},
      {"email":"123@xxx.com"},
      {"pen_name":"lorancechen"}
 ]}

元素内部"$ or"的每个字段键(例如,usernamephone)都不是通用键名,我还没有找到使用类模板来表示它的方法.

every field key (eg, username, phone) of element inner "$or" is not common key name and I haven't find a way to represent it use class template.

推荐答案

我不明白为什么您的JSON结构是这种方式,也许您想要的是以下内容:

I don't get it why your JSON structure is that way, maybe what you want is the following one:

{
  "$or": [
    {
      "username": "administrator", "phone":"110",  
      "email":"123@xxx.com", "pen_name":"lorancechen"
    },
    {
      "username": "xxx", "phone":"xxx",  
      "email":"xxx", "pen_name":"xxx"
    }
    ...
  ]
}

实际上,Lift提供了这样的工具,缺点是实现起来有点难看.

Actually Lift provides such tools and the downside is that the implementation is a little bit ugly.

  import net.liftweb.mongodb.{JsonObjectMeta, JsonObject}

  // the trait here is for unifying the type of 
  //four case classes i.e Username, Phone....
  sealed trait PersonField

  object Username extends JsonObjectMeta[Username]
  case class Username(username: String) extends JsonObject[Username] 
     with PersonField {
     def meta = Username
  }

  case class Phone(phone: String) extends JsonObject[Phone] with PersonField {
     def meta = Phone
  }
  object Phone extends JsonObjectMeta[Phone]

  case class Email(email: String) extends JsonObject[Email] with PersonField {
     def meta = Email
  }
  object Email extends JsonObjectMeta[Email]

  case class PenName(pen_name: String) extends JsonObject[PenName] 
     with PersonField {
     def meta = PenName
  }
  object PenName extends JsonObjectMeta[PenName]

  case class Output(`$or`: List[PersonField]) extends JsonObject[Output] {
    def meta = Output
  }

  object Output extends JsonObjectMeta[Output]


  object JsonClient extends App {

     val username = Username("administrator")
     val phone = Phone("110")
     val email = Email("123@xxx.com")
     val penName = PenName("lorancechen")
     val outPut = Output(username :: phone :: email :: penName :: Nil)

     import net.liftweb.json._
     implicit val formats = DefaultFormats

     import net.liftweb.json.{JsonAST, Printer}
     val result = Printer.pretty(JsonAST.render(outPut.asJObject))

     /*
        {
          "$or":[{
            "username":"administrator"
          },{
            "phone":"110"
          },{
            "email":"123@xxx.com"
          },{
            "pen_name":"lorancechen"
          }]
        }

     */
     println(result)
  }

无论如何,希望对您有所帮助.

Anyway, hope it helps.

这篇关于如何使用lift-json的类提取器构造函数mongo bson数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆