简体   繁体   English

在PlayFramework 2.4中使用JSON转换器添加可选属性

[英]Add optional property with json transformers in playframework 2.4

I can't understand, how can i add optional property with json transformer. 我不明白,如何使用json转换器添加可选属性。

I want merge two json objects (list and calendars) without or with dynamic list of properties (for example without owner ): 我想合并两个json对象(列表和日历),不带或带有动态属性列表(例如,不带owner ):

calendar1 = {id:1, name: "first", description:"my first calendar", owner: 1}
calendar2 = {id:2, name: "second", owner: 1}

list = [{id: 1, settings: []}, {id: 2, settings: []}]

and result must be 结果必须是

{calendars:
    [
      {id:1, name: "first", description:"my first calendar", settings: []}, 
      {id:2, name: "second", settings: []}
    ]
}

I'll assume the following json trees 我假设以下json树

val calendar1 = Json.parse("""{"id":1, "name": "first", "description":"my first calendar", "owner": 1}""")
val calendar2 = Json.parse("""{"id":2, "name": "second", "owner": 1}""")

You need to add settings to each calendar, then remove the owner if it exists. 您需要为每个日历添加设置,然后删除所有者(如果存在)。

Putting a value in branch settings is explained in the documentation 文档中说明了在分支settings放置值的方法

val addSettings = __.json.update((__ \ "settings").json.put(Json.arr()))

Dropping the owner is also explained 说明了放弃所有者

val removeOwner = (__ \ "owner").json.prune

Now you can define the transformer to be applied to each of your calendar object 现在,您可以定义要应用于每个日历对象的转换器

val transformer = addSettings andThen removeOwner

With that in place there are multiple options depending on how your data is actually modeled. 有了这些,根据实际数据建模的方式,可以有多种选择。 If you have a Seq of calendars as in 如果您有如下Seq的日历:

val calendars = Seq(calendar1, calendar2)

you can do 你可以做

val normalizedCalendars = calendars.map(_.transform(transformer)) 

This gives you a Seq[JsResult[JsObject]] which you want to transform into a JsResult[Seq[JsObject]] . 这为您提供了一个Seq[JsResult[JsObject]] ,您想将其转换为JsResult[Seq[JsObject]]

I am pretty sure there is a way to do it using play's functional syntax (see play.api.libs.functional and play.api.libs.functional.syntax ) but this part of play is not well documented and I haven't gotten around to studying Applicatives yet even though I have a feel for what they do. 我很确定有一种方法可以使用play的函数语法(请参阅play.api.libs.functionalplay.api.libs.functional.syntax ),但是这部分内容并没有得到很好的记录,我还没有得到尽管我对他们的工作有所了解,但仍然可以学习Applicatives

Instead, I rely on the following code inspired by scala's Future#sequence 相反,我依赖于以下受Scala Future#sequence启发的代码

def sequence[A, M[X] <: TraversableOnce[X]](in: M[JsResult[A]])(implicit cbf: CanBuildFrom[M[JsResult[A]], A, M[A]]): JsResult[M[A]] = {
  val empty: JsResult[mutable.Builder[A, M[A]]] = JsSuccess(cbf(in))
  in.foldLeft(empty) {(jracc,jrel) => (jracc,jrel) match {
      case (JsSuccess(builder,_), JsSuccess(a,p)) =>JsSuccess(builder+=a, p)
      case (ra@JsError(builderErrors), re@JsError(errors)) =>JsError.merge(ra, re)
      case (JsSuccess(_,_), re@JsError(errors)) => re
      case (ra@JsError(builderErrors), JsSuccess(_,_)) => ra
    }} map (_.result())
}

With that you can write : 这样,您可以编写:

val calendarArray = sequence(normalizedCalendars).map(v=>Json.obj("calendars"->JsArray(v)))

which will give you a JsResult[JsObject] . 这会给你一个JsResult[JsObject] As long as your original calendars are indeed JsObject s you will get a JsSuccess . 只要您的原始日历确实是JsObject您就可以获得JsSuccess You can verify the output structure with : 您可以使用以下命令验证输出结构:

calendarArray.foreach(println)

which returns : 返回:

{"calendars":[{"id":1,"name":"first","description":"my first calendar","settings":[]},{"id":2,"name":"second","settings":[]}]}

which is the same as what you asked modulo some whitespace 这与您对某些空格取模的要求相同

{
  "calendars":[
    {"id":1,"name":"first","description":"my first calendar","settings":[]},
    {"id":2,"name":"second","settings":[]}
  ]
}

Start with: 从...开始:

scala> case class Calendar(id:Int,name:String,description:Option[String],owner:Int)
defined class Calendar

scala> case class CalendarRow(id:Int,name:String,description:Option[String],settings:Seq[String]=Seq.empty)
defined class CalendarRow

scala>  def append(calendars:Calendar*) = calendars.map(c => CalendarRow(c.id,c.name,c.description))
append: (calendars: Calendar*)Seq[CalendarRow]

scala> val calendar1 = Calendar(1,"first",Option("my first calendar"),1)
calendar1: Calendar = Calendar(1,first,Some(my first calendar),1)

scala> val calendar2 = Calendar(2, "second",None,1)
calendar2: Calendar = Calendar(2,second,None,1)

scala> val list =  append(calendar1,calendar2)
list: Seq[CalendarRow] = ArrayBuffer(CalendarRow(1,first,Some(my first calendar),List()), CalendarRow(2,second,None,List()))

Many thanks to @Jean and comments in "Unveiling Play 2.1 Json API - Part 3 : JSON Transformers" 非常感谢@Jean和“发布Play 2.1 Json API-第3部分:JSON变形器”中的评论

It's hard understanding things like and , andThen , andKeep , keepAnd in JSON transformers for me (I can not find any detailed descriptions with examples), but i found some templates for my question: 我很难理解JSON transformers andandThenandKeepkeepAnd (我无法找到带有示例的详细描述),但是我发现了一些模板来回答我的问题:

Optional property in JSON: JSON中的可选属性:

With Reader 与读者

(__ \ "id").json.pick[JsString].flatMap{
  case id if id.equals(JsString(accountId)) =>
    (__ \ "primary").json.put(JsBoolean(true))
  case _ =>
    Reads.pure(Json.obj())
}

With Json.obj() 使用Json.obj()

(__ \ "id").json.pick[JsString].map{
  case id if id.equals(JsString(accountId)) =>
    Json.obj("primary" -> true)
  case _ =>
    Json.obj()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM