2017-05-21 13 views
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 
    } 
}

답변

0

내가 도움을 전문가를 요구하고, 어떤 경우 클래스 계층 구조에 적용 할 수있는이 답변을 얻었다! 여기

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) 
}

은 무엇이다 :

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_== "" 
    } 
}