Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

play json serialisers #1

Open
prayagupa opened this issue May 10, 2018 · 0 comments
Open

play json serialisers #1

prayagupa opened this issue May 10, 2018 · 0 comments

Comments

@prayagupa
Copy link
Member

Play Serialisers

scala> import play.api.libs.json.Json._
import play.api.libs.json.Json._

scala> import play.api.libs.json._
import play.api.libs.json._

scala> final case class Android(name: String, version: String)
defined class Android

scala> final case class AiResponse[T](body: T)
defined class AiResponse

scala> implicit val androidFormat: Format[Android] = Json.format[Android]
androidFormat: play.api.libs.json.Format[Android] = play.api.libs.json.OFormat$$anon$1@2cb66c68

scala> val androidAiResponseFormat = Json.format[AiResponse[Android]]
androidAiResponseFormat: play.api.libs.json.OFormat[AiResponse[Android]] = play.api.libs.json.OFormat$$anon$1@704adf36

Serialisation

scala> Json.toJson(AiResponse(body = Android(name = "android", version = "1.0")))
res1: play.api.libs.json.JsValue = {"body":{"name":"android","version":"1.0"}}

Deserialisation

scala> Json.fromJson[AiResponse[Android]](Json.parse("""{"body":{"name":"android","version":"1.0"}}"""))
res3: play.api.libs.json.JsResult[AiResponse[Android]] = JsSuccess(AiResponse(Android(android,1.0)),)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant