0

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
  }
}
jpswain
  • 14,642
  • 8
  • 58
  • 63

1 Answers1

0

I asked an expert for help, and got this answer, which can be applied to any case class hierarchy!

object ObjectUtil {
  import shapeless._
  import shapeless.poly._

  object someEmptyToNone extends ->((os: Option[String]) => os flatMap (s => if (s == "") None else Some(s)))

  def transformEmptyStringsToNone[A](a: A)(implicit ev: Case1.Aux[EverywhereAux[someEmptyToNone.type], A, A]): A =
    ev(a)
}

Here's what it does:

import ObjectUtil
import com.typesafe.scalalogging.StrictLogging
import net.liftweb.json._
import org.specs2.mutable.Specification

class JsonNoneStringTest extends Specification with StrictLogging {
  "Empty string to null example" >> {
    implicit val formats = DefaultFormats
    import net.liftweb.json._

    case class Person(first: String, last: String, nickname: Option[String], address: Address)
    case class Address(street: String, unit: Option[String], city: String)

    val jamie =
      parse(
        """
          | {
          |   "first" : "jamie",
          |   "last" : "",
          |   "nickname" : "",
          |   "address" : {
          |     "street": "11111 national ave",
          |     "unit": "",
          |     "city": ""
          |   }
          | }
          | """.stripMargin
      ).extract[Person]

    jamie.last must_== ""
    jamie.nickname must_== Some("")
    jamie.address.unit must_== Some("")
    jamie.address.city must_== ""

    val jamie2 = ObjectUtil.transformEmptyStringsToNone(jamie)

    jamie2.last must_== ""
    jamie2.nickname must_== None
    jamie2.address.unit must_== None
    jamie2.address.city must_== ""
  }
}
jpswain
  • 14,642
  • 8
  • 58
  • 63