score:8

this should do it, with the caveat that it doesn't handle numberformatexception in the reads case:

//
// scala> json.tojson(map(1 -> 2l, 2 -> 3l))
// res0: play.api.libs.json.jsvalue = {"1":2,"2":3}
//
implicit val formatter: format[map[int, long]] = {
  new format[map[int, long]] {
    def writes(m: map[int, long]) = {
      json.tojson(m.map {
        case (key, value) => key.tostring -> value
      })
    }

    def reads(json: jsvalue) = {
      json.validate[map[string, long]].map(_.map {
        case (key, value) => key.toint -> value
      })
    }
  }
}

as separate reads and writes instances:

implicit val readsinstance: reads[map[int, long]] = {
  new reads[map[int, long]] {
    def reads(json: jsvalue) = {
      json.validate[map[string, long]].map(_.map {
        case (key, value) => key.toint -> value
      })
    }
  }
}

implicit val writesinstance: writes[map[int, long]] = {
    def writes(m: map[int, long]) = {
      json.tojson(m.map {
        case (key, value) => key.tostring -> value
      })
    }
  }
}

Related Query

More Query from same tag