如何在 class 中设置属性值并使用 lift json 将其转换为 json
how to set value of attribute in a class and convert it into json using lift json
我有一个名为 Child
的 class,我想使用 Lift Json 将其转换为 JSON。一切正常,但问题是我正在通过 Scala setter 设置属性的值,但这个新值未存储在 Json.
中
代码如下:
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)
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)
控制台打印的输出是:
number1
Child class converted to string{"str":"Mary","Num":5,"MyList":[1,2],"myDate":{}}
object of Child is Child1(Mary,5,List(1, 2),2015-07-24T14:04:09.266+05:00)
strMary
Num5
MyListList(1, 2)
myDate2015-07-24T14:04:09.266+05:00
number 555
为什么 obj.number
显示值 555
?它应该打印 1
.
假设 DateTime 是 Joda DateTime,您需要定义一个自定义 json 序列化器来序列化 DateTime 对象
请参阅“序列化不受支持的类型”部分here
序列化程序看起来像这样。
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
import net.liftweb.json.FieldSerializer
class JodaDateTimeSerializer extends Serializer[DateTime] {
private val JodaDateTimeClass = classOf[DateTime]
def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), DateTime] = {
case (TypeInfo(JodaDateTimeClass, _), json) => json match {
case JInt(timemillis) => new DateTime(timemillis.longValue)
case x => throw new MappingException("Can't convert " + x + " to DateTime")
}
}
def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
case x: DateTime => JInt(BigInt(x.getMillis))
}
}
也像这样定义格式
implicit val formats = DefaultFormats + new JodaDateTimeSerializer + new FieldSerializer[Child1]
请注意 FieldSerializer 序列化非构造函数字段的用法,number
我有一个名为 Child
的 class,我想使用 Lift Json 将其转换为 JSON。一切正常,但问题是我正在通过 Scala setter 设置属性的值,但这个新值未存储在 Json.
代码如下:
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)
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)
控制台打印的输出是:
number1
Child class converted to string{"str":"Mary","Num":5,"MyList":[1,2],"myDate":{}}
object of Child is Child1(Mary,5,List(1, 2),2015-07-24T14:04:09.266+05:00)
strMary
Num5
MyListList(1, 2)
myDate2015-07-24T14:04:09.266+05:00
number 555
为什么 obj.number
显示值 555
?它应该打印 1
.
假设 DateTime 是 Joda DateTime,您需要定义一个自定义 json 序列化器来序列化 DateTime 对象
请参阅“序列化不受支持的类型”部分here
序列化程序看起来像这样。
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
import net.liftweb.json.FieldSerializer
class JodaDateTimeSerializer extends Serializer[DateTime] {
private val JodaDateTimeClass = classOf[DateTime]
def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), DateTime] = {
case (TypeInfo(JodaDateTimeClass, _), json) => json match {
case JInt(timemillis) => new DateTime(timemillis.longValue)
case x => throw new MappingException("Can't convert " + x + " to DateTime")
}
}
def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
case x: DateTime => JInt(BigInt(x.getMillis))
}
}
也像这样定义格式
implicit val formats = DefaultFormats + new JodaDateTimeSerializer + new FieldSerializer[Child1]
请注意 FieldSerializer 序列化非构造函数字段的用法,number