0
이 시험은 내가 할 노력하고있어 설명해야합니다, 감사합니다 :lift-json 또는 json4s 사용 빈 문자열의 경우 Option [String] 필드를 None으로 매핑 할 수 있습니까?
import com.typesafe.scalalogging.StrictLogging
import net.liftweb.json._
import org.specs2.mutable.Specification
object Dummy {
case class Person(first: String, last: String, job: Option[String])
}
case object StringNotEmptySerializer extends CustomSerializer[String](format => (
{
case JString(s) => s
case JNull => null
},
{
case "" => null
case s: String => JString(s)
}
))
class JsonNoneStringTest extends Specification with StrictLogging {
implicit val formats = DefaultFormats + StringNotEmptySerializer
"Dummy" >> {
val p1 = Dummy.Person("lola", "", Some("dev"))
val j1 = compactRender(Extraction.decompose(p1))
p1.first must_== "lola"
p1.last must_== ""
p1.job must_== Some("dev")
val d2 = """{ "first": "jamie", "last": "", "job": "" }"""
val p2 = parse(d2).extract[Dummy.Person]
p2.first must_== "jamie"
p2.last must_== ""
p2.job must_== None
ok
}
}