[英]Serializing and unserializing case classes with lift-json
我正在嘗試使用lift-json進行基本序列化/水合作用,但沒有成功。 從包裝自述文件中可以看出,這應該可行。 救命?
我正在使用Scala 2.8.0和Lift 2.2為sbt交叉構建2.8(“net.liftweb”%%“lift-json”%“2.2”)。
import net.liftweb.json._
import net.liftweb.json.Serialization.{read, write}
implicit val formats = Serialization.formats(NoTypeHints)
case class Route(title: String)
val rt = new Route("x277a1")
val ser = write(rt)
// ser: String = {} ...
val deser = read[Route]("""{"title":"Some Title"}""")
// net.liftweb.json.MappingException: Parsed JSON values do not match with class constructor
Lift JSON的序列化不適用於REPL中定義的case類(paranamer無法找到字節碼來讀取類型元數據)。 使用scalac編譯路由,然后上面的示例工作。
每次(de)serialuzed類不在類路徑上時,同樣的問題也適用。 在這種情況下,paranamer無法讀取參數名稱。 有必要提供自定義ParameterNameReader。
這樣的問題適用於例如:
PlayParameterNameReader:
import net.liftweb.json.ParameterNameReader
import java.lang.reflect.Constructor
import play.classloading.enhancers.LocalvariablesNamesEnhancer
import scala.collection.JavaConversions._
object PlayParameterReader extends ParameterNameReader{
def lookupParameterNames(constructor: Constructor[_]) = LocalvariablesNamesEnhancer.lookupParameterNames(constructor)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.