How to enforce strict serialization of JSON in Play 2.x

前端 未结 2 832
臣服心动
臣服心动 2020-12-20 23:11

Play\'s JSON serialization is by default permissive when serializing from JSON into a case class. For example.

case class Stuff(name: String, value: Option[         


        
2条回答
  •  清酒与你
    2020-12-20 23:19

    Inspired from Travis' comment to use LabelledGeneric I was able achieve compile time safe solution.

    object toStringName extends Poly1 {
        implicit def keyToStrName[A] = at[Symbol with A](_.name)
    }
    case class Foo(bar: String, boo: Boolean)
    
    val labl = LabelledGeneric[Foo]
    val keys = Keys[labl.Repr].apply
    

    now keys.map (toStringName).toList will give you

    res0: List[String] = List(bar, boo)

提交回复
热议问题