当 scala 案例类包含 Enumeration 类型的字段时,将其转换为 json JObject 时遇到问题
在 Scala 2.8 和 liftweb.net 2.0 中,我尝试将案例类序列化为 Json,但是当案例类中有枚举时,它会失败。
import net.liftweb.json.DefaultFormats import net.liftweb.json.Extraction._ import net.liftweb.json.JsonDSL._ import net.liftweb.json.JsonAST.JObject // Enumerated type object ColorType extends scala.Enumeration { type ColorType = Value val Red, Blue, Grean = Value }
然后案例类 -
case class colorInfo (name : String, color : ColorType)
尝试转换为 JObject 的代码
val tColor = colorInfo("sienna",ColorType.Blue) implicit val formats = DefaultFormats val x = decompose(tColor).asInstanceOf[JObject]
我收到此错误:
java.lang.NoSuchFieldException: $outer at java.lang.Class.getDeclaredField(Class.java:1882) at net.liftweb.json.Extraction$$anonfun$decompose$6.apply(Extraction.scala:82) at net.liftweb.json.Extraction$$anonfun$decompose$6.apply(Extraction.scala:81) at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:206) at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:206) at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:61) at scala.collection.immutable.List.foreach(List.scala:45) at scala.collection.TraversableLike$class.map(TraversableLike.scala:206) at scala.collection.immutable.List.map(List.scala:45) at net.liftweb.json.Extraction$.decompose(Extraction.scala:81) at net.liftweb.json.Extraction$$anonfu...
In Scala 2.8 and liftweb.net 2.0 I'm trying to serialize a case-class to Json but when the case class has an enumeration in it it fails.
import net.liftweb.json.DefaultFormats import net.liftweb.json.Extraction._ import net.liftweb.json.JsonDSL._ import net.liftweb.json.JsonAST.JObject // Enumerated type object ColorType extends scala.Enumeration { type ColorType = Value val Red, Blue, Grean = Value }
Then the case class -
case class colorInfo (name : String, color : ColorType)
Code that tries to convert to JObject
val tColor = colorInfo("sienna",ColorType.Blue) implicit val formats = DefaultFormats val x = decompose(tColor).asInstanceOf[JObject]
I get this error:
java.lang.NoSuchFieldException: $outer at java.lang.Class.getDeclaredField(Class.java:1882) at net.liftweb.json.Extraction$anonfun$decompose$6.apply(Extraction.scala:82) at net.liftweb.json.Extraction$anonfun$decompose$6.apply(Extraction.scala:81) at scala.collection.TraversableLike$anonfun$map$1.apply(TraversableLike.scala:206) at scala.collection.TraversableLike$anonfun$map$1.apply(TraversableLike.scala:206) at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:61) at scala.collection.immutable.List.foreach(List.scala:45) at scala.collection.TraversableLike$class.map(TraversableLike.scala:206) at scala.collection.immutable.List.map(List.scala:45) at net.liftweb.json.Extraction$.decompose(Extraction.scala:81) at net.liftweb.json.Extraction$anonfu...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前还没有对 scala.Enumeration 的开箱即用的支持。请参阅此邮件列表线程,其中概述了添加 JSON 枚举支持的扩展:
http://groups.google.fi/group/liftweb/browse_thread/thread/d38090d804d902a3/7d7b55c4a63b2580?lnk=gst&q=json#7d7b55c4a63b2580
There's no out-of-the-box support for scala.Enumeration yet. Please see this mailing list thread which outlines an extension adding Enumeration support for JSON:
http://groups.google.fi/group/liftweb/browse_thread/thread/d38090d804d902a3/7d7b55c4a63b2580?lnk=gst&q=json#7d7b55c4a63b2580