本文介绍了如何在电梯中反序列化DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将JSON中的org.joda.time.DateTime字段反序列化为案例类。



JSON:

val ajson = parse({creationDate:2013-01-02T10 :48:41.000-05:00})



我还设置了这些序列化选项:

隐式val formats = Serialization.formats(NoTypeHints)++ net.liftweb.json.ext.JodaTimeSerializers.all



反序列化:

val val1 = ajson.extract [Post]



其中帖子是:

case class Post( creationDate :DateTime){...} / p>

我得到的例外是:

  net.liftweb.json。 MappingException:createDate的可用值
日期格式无效2013-01-02T10:48:41.000-05:00

如何将该日期字符串反序列化为DateTime对象?



编辑:

这个工作原理: val date3 = new DateTime(2013-01-05T06:24:53.000- 05:00)
,它使用与反序列化中相同的JSON字符串。这里发生了什么?

解决方案

似乎是 DateParser 格式电梯默认使用。在挖掘,您可以看到解析器尝试在将结果传递给构造函数之前使用 DateParser.parse(s,format)对于 org.joda.time.DateTime

  object DateParser {
def parse(s:String,format:Formats)=
format.dateFormat.parse(。)(_。getTime).getOrElse(throw new MappingException(Invalid date format+ s))
}

案例对象DateTimeSerializer扩展CustomSerializer [DateTime](format =>(
{
case JString(s)=> new DateTime(DateParser.parse (s,format))
case JNull => null
},
{
case d:DateTime => JString(format.dateFormat.format(d.toDate) )
}
))

Lift似乎使用的格式是: yyyy-MM-dd'T'HH:mm:ss.S SS'Z'



要解决这个问题,您可以指定正确的模式,并将其添加到序列化选项中,或者如果喜欢刚刚拥有JodaTime构造函数做所有的工作,你可以创建自己的序列化器,如:

  case对象MyDateTimeSerializer扩展CustomSerializer [ DateTime](format => (
{
case JString(s)=> tryo(new DateTime(s))。openOr(throw new MappingException(Invalid date format+ s))
case JNull => ; null
},
{
案例d:DateTime => JString(format.dateFormat.format(d.toDate))
}
))

然后将其添加到您的格式列表中,而不是 net.liftweb.json .ext.JodaTimeSerializers.all


I am having trouble deserializing a org.joda.time.DateTime field from JSON into a case class.

The JSON:
val ajson=parse(""" { "creationDate": "2013-01-02T10:48:41.000-05:00" }""")

I also set these serialization options:
implicit val formats = Serialization.formats(NoTypeHints) ++ net.liftweb.json.ext.JodaTimeSerializers.all

And the deserialization:
val val1=ajson.extract[Post]

where Post is:
case class Post(creationDate: DateTime){ ... }

The exception I get is:

 net.liftweb.json.MappingException: No usable value for creationDate
    Invalid date format 2013-01-02T10:48:41.000-05:00

How can I deserialize that date string into a DateTime object?

EDIT:
This works: val date3= new DateTime("2013-01-05T06:24:53.000-05:00")which uses the same date string from the JSON as in the deserialization. What's happening here?

解决方案

Seems like it is the DateParser format that Lift uses by default. In digging into the code, you can see that the parser attempts to use DateParser.parse(s, format) before passing the result to the constructor for org.joda.time.DateTime.

object DateParser {
  def parse(s: String, format: Formats) =
    format.dateFormat.parse(s).map(_.getTime).getOrElse(throw new MappingException("Invalid date format " + s))
}

case object DateTimeSerializer extends CustomSerializer[DateTime](format => (
  {
    case JString(s) => new DateTime(DateParser.parse(s, format))
    case JNull => null
  },
  {
    case d: DateTime => JString(format.dateFormat.format(d.toDate))
  }
))

The format that Lift seems to be using is: yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

To get around that, you could either specify the correct pattern and add that to your serialization options, or if you would prefer to just have the JodaTime constructor do all the work, you could create your own serializer like:

case object MyDateTimeSerializer extends CustomSerializer[DateTime](format => (
  {
    case JString(s) => tryo(new DateTime(s)).openOr(throw new MappingException("Invalid date format " + s))
    case JNull => null
  },
  {
    case d: DateTime => JString(format.dateFormat.format(d.toDate))
  }
))

And then add that to your list of formats, instead of net.liftweb.json.ext.JodaTimeSerializers.all

这篇关于如何在电梯中反序列化DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 00:58