Last active
August 26, 2016 12:34
-
-
Save fr3akX/2c8ff35b7001a4967d62e91c2d1c0589 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait StringWrites[-A] { | |
| def writes(o: A): String | |
| } | |
| object StringWrites { | |
| def apply[A](f: A => String): StringWrites[A] = new StringWrites[A] { | |
| def writes(a: A): String = f(a) | |
| } | |
| } | |
| implicit def mapWrites[K, V](implicit kFmt: StringWrites[K], vFmt: Writes[V]) = Writes[Map[K, V]] { map => | |
| JsObject(map.map { | |
| case (k, v) => (kFmt.writes(k), vFmt.writes(v)) | |
| }.toSeq) | |
| } | |
| case class UserId(value: String) extends AnyVal | |
| implicit val userIdFmt: StringWrites[UserId] = StringWrites[UserId] { | |
| userId => userId.value | |
| } | |
| implicit val userIdJsFmt = Json.format[UserId] | |
| val map = Map(UserId("one") -> UserId("asd")) //{"one":{"value":"asd"}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment