使用JObject的Scala案例类构造

时间:2013-07-02 17:01:38

标签: scala serialization

我正在关注https://github.com/json4s/json4s/blob/master/tests/src/test/scala/org/json4s/native/SerializationExamples.scala上的示例来构建自定义序列化程序。如果有人对scala中的自定义序列化程序有任何经验,那么您的输入将非常感谢!!我收到了这个错误:

value JObject is not a case class constructor, nor does it have an unapply/unapplySeq 

方法

对于这段代码:

case JObject(JField("username", JString(s)) :: JField("email", Jstring(e)) :: Nil) => new NewUser(s, e, null, null)

有没有人遇到过类似的问题?有人知道这个案例构造函数进程是如何工作的吗?

编辑:

以下是我班级的全部代码:

import org.json4s._
import org.scalatra.json._
import org.specs2.mutable.Specification

case class NewUser(
    username: String, 
    email: String, 
    password: String, 
    confirm_password: String
)

object DataStore {
  val users = List(
      NewUser("yacomink", "yacomink.anteateaaaaar@yahoo.com", "aol123", "aol123"),
      NewUser("LadyAva", "lady_ava@excite.com", "aol456", "aol456"),
      NewUser("MoeLarryAndJesus", "moelarryandjesus@aol.com", "aol789", "aol789")
      )
}

object CustomSerialization extends Specification{
  import native.Serialization
  import native.Serialization.{read, write => swrite}
  import net.liftweb.json.JsonAST._
  import java.util.regex.Pattern
  import org.json4s.CustomSerializer

  class UserSerializer extends CustomSerializer[NewUser](format => (
      {
        case JObject(JField("username", JString(s)) :: JField("email", Jstring(e)) :: Nil) => new NewUser(s, e, null, null)
      },
      {
        case x: NewUser => JObject(JField("username", JString(x.username)) ::JField("email", JString(x.email)) :: Nil)
      }
      ))

  implicit val formats = native.Serialization.formats(NoTypeHints) + new UserSerializer   

  val i = new NewUser("Larry", "lmack@gmail.com", "lol123", "lol123")
  val ser = swrite(i)

  println(ser)
      }

0 个答案:

没有答案