1

我有一个名为的类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)
}

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

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

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

4

2 回答 2

1

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

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 对象使用 ISO 格式写入,toString并且parse工厂方法应该能够从这样的字符串重建对象。

于 2015-07-25T15:52:08.563 回答
0

您可以像这样定义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序列化非构造函数字段,编号

于 2015-07-26T17:36:19.203 回答