我正在尝试使用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
Run Code Online (Sandbox Code Playgroud) 我一直在努力通过lift-json做一些简单的事情:将地图序列化为JSON.
我知道,我知道 - "Root对象还不能是List或Map" - 但我现在愿意将它包装在一个案例类中,但我仍然无法让它工作.感谢一些堆栈溢出帮助,我有序列化工作,但我无法从字符串反序列化.我得到的错误如"没有可用的_值"和"没有关于类型的信息".
网上有其他较旧的帖子,表明类型提示是答案,但这只会导致一个不同的错误,如"不知道如何反序列化_ _".
对于Scala 2.8.0和Lift 2.2:
import net.liftweb.json._
import net.liftweb.json.Serialization.{read, write}
case class MapWrap(data: Map[String, Any])
object Scaffold {
def main(args: Array[String]) {
implicit val formats = Serialization.formats(NoTypeHints)
//implicit val formats = Serialization.formats(ShortTypeHints(List(classOf[MapWrap])))
//implicit val formats = Serialization.formats(FullTypeHints(List(classOf[MapWrap])))
val ser = write(new MapWrap(Map[String,Any]("key" -> "value")))
println("JSON: " + ser)
println(read[MapWrap](ser))
}
}
Run Code Online (Sandbox Code Playgroud)
该行println(read[MapWrap](ser))导致投诉"net.liftweb.json.MappingException:数据无可用值".
如何反序化这个case类包装器(或实现我的最终目标:read(write("key" - >"value"))))?
我正在构造一个元组并将其元素绑定到Erlang函数签名中的变量,如下所示:
store({X, Y}, State) ->
...
Run Code Online (Sandbox Code Playgroud)
但有时我需要将源元组绑定和它的内容.到目前为止,我已用额外的代码处理了这个问题:
store(Point, State) ->
{X, Y} = Point,
...
Run Code Online (Sandbox Code Playgroud)
但我是Erlang的新手,并想知道这是否天真.我的问题受到Scala提取中绑定的启发:
case point@Point(x, y) => ???
Run Code Online (Sandbox Code Playgroud)
是否有更好的方法同时绑定元组及其内容,或者最好是在单独的赋值中对元组进行解构?