anshulbajpai / typesafe-ids-play-json

An integration of with

Version Matrix


An integration of typesafe-ids with play-json

Build Status Maven Central license

This library helps with play-json's Reads/Writes/Format instance derivation for a typesafe-id type.


Include typesafe-ids-play-json via SBT from maven central

libraryDependencies += "com.github.anshulbajpai" %% "typesafe-ids-play-json" % "0.1.0"

We support scala 2.11 and 2.12.

If a play-json's implicit Reads/Writes/Format instance for underlying IdType#IdValue is available in the scope, then a corresponding implicit instance for Id[IdType] will be automatically available.

  import com.github.anshulbajpai.typsafeids.core.IdType
  import com.github.anshulbajpai.typsafeids.core.Id

  trait UserId extends IdType {
    type IdValue = UUID
  val userId: Id[UserId] = Id[UserId](UUID.randomUUID())
  // Serialization
  Json.toJson(userId) // will evaluate to JsString(userId.value.toString)
  val uuid = UUID.randomUUID()
  Json.fromJson[Id[UserId]](JsString(uuid.toString)) // will evaluate to JsSuccess(Id[UserId](userId))

Please note that we had to import in the above example.

The above example work because an implicit Reads/Writes/Format instance for java.util.UUID (provided by play.api.libs.json) is present in scope.