Implicit format for Map[UUID, CustomObject]

233 views
Skip to first unread message

Joo Lee

unread,
Jan 12, 2017, 6:12:29 AM1/12/17
to Lagom Framework Users
Hello,

I am getting an compile error "No Implicit format for Map[java.util.UUID, com.xyz.Position] available"
I tried imported the JSON Utils from the online-auction sample, and also imported the play json package in the same module but still getting the same error.

Before I write my own custom READ / WRITE formatter, I would like to know if there is an easier way to get around this because I haven't worked with play json framework before.

Thanks,

Joo

James Roper

unread,
Jan 12, 2017, 7:49:40 PM1/12/17
to Joo Lee, Lagom Framework Users
Hi Joo,

Play only defines a format for Map[String, T].  If you want to support arbitrary formats for the keys of a map, you'll have to define that yourself, eg:

implicit val uuidPositionMapFormat: Format[Map[UUID, Position]] = {
  implicitly[Format[Map[String, Position]]]
    .inmap(_.map {
      case (key, value) => UUID.fromString(key) -> value
    }, _.map {
      case (key, value) => key.toString -> value
    })
}

Regards,

James

--
You received this message because you are subscribed to the Google Groups "Lagom Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lagom-framework+unsubscribe@googlegroups.com.
To post to this group, send email to lagom-framework@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lagom-framework/e9a4dec4-d632-4760-a8b1-924d270efa5d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
James Roper
Software Engineer

Lightbend – Build reactive apps!
Twitter: @jroper

Joo Lee

unread,
Jan 12, 2017, 11:47:17 PM1/12/17
to Lagom Framework Users, joo.an...@gmail.com
Thanks James.

Did you mean to use .map instead of .inmap? It gives a compile error.


On Friday, January 13, 2017 at 8:49:40 AM UTC+8, James Roper wrote:
Hi Joo,

Play only defines a format for Map[String, T].  If you want to support arbitrary formats for the keys of a map, you'll have to define that yourself, eg:

implicit val uuidPositionMapFormat: Format[Map[UUID, Position]] = {
  implicitly[Format[Map[String, Position]]]
    .inmap(_.map {
      case (key, value) => UUID.fromString(key) -> value
    }, _.map {
      case (key, value) => key.toString -> value
    })
}

Regards,

James
On 12 January 2017 at 22:12, Joo Lee <joo.an...@gmail.com> wrote:
Hello,

I am getting an compile error "No Implicit format for Map[java.util.UUID, com.xyz.Position] available"
I tried imported the JSON Utils from the online-auction sample, and also imported the play json package in the same module but still getting the same error.

Before I write my own custom READ / WRITE formatter, I would like to know if there is an easier way to get around this because I haven't worked with play json framework before.

Thanks,

Joo

--
You received this message because you are subscribed to the Google Groups "Lagom Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lagom-framewo...@googlegroups.com.
To post to this group, send email to lagom-f...@googlegroups.com.

James Roper

unread,
Jan 16, 2017, 9:43:40 PM1/16/17
to Joo Lee, Lagom Framework Users
On 13 January 2017 at 15:47, Joo Lee <joo.an...@gmail.com> wrote:
Thanks James.

Did you mean to use .map instead of .inmap? It gives a compile error.

I did mean inmap, which is short for "invariant map", and is used to supply a function that doesn't just convert A => B, but also B => A (as is needed for Formats since they convert both to and from JSON).

What I missed was that you need to import play.api.libs.functional.syntax._ in order for it to work, as inmap is supplied via an implicit conversion.

Also, after testing myself I found that Scala's type inference is unable to infer the correct type, so you need a type annotation on the inmap call for it to work.  Here's the completed code:

import java.util.UUID
import play.api.libs.json._
import play.api.libs.functional.syntax._

implicit val uuidPositionMapFormat: Format[Map[UUID, Position]] = {
  implicitly[Format[Map[String, Position]]]
    .inmap[Map[UUID, Position]](_.map {
To unsubscribe from this group and stop receiving emails from it, send an email to lagom-framework+unsubscribe@googlegroups.com.
To post to this group, send email to lagom-framework@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lagom-framework/38b3878b-0674-4c40-8f44-f1870fa99f34%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages