如何为Java 8 LocalDateTime编写自定义序列化程序



我有一个名为Child1的类,我想使用Lift JSON转换为JSON。一切都很好,我使用joda date time,但现在我想使用Java 8 LocalDateTime,但我无法编写自定义序列化器这是我的代码

import org.joda.time.DateTime
import net.liftweb.json.Serialization.{ read, write }
import net.liftweb.json.DefaultFormats
import net.liftweb.json.Serializer
import net.liftweb.json.JsonAST._
import net.liftweb.json.Formats
import net.liftweb.json.TypeInfo
import net.liftweb.json.MappingException
class Child1Serializer extends Serializer[Child1] {
  private val IntervalClass = classOf[Child1]
  def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), Child1] = {
    case (TypeInfo(IntervalClass, _), json) => json match {
      case JObject(
        JField("str", JString(str)) :: JField("Num", JInt(num)) :: 
        JField("MyList", JArray(mylist)) :: (JField("myDate", JInt(mydate)) :: 
        JField("number", JInt(number)) ::Nil)
      ) => {
        val c = Child1(
          str, num.intValue(), mylist.map(_.values.toString.toInt), new DateTime(mydate.longValue)
        )
        c.number = number.intValue()
        c
      }
      case x => throw new MappingException("Can't convert " + x + " to Interval")
    }
  }
  def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
    case x: Child1 => 
      JObject(
        JField("str", JString(x.str)) :: JField("Num", JInt(x.Num)) ::
        JField("MyList", JArray(x.MyList.map(JInt(_)))) :: 
        JField("myDate", JInt(BigInt(x.myDate.getMillis))) :: 
        JField("number", JInt(x.number)) :: Nil
      )
  }
}
Object Test extends App {
  case class Child1(var str:String, var Num:Int, MyList:List[Int], myDate:DateTime) {
    var number: Int=555
  }
  val c = Child1("Mary", 5, List(1, 2), DateTime.now())
  c.number = 1
  println("number" + c.number)
  implicit val formats = DefaultFormats + new Child1Serializer
  val ser = write(c)
  println("Child class converted to string" + ser) 
  var obj = read[Child1](ser)
  println("object of Child is "+  obj)
  println("str" + obj.str)
  println("Num" + obj.Num)
  println("MyList" + obj.MyList)
  println("myDate" + obj.myDate)
  println("number" + obj.number)
}

现在我想使用Java 8 LocalDateTime像这样

case class Child1(var str: String, var Num: Int, MyList: List[Int], val myDate: LocalDateTime = LocalDateTime.now()) {
  var number: Int=555
}

我需要在我的自定义序列化器类Child1Serializer中做什么修改我试图这样做,但我无法做到,请帮助我

在序列化器中,像这样序列化日期:

def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
case x: Child1 => 
  JObject(
    JField("str", JString(x.str)) :: JField("Num", JInt(x.Num)) ::
    JField("MyList", JArray(x.MyList.map(JInt(_)))) :: 
    JField("myDate", JString(x.myDate.toString)) :: 
    JField("number", JInt(x.number)) :: Nil
  )

}

在反序列化器中,

def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), Child1] = {
case (TypeInfo(IntervalClass, _), json) => json match {
  case JObject(
    JField("str", JString(str)) :: JField("Num", JInt(num)) :: 
    JField("MyList", JArray(mylist)) :: (JField("myDate", JString(mydate)) :: 
    JField("number", JInt(number)) ::Nil)
  ) => {
    val c = Child1(
      str, num.intValue(), mylist.map(_.values.toString.toInt), LocalDateTime.parse(myDate)
    )
    c.number = number.intValue()
    c
  }
  case x => throw new MappingException("Can't convert " + x + " to Interval")
 }
}

LocalDateTime对象使用toString写入ISO格式,parse工厂方法应该能够从这样的字符串中重建对象。

您可以像这样定义LocalDateTime序列化器。

class LocalDateTimeSerializer extends Serializer[LocalDateTime] {
  private val LocalDateTimeClass = classOf[LocalDateTime]
  def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), LocalDateTime] = {
    case (TypeInfo(LocalDateTimeClass, _), json) => json match {
      case JString(dt) =>  LocalDateTime.parse(dt)
      case x => throw new MappingException("Can't convert " + x + " to LocalDateTime")
    }
  }
  def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
    case x: LocalDateTime => JString(x.toString)
  }
}

也定义你的格式,像这样

implicit val formats = DefaultFormats + new LocalDateTimeSerializer + new FieldSerializer[Child1]

请注意FieldSerializer序列化非构造函数字段的用法,number

相关内容

  • 没有找到相关文章