我正在遵循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)
}