Skip to content

Instantly share code, notes, and snippets.

@gbougeard
Created April 30, 2015 15:27
Show Gist options
  • Save gbougeard/0b7972824f95aaeaa9a1 to your computer and use it in GitHub Desktop.
Save gbougeard/0b7972824f95aaeaa9a1 to your computer and use it in GitHub Desktop.
case class GHUser(
login: String,
id: Long,
avatar_url: String,
gravatar_id: Option[String],
url: String,
html_url: String,
followers_url: String,
following_url: Option[String],
gists_url: Option[String],
starred_url: String,
subscriptions_url: String,
organizations_url: String,
repos_url: String,
events_url: Option[String],
received_events_url: String,
`type`: String,
site_admin: Boolean
)
case class GHPullRequestMini(project :String,
url: String,
title: String,
state: String,
user: GHUser,
body: String,
created_at: String,
updated_at: String,
closed_at: Option[String],
merged_at: Option[String],
assignee: Option[GHUser],
branch: String
)
implicit val GHUserJsonWrite = {
import play.api.data.mapping.json.Writes._ // let's no leak implicits everywhere
Write.gen[GHUser, JsObject]
}
implicit val GHPullRequestMiniJsonWrite = {
import play.api.data.mapping.json.Writes._ // let's no leak implicits everywhere
Write.gen[GHPullRequestMini, JsObject]
}
val pullRequests:Seq[GHPullRequestMini] = ...
val json = To[Seq[GHPullRequestMini], JsObject](pullRequests)
// => could not find implicit value for parameter w: play.api.data.mapping.WriteLike[Seq[models.GHPullRequestMini],play.api.libs.json.JsObject]
@jto
Copy link

jto commented Apr 30, 2015

val json = To[Seq[GHPullRequestMini], JsArray](pullRequests)

@jto
Copy link

jto commented Apr 30, 2015

sinon

(JsPath \ "foo").write[Seq[GHPullRequestMini], JsObject].writes(pullRequests)

@jto
Copy link

jto commented Apr 30, 2015

Il faut utiliser JsValue:

scala> To[Seq[Int], JsArray](1 to 10)
<console>:17: error: could not find implicit value for parameter w: play.api.data.mapping.WriteLike[Seq[Int],play.api.libs.json.JsArray]
              To[Seq[Int], JsArray](1 to 10)

scala> To[Seq[Int], JsValue](1 to 10)
res3: play.api.libs.json.JsValue = [1,2,3,4,5,6,7,8,9,10]

@gbougeard
Copy link
Author

JsValue c'est bon pour un type simple par pour une case class j'ai l'impression.

val json = To[Seq[GHPullRequestMini], JsValue](pullRequests) // => could not find implicit value for parameter w: play.api.data.mapping.WriteLike[Seq[models.GHPullRequestMini],play.api.libs.json.JsValue]

du coup il faut que je modifie mes Write comme cela
Write.gen[GHPullRequestMini, JsValue] => value ~ is not a member of play.api.data.mapping.Write[String,play.api.libs.json.JsValue] Note: implicit value GHPullRequestMiniJsonWrite is not applicable here because it comes after the application point and it lacks an explicit result type

@jto
Copy link

jto commented Apr 30, 2015

Mouai en fait pour generer le Write il faut un JsObject car tu as besoin du Monoid sur ce type. Par contre pour la Seq i tu faut une JsValue du coup il faut que tu type explicitement GHPullRequestMiniJsonWrite

Exemple:

case class Foo(s: String, i: Int)
val foos = Seq(Foo("foo", 1))
implicit val ws: Write[Foo, JsValue] = Write.gen[Foo, JsObject]
scala> To[Seq[Foo], JsValue](foos)
res7: play.api.libs.json.JsValue = [{"s":"foo","i":1}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment