lift json:自定义序列化器为java 8 LocalDateTime抛出映射异常



我有一个名为child2的类,我想序列化和反序列化我的类包含一个LocalDateTime属性,我必须编写一个自定义序列化器,我尝试了两个解决方案,但都抛出异常这是我的代码

解决方案1

case class Child2(var str:String,var Num:Int,MyList:List[Int],val myDate : LocalDateTime = LocalDateTime.now()){
                        var number:Int=555
                       }
class Message1SerializerDateTime  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)
  }
}
  object MessageTest extends App {
 implicit val formats = /*Serialization.formats(NoTypeHints)*/DefaultFormats + new FieldSerializer[Child2]+new Message1SerializerDateTime
        var c= new Child2("Mary", 5,List(1, 2),LocalDateTime.now())
                      c.number=1
                    //  println("number"+c.number)
              val ser = write(c)
                     println("Child class converted to string" +ser) 
                     var obj=read[Child2](ser)
                     println("object of Child is "+obj)
                     println("str"+obj.str)
                     println("Num"+obj.Num)
                     println("MyList"+obj)
                     println("myDate"+obj.myDate)
                     println("number"+obj.number)  
}

及其投掷mapping exception

Child class converted to string{"number":1,"str":"Mary","Num":5,"MyList":[1,2],"myDate":"2015-07-28T16:45:44.030"}
[error] (run-main-2) net.liftweb.json.MappingException: unknown error
net.liftweb.json.MappingException: unknown error
    at net.liftweb.json.Extraction$.extract(Extraction.scala:46)
    at net.liftweb.json.JsonAST$JValue.extract(JsonAST.scala:312)
    at net.liftweb.json.Serialization$.read(Serialization.scala:58)
    at MessageTest$.delayedEndpoint$MessageTest$1(MessageTest.scala:42)
    at MessageTest$delayedInit$body.apply(MessageTest.scala:15)
    at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App$$anonfun$main$1.apply(App.scala:76)
    at scala.App$$anonfun$main$1.apply(App.scala:76)
    at scala.collection.immutable.List.foreach(List.scala:383)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
    at scala.App$class.main(App.scala:76)
    at MessageTest$.main(MessageTest.scala:15)
    at MessageTest.main(MessageTest.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 49938
    at com.thoughtworks.paranamer.BytecodeReadingParanamer$ClassReader.<init>(BytecodeReadingParanamer.java:451)
    at com.thoughtworks.paranamer.BytecodeReadingParanamer$ClassReader.<init>(BytecodeReadingParanamer.java:431)
    at com.thoughtworks.paranamer.BytecodeReadingParanamer$ClassReader.<init>(BytecodeReadingParanamer.java:492)
    at com.thoughtworks.paranamer.BytecodeReadingParanamer$ClassReader.<init>(BytecodeReadingParanamer.java:337)
    at com.thoughtworks.paranamer.BytecodeReadingParanamer.lookupParameterNames(BytecodeReadingParanamer.java:100)
    at com.thoughtworks.paranamer.CachingParanamer.lookupParameterNames(CachingParanamer.java:75)
    at com.thoughtworks.paranamer.CachingParanamer.lookupParameterNames(CachingParanamer.java:68)
    at net.liftweb.json.Meta$ParanamerReader$.lookupParameterNames(Meta.scala:89)
    at net.liftweb.json.Meta$Reflection$.argsInfo$1(Meta.scala:237)
    at net.liftweb.json.Meta$Reflection$.constructorArgs(Meta.scala:253)
    at net.liftweb.json.Meta$Reflection$$anonfun$constructors$1.apply(Meta.scala:227)
    at net.liftweb.json.Meta$Reflection$$anonfun$constructors$1.apply(Meta.scala:227)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
    at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33)
    at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:186)
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:245)
    at scala.collection.mutable.ArrayOps$ofRef.map(ArrayOps.scala:186)
    at net.liftweb.json.Meta$Reflection$.constructors(Meta.scala:227)
    at net.liftweb.json.Meta$.net$liftweb$json$Meta$$constructors$1(Meta.scala:97)
    at net.liftweb.json.Meta$.mkConstructor$1(Meta.scala:124)
    at net.liftweb.json.Meta$.fieldMapping$1(Meta.scala:151)
    at net.liftweb.json.Meta$.net$liftweb$json$Meta$$toArg$1(Meta.scala:155)
    at net.liftweb.json.Meta$$anonfun$net$liftweb$json$Meta$$constructors$1$1$$anonfun$apply$1.apply(Meta.scala:99)
    at net.liftweb.json.Meta$$anonfun$net$liftweb$json$Meta$$constructors$1$1$$anonfun$apply$1.apply(Meta.scala:98)
    at scala.collection.immutable.List.map(List.scala:278)
    at net.liftweb.json.Meta$$anonfun$net$liftweb$json$Meta$$constructors$1$1.apply(Meta.scala:98)
    at net.liftweb.json.Meta$$anonfun$net$liftweb$json$Meta$$constructors$1$1.apply(Meta.scala:97)
    at scala.collection.immutable.List.map(List.scala:274)
    at net.liftweb.json.Meta$.net$liftweb$json$Meta$$constructors$1(Meta.scala:97)
    at net.liftweb.json.Meta$$anonfun$mappingOf$1.apply(Meta.scala:169)
    at net.liftweb.json.Meta$$anonfun$mappingOf$1.apply(Meta.scala:161)
    at net.liftweb.json.Meta$Memo.memoize(Meta.scala:199)
    at net.liftweb.json.Meta$.mappingOf(Meta.scala:161)
    at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$$mkMapping$1(Extraction.scala:194)
    at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$$extract0(Extraction.scala:199)
    at net.liftweb.json.Extraction$.extract(Extraction.scala:43)
    at net.liftweb.json.JsonAST$JValue.extract(JsonAST.scala:312)
    at net.liftweb.json.Serialization$.read(Serialization.scala:58)
    at MessageTest$.delayedEndpoint$MessageTest$1(MessageTest.scala:42)
    at MessageTest$delayedInit$body.apply(MessageTest.scala:15)
    at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App$$anonfun$main$1.apply(App.scala:76)
    at scala.App$$anonfun$main$1.apply(App.scala:76)
    at scala.collection.immutable.List.foreach(List.scala:383)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
    at scala.App$class.main(App.scala:76)
    at MessageTest$.main(MessageTest.scala:15)
    at MessageTest.main(MessageTest.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)

解决方案2

case class Child2(var str:String,var Num:Int,MyList:List[Int],val myDate : LocalDateTime = LocalDateTime.now()){
                                                                                                                                        var number:Int=555
                                                                                                                                       }
class Message1Serializer extends Serializer[Child2]{
  private val IntervalClass = classOf[Child2]
  def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), Child2] = {
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 = Child2(
      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")
 }
}
  def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
case x: Child2 => 
  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
  )
}
}
object MessageTest extends App
{
implicit val formats = /*Serialization.formats(NoTypeHints)*/DefaultFormats+new Message1Serializer
        var c= new Child2("Mary", 5,List(1, 2),LocalDateTime.now())
                      c.number=1
                    //  println("number"+c.number)
              val ser = write(c)
                     println("Child class converted to string" +ser) 
                     var obj=read[Child2](ser)
                     println("object of Child is "+obj)
                     println("str"+obj.str)
                     println("Num"+obj.Num)
                     println("MyList"+obj)
                     println("myDate"+obj.myDate)
                     println("number"+obj.number)
}

抛出映射异常

Child class converted to string{"str":"Mary","Num":5,"MyList":[1,2],"myDate":"2015-07-28T17:09:31.512","number":1}
[error] (run-main-1) net.liftweb.json.MappingException: unknown error
net.liftweb.json.MappingException: unknown error
    at net.liftweb.json.Extraction$.extract(Extraction.scala:46)
    at net.liftweb.json.JsonAST$JValue.extract(JsonAST.scala:312)
    at net.liftweb.json.Serialization$.read(Serialization.scala:58)
    at MessageTest$.delayedEndpoint$MessageTest$1(MessageTest.scala:58)
    at MessageTest$delayedInit$body.apply(MessageTest.scala:15)
    at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App$$anonfun$main$1.apply(App.scala:76)
    at scala.App$$anonfun$main$1.apply(App.scala:76)
    at scala.collection.immutable.List.foreach(List.scala:383)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
    at scala.App$class.main(App.scala:76)
    at MessageTest$.main(MessageTest.scala:15)
    at MessageTest.main(MessageTest.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 49938
    at com.thoughtworks.paranamer.BytecodeReadingParanamer$ClassReader.<init>(BytecodeReadingParanamer.java:451)
    at com.thoughtworks.paranamer.BytecodeReadingParanamer$ClassReader.<init>(BytecodeReadingParanamer.java:431)
    at com.thoughtworks.paranamer.BytecodeReadingParanamer$ClassReader.<init>(BytecodeReadingParanamer.java:492)
    at com.thoughtworks.paranamer.BytecodeReadingParanamer$ClassReader.<init>(BytecodeReadingParanamer.java:337)
    at com.thoughtworks.paranamer.BytecodeReadingParanamer.lookupParameterNames(BytecodeReadingParanamer.java:100)
    at com.thoughtworks.paranamer.CachingParanamer.lookupParameterNames(CachingParanamer.java:75)
    at com.thoughtworks.paranamer.CachingParanamer.lookupParameterNames(CachingParanamer.java:68)
    at net.liftweb.json.Meta$ParanamerReader$.lookupParameterNames(Meta.scala:89)
    at net.liftweb.json.Meta$Reflection$.argsInfo$1(Meta.scala:237)
    at net.liftweb.json.Meta$Reflection$.constructorArgs(Meta.scala:253)
    at net.liftweb.json.Meta$Reflection$$anonfun$constructors$1.apply(Meta.scala:227)
    at net.liftweb.json.Meta$Reflection$$anonfun$constructors$1.apply(Meta.scala:227)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
    at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33)
    at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:186)
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:245)
    at scala.collection.mutable.ArrayOps$ofRef.map(ArrayOps.scala:186)
    at net.liftweb.json.Meta$Reflection$.constructors(Meta.scala:227)
    at net.liftweb.json.Meta$.net$liftweb$json$Meta$$constructors$1(Meta.scala:97)
    at net.liftweb.json.Meta$.mkConstructor$1(Meta.scala:124)
    at net.liftweb.json.Meta$.fieldMapping$1(Meta.scala:151)
    at net.liftweb.json.Meta$.net$liftweb$json$Meta$$toArg$1(Meta.scala:155)
    at net.liftweb.json.Meta$$anonfun$net$liftweb$json$Meta$$constructors$1$1$$anonfun$apply$1.apply(Meta.scala:99)
    at net.liftweb.json.Meta$$anonfun$net$liftweb$json$Meta$$constructors$1$1$$anonfun$apply$1.apply(Meta.scala:98)
    at scala.collection.immutable.List.map(List.scala:278)
    at net.liftweb.json.Meta$$anonfun$net$liftweb$json$Meta$$constructors$1$1.apply(Meta.scala:98)
    at net.liftweb.json.Meta$$anonfun$net$liftweb$json$Meta$$constructors$1$1.apply(Meta.scala:97)
    at scala.collection.immutable.List.map(List.scala:274)
    at net.liftweb.json.Meta$.net$liftweb$json$Meta$$constructors$1(Meta.scala:97)
    at net.liftweb.json.Meta$$anonfun$mappingOf$1.apply(Meta.scala:169)
    at net.liftweb.json.Meta$$anonfun$mappingOf$1.apply(Meta.scala:161)
    at net.liftweb.json.Meta$Memo.memoize(Meta.scala:199)
    at net.liftweb.json.Meta$.mappingOf(Meta.scala:161)
    at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$$mkMapping$1(Extraction.scala:194)
    at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$$extract0(Extraction.scala:199)
    at net.liftweb.json.Extraction$.extract(Extraction.scala:43)
    at net.liftweb.json.JsonAST$JValue.extract(JsonAST.scala:312)
    at net.liftweb.json.Serialization$.read(Serialization.scala:58)
    at MessageTest$.delayedEndpoint$MessageTest$1(MessageTest.scala:58)
    at MessageTest$delayedInit$body.apply(MessageTest.scala:15)
    at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App$$anonfun$main$1.apply(App.scala:76)
    at scala.App$$anonfun$main$1.apply(App.scala:76)
    at scala.collection.immutable.List.foreach(List.scala:383)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
    at scala.App$class.main(App.scala:76)
    at MessageTest$.main(MessageTest.scala:15)
    at MessageTest.main(MessageTest.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)

请帮助我如何解决这个问题。我应该如何为java 8写序列化器LocalDateTime

我隐约记得有一个类似的问题,我解决了它通过解析对应的DateTimeFormat类

LocalDateTime.parse(dt, DateTimeFormatter.ofPattern("yyyy-MM-ddTHH:mm"))

为您的案例更正模式

相关内容

  • 没有找到相关文章