score:0

Accepted answer
import spray.json._


case class Person(name: String)
object PersonFormat extends DefaultJsonProtocol {
  implicit val personFormat = jsonFormat1(Person)
}
import PersonFormat._

val source = """{ "name":"jack", "email":"jackt@gmail.com" }"""
source.parseJson.convertTo[Person]

You need to define the JsonProtocol with a implicit val.

score:-2

    import play.api.libs.json.Json 

    case class Sample(name:String)

    implicit val SampleJosn = Json.reads[Sample]

    val result = Json.parse("""{"name":"jack","email":"jackt@gmail.com"}""").as[Sample]

    val result2 = Json.parse("""{"name":"jack"}""").as[Sample] 

Your name will be mapped ignoring email.

See here the Fiddle: https://scalafiddle.io/sf/9nppAtI/0


Related Query

More Query from same tag