-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Play-JSON KeyReads & KeyWrites (#302)
* Refactor PlayJson EnumFormats * Provides KeyReads/Writes
- Loading branch information
Showing
7 changed files
with
231 additions
and
44 deletions.
There are no files selected for viewing
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
14 changes: 13 additions & 1 deletion
14
enumeratum-play-json/src/main/scala/enumeratum/PlayInsensitiveJsonEnum.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,20 @@ | ||
package enumeratum | ||
|
||
import play.api.libs.json.{Format, Writes} | ||
import play.api.libs.json._ | ||
|
||
trait PlayInsensitiveJsonEnum[A <: EnumEntry] { self: Enum[A] => | ||
implicit val keyWrites: KeyWrites[A] = EnumFormats.keyWrites(this) | ||
|
||
implicit def contraKeyWrites[K <: A]: KeyWrites[K] = { | ||
val w = this.keyWrites | ||
|
||
new KeyWrites[K] { | ||
def writeKey(k: K) = w.writeKey(k) | ||
} | ||
} | ||
|
||
implicit val keyReads: KeyReads[A] = EnumFormats.keyReads(this, insensitive = true) | ||
|
||
implicit val jsonFormat: Format[A] = EnumFormats.formats(this, insensitive = true) | ||
implicit def contraJsonWrites[B <: A]: Writes[B] = jsonFormat.contramap[B](b => b: A) | ||
} |
14 changes: 13 additions & 1 deletion
14
enumeratum-play-json/src/main/scala/enumeratum/PlayJsonEnum.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,20 @@ | ||
package enumeratum | ||
|
||
import play.api.libs.json.{Format, Writes} | ||
import play.api.libs.json._ | ||
|
||
trait PlayJsonEnum[A <: EnumEntry] { self: Enum[A] => | ||
implicit val keyWrites: KeyWrites[A] = EnumFormats.keyWrites(this) | ||
|
||
implicit def contraKeyWrites[K <: A]: KeyWrites[K] = { | ||
val w = this.keyWrites | ||
|
||
new KeyWrites[K] { | ||
def writeKey(k: K) = w.writeKey(k) | ||
} | ||
} | ||
|
||
implicit val keyReads: KeyReads[A] = EnumFormats.keyReads(this) | ||
|
||
implicit val jsonFormat: Format[A] = EnumFormats.formats(this) | ||
implicit def contraJsonWrites[B <: A]: Writes[B] = jsonFormat.contramap[B](b => b: A) | ||
} |
14 changes: 13 additions & 1 deletion
14
enumeratum-play-json/src/main/scala/enumeratum/PlayLowercaseJsonEnum.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,20 @@ | ||
package enumeratum | ||
|
||
import play.api.libs.json.{Format, Writes} | ||
import play.api.libs.json._ | ||
|
||
trait PlayLowercaseJsonEnum[A <: EnumEntry] { self: Enum[A] => | ||
implicit val keyWrites: KeyWrites[A] = EnumFormats.keyWritesLowercaseOnly(this) | ||
|
||
implicit def contraKeyWrites[K <: A]: KeyWrites[K] = { | ||
val w = this.keyWrites | ||
|
||
new KeyWrites[K] { | ||
def writeKey(k: K) = w.writeKey(k) | ||
} | ||
} | ||
|
||
implicit val keyReads: KeyReads[A] = EnumFormats.keyReadsLowercaseOnly(this) | ||
|
||
implicit val jsonFormat: Format[A] = EnumFormats.formatsLowerCaseOnly(this) | ||
implicit def contraJsonWrites[B <: A]: Writes[B] = jsonFormat.contramap[B](b => b: A) | ||
} |
14 changes: 13 additions & 1 deletion
14
enumeratum-play-json/src/main/scala/enumeratum/PlayUppercaseJsonEnum.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,20 @@ | ||
package enumeratum | ||
|
||
import play.api.libs.json.{Format, Writes} | ||
import play.api.libs.json._ | ||
|
||
trait PlayUppercaseJsonEnum[A <: EnumEntry] { self: Enum[A] => | ||
implicit val keyWrites: KeyWrites[A] = EnumFormats.keyWritesUppercaseOnly(this) | ||
|
||
implicit def contraKeyWrites[K <: A]: KeyWrites[K] = { | ||
val w = this.keyWrites | ||
|
||
new KeyWrites[K] { | ||
def writeKey(k: K) = w.writeKey(k) | ||
} | ||
} | ||
|
||
implicit val keyReads: KeyReads[A] = EnumFormats.keyReadsUppercaseOnly(this) | ||
|
||
implicit val jsonFormat: Format[A] = EnumFormats.formatsUppercaseOnly(this) | ||
implicit def contraJsonWrites[B <: A]: Writes[B] = jsonFormat.contramap[B](b => b: A) | ||
} |
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
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