Last active
May 29, 2020 15:10
-
-
Save vituchon/cebde60b4f40f4e5e232ce0261cd4b04 to your computer and use it in GitHub Desktop.
Todo el quilombo que hay que hacer para transformar a JsValue un Map
This file contains 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
case class Falopa(valor: Long, mapa: Map[String,Object]) { | |
} | |
object Falopa { | |
implicit val writes = Json.writes[Falopa] | |
implicit val reads = Json.reads[Falopa] | |
} | |
def version() = Action { | |
val falopa = new Falopa(1, Map("1" -> new Object())) | |
Ok(Json.toJson(falopa)) // falla (el intellj Idea dice: No implicit arguments of type: Writes[Falopa]) | |
} | |
Y cuando corrro desde play y lo intento hacer me pasa... | |
No Json serializer found for type controllers.Falopa. Try to implement an implicit Writes or Format for this type. | |
In /home/vituchon/development/go_workspace/src/gitlab.com/omniasalud/mvp/app/controllers/Application.scala:281 | |
278 def version = Action { | |
280 val falopa = new Falopa(1, Map("1" -> new Object())) | |
281 Ok(Json.toJson(falopa)) | |
282 } |
This file contains 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
object CookiesStore { | |
private var sessionDataBySessionId = Map[Long, CookieUserInfo]() | |
def asJson(): JsValue = { // taken and adapted from: https://stackoverflow.com/a/27286808/903998 | |
import play.api.libs.json.Json.JsValueWrapper | |
/*implicit val mapReads: Reads[Map[Long, CookieUserInfo]] = new Reads[Map[Long, CookieUserInfo]] { | |
def reads(jv: JsValue): JsResult[Map[Long, CookieUserInfo]] = | |
JsSuccess(jv.as[Map[Long, CookieUserInfo]].map{case (k, v) => | |
k -> v.asInstanceOf[CookieUserInfo] | |
}) | |
}*/ // Commented due to compilation error ("forward reference extends over definition of value mapReads") and it is not needed to read from json value to Map[Long, CookieUserInfo] | |
implicit val mapWrites: Writes[Map[Long, CookieUserInfo]] = new Writes[Map[Long, CookieUserInfo]] { | |
def writes(map: Map[Long, CookieUserInfo]): JsValue = | |
Json.obj(map.map{case (s, o) => | |
val ret: (String, JsValueWrapper) = s.toString -> Json.toJsFieldJsValueWrapper(o) | |
ret | |
}.toSeq:_*) | |
} | |
implicit val mapFormat: Format[Map[Long, CookieUserInfo]] = Format(null, mapWrites) | |
Json.toJson(sessionDataBySessionId) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment