在ReactiveMongo Play JSON中格式化ISODate表单字段 [英] Format an ISODate form field in ReactiveMongo Play JSON

查看:149
本文介绍了在ReactiveMongo Play JSON中格式化ISODate表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Play Framework和 ReactiveMongo Play JSON库中使用MongoDB ;我正在尝试正确配置 form .我在MongoDB中存储了一些日期,这些日期位于ISODate包装器/monad中,并作为JSObject处理:

Using MongoDB with Play Framework and the ReactiveMongo Play JSON library; I am trying to correctly configure a form. I have some dates stored in MongoDB which are in an ISODate wrapper/monad and handled as a JSObject:

   Json.obj("dateOfBirth" -> Json.obj("$date" -> dateTimeLong))

使用在Play中自动映射的案例类的示例是:

An example of my case class using automated mapping in Play is:

   case class FormData(_id: Option[BSONObjectID], name: String, dateOfBirth: Option[JSObject])

这是表格:

  object MyForm {

     val form = Form(
        mapping(
          "_id" -> ignored(Option.empty[BSONObjectID]),
          "name" -> nonEmptyText,
          "dateofBirth" -> ? // not `date` or `optional(date)`
        )
     )

  }

我认为我需要自定义绑定form中的dateOfBirth字段,但这是我遇到的困难.我找不到在何处包含隐式对象以将新类型的格式设置为此处显示

I think that I need to custom bind the dateOfBirth field in the form but this is where I am having difficulty. I couldn't find where to include an implicit object to format a new type as shown here and this also didn't help me with this (and looks like it might be outdated by now).

我认为bind方法可能看起来像这样:

I think that the bind method might look something like this:

  def bind(date: Date, dateFieldName: String): JsObject = {

    val longDate = new DateTime(date.getTime())
      .withZoneRetainFields(DateTimeZone.UTC)
      .withZone(DateTimeZone.getDefault())
      .getMillis

    Json.obj(s"$$$dateFieldName" -> JsNumber(longDate))

  }

但是我可能错了,但是尝试尝试会很好,因为我说我似乎找不到要插入custom formatter的正确位置-我认为这应该在 companion对象中(FormData)(格式将用于嵌套字段).情况并非如此,因此我再次与社区接洽.感谢您的任何建议-特别是如果有更好的方法来解决这个问题.

But I could be wrong and it would be nice to try however as I say I can't seem to find where the correct place to insert a custom formatter - I thought this should be in the companion object of FormData (where formats would be for nested fields). This wasn't the case so I am approaching the community once more. Thanks for any suggestions - particularly if there is a better way to handle this.

推荐答案

我知道答案已经存在,但是绝对不应该如此隐藏!这是给任何感到我痛苦的人的.因此,我更改了以下内容:

I knew that the answer was out there but it definitely shouldn't have been so well hidden! This one is for anyone that is feeling my pain. So I changed the following:

   case class FormData(_id: Option[BSONObjectID], name: String, dateOfBirth: Option[Date])

还有表格..

  object MyForm {

     val form = Form(
        mapping(
          "_id" -> ignored(Option.empty[BSONObjectID]),
          "name" -> nonEmptyText,
          "dateOfBirth" -> optional(date)
        )(FormData.apply)(FormData.unapply)
     )

  }

现在是关键部分.在 case类伴侣对象中(我的名字是 FormData ),在其中放置以下内容(此内容已从

And now the critical part. Within the companion object of the case class (mine is FormData) is where to put the following (this was altered from Stephane Godbillon's answer here):

  implicit val dateRead: Reads[Date] = (__ \ "$date").read[Long].map { date =>
    new Date(date)
  }

  implicit val dateWrite: Writes[Date] = new Writes[Date] {
    def writes(date: Date): JsValue = Json.obj(
      "$date" -> date.getTime
    )
  }

这篇关于在ReactiveMongo Play JSON中格式化ISODate表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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