在包含Enumeration类型的字段时,将scala案例类转换为json JObject时遇到问题

时间:2010-09-09 22:20:23

标签: json scala lift

在Scala 2.8和liftweb.net 2.0中我试图将一个case-class序列化为Json但是当case类中有一个枚举时它会失败。

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...

1 个答案:

答案 0 :(得分:1)

scala.Enumeration还没有开箱即用的支持。请参阅此邮件列表主题,其中概述了添加对JSON的枚举支持的扩展:

http://groups.google.fi/group/liftweb/browse_thread/thread/d38090d804d902a3/7d7b55c4a63b2580?lnk=gst&q=json#7d7b55c4a63b2580