This test should explain what I'm trying to do, thanks:
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
}
}