How to use ReactiveMongo with an enum? - scala

I am working with the Play Framework and ReactiveMongo. I am trying to write a reader and a writer for my class called Platforms. I am trying to use a type that I created as scala enum, but I don't know how the reader/writer syntax should be defined. Can someone help me figure out the correct syntax?
import reactivemongo.bson._
sealed trait PlatformType { def name: String }
case object PROPER extends PlatformType { val name = "PROPER" }
case object TRANSACT extends PlatformType { val name = "TRANSACT" }
case object UPPER extends PlatformType { val name = "UPPPER" }
case class Platforms(
id: Option[BSONObjectID],
Platform: PlatformType,
Active: Boolean,
SystemIds:List[String],
creationDate: Option[DateTime],
updateDate: Option[DateTime])
object Platforms {
implicit object PlatformsBSONReader extends BSONDocumentReader[Platforms] {
def read(doc: BSONDocument): Platforms =
Platforms(
doc.getAs[BSONObjectID]("_id"),
doc.getAs[PlatformType]("Platform").get,
doc.getAs[Boolean]("Active").get,
doc.getAs[List[String]]("SystemIds").get,
doc.getAs[BSONDateTime]("creationDate").map(dt => new DateTime(dt.value)),
doc.getAs[BSONDateTime]("updateDate").map(dt => new DateTime(dt.value)))
}
implicit object PlatformsBSONWriter extends BSONDocumentWriter[Platforms] {
def write(platforms: Platforms): BSONDocument =
BSONDocument(
"_id" -> platforms.id.getOrElse(BSONObjectID.generate),
"Platform" -> platforms.Platform,
"Active" -> platforms.Active,
"SystemIds" -> platforms.SystemIds,
"creationDate" -> platforms.creationDate.map(date => BSONDateTime(date.getMillis)),
"updateDate" -> platforms.updateDate.map(date => BSONDateTime(date.getMillis)))
}
}

For PlatformType
implicit object PTW extends BSONWriter[PlatformType, BSONString] {
def write(t: PlatformType): BSONString = BSONString(n.type)
}
implicit object PTR extends BSONReader[BSONValue, PlatformType] {
def read(bson: BSONValue): PlatformType = bson match {
case BSONString("PROPER") => PROPER
// ...
}
}
There is an online documentation about the BSON readers & writers in ReactiveMongo.

Related

Play how to implement an implicit Writes or Format for a case class including an Enumeration

Following this answer shows how to bind an Enumeration to a form using a case class.
However in Play 2.7.3 this code fails with:
No Json serializer found for type jura.SearchRequest. Try to implement an implicit Writes or Format for this type.
When I implement the formatter:
object SearchRequest {
implicit val searchRequestFormat: OFormat[SearchRequest] = Json.format[SearchRequest]
}
I get
No instance of play.api.libs.json.Format is available for
scala.Enumeration.Value in the implicit scope
Should I be trying to write a formatter for the system scala.Enumeration type?
Or is there another way to implement a formatter when Enumerations are involved?
Test case here.
I use for any Enumeration this Library: enumeratum
With Dotty there will be great Enumerations, but until then I think moving to enumeratum is the best way handling Enumerations in Scala. See also Dotty - Enumerations.
As a bonus there is a play-json Extension, see Play JSON Extension.
With this your code would look like this:
import enumeratum.{ PlayJsonEnum, Enum, EnumEntry }
sealed trait SearchRequest extends EnumEntry
object SearchRequest extends Enum[SearchRequest] with PlayJsonEnum[SearchRequest] {
val values = findValues
case object SuperRequest extends SearchRequest
case object SimpleRequest extends SearchRequest
..
}
In essence PlayJsonEnum[SearchRequest] does all the work.
To write the enum as a string as cchantep says you can use Writes.enumNameWrites, we specifically use to read and write the ID. Therefore we have an EnumFormat in the package global for enums:
package object enums {
implicit def reads[E <: Enumeration](enum: E): Reads[E#Value] = new Reads[E#Value] {
def reads(json: JsValue): JsResult[E#Value] = json match {
case JsNumber(s) =>
try {
JsSuccess(enum.apply(s.toInt))
} catch {
case _: NoSuchElementException => JsError(s"Enumeration expected of type: '${enum.getClass}', but it does not appear to contain the value: '$s'")
}
case _ => JsError("Number value expected")
}
}
implicit def writes[E <: Enumeration]: Writes[E#Value] = new Writes[E#Value] {
def writes(v: E#Value): JsValue = JsNumber(v.id)
}
implicit def formatID[E <: Enumeration](enum: E): Format[E#Value] =
Format(reads(enum), writes)
def readsString[E <: Enumeration](enum: E): Reads[E#Value] = new Reads[E#Value] {
def reads(json: JsValue): JsResult[E#Value] = json match {
case JsString(s) => {
try {
JsSuccess(enum.withName(s))
} catch {
case _: NoSuchElementException => JsError(s"Enumeration expected of type: '${enum.getClass}', but it does not appear to contain the value: '$s'")
}
}
case _ => JsError("String value expected")
}
}
implicit def writesString[E <: Enumeration]: Writes[E#Value] = new Writes[E#Value] {
def writes(v: E#Value): JsValue = JsString(v.toString)
}
implicit def formatString[E <: Enumeration](enum: E): Format[E#Value] =
Format(readsString(enum), writesString)
}
And used:
object SearchRequest extends Enumeration(1) {
type SearchRequest = Value
val ONE /*1*/ , TWO /*2*/ , ETC /*n*/ = Value
implicit val searchRequestFormat: Format[SearchRequest] = formatID(SearchRequest)
}
Add the following to your Enumeration Object
implicit object MatchFilterTypeFormatter extends Formatter[MatchFilterType.Value] {
override val format = Some(("format.enum", Nil))
override def bind(key: String, data: Map[String, String]) = {
try {
Right(MatchFilterType.withName(data.get(key).head))
} catch {
case e:NoSuchElementException => Left(Seq(play.api.data.FormError(key, "Invalid MatchFilterType Enumeration")))
}
}
override def unbind(key: String, value: MatchFilterType.Value) = {
Map(key -> value.toString)
}
}
implicit val matchFilterTypeFormat = new Format[MatchFilterType.MatchFilterType] {
def reads(json: JsValue) = JsSuccess(MatchFilterType.withName(json.as[String]))
def writes(myEnum: MatchFilterType.MatchFilterType) = JsString(myEnum.toString)
}
And then the Formatter/Controller given in the question will work.
A working test case is here.

Why it does not decode to ADT type?

I am trying to derive the following string into a proper ADT type:
res6: String = {"raw":"Hello","status":{"MsgSuccess":{}}}
and using the circe library.
The ADT type looks as the following:
sealed trait MsgDoc {
}
final case class MsgPreFailure(raw: String, reasons: Chain[String]) extends MsgDoc
final case class MsgProceed(raw: String, status: MsgStatus) extends MsgDoc
and the MsgStatus type:
sealed trait MsgStatus {
}
case object MsgSuccess extends MsgStatus
final case class MsgFailure(reasons: Chain[String]) extends MsgStatus
final case class MsgUnknown(reason: String) extends MsgStatus
and the way, I've tried to drive:
object MsgDocDerivation {
import shapeless.{Coproduct, Generic}
implicit def encodeAdtNoDiscr[A, Repr <: Coproduct](implicit
gen: Generic.Aux[A, Repr],
encodeRepr: Encoder[Repr]
): Encoder[A] = encodeRepr.contramap(gen.to)
implicit def decodeAdtNoDiscr[A, Repr <: Coproduct](implicit
gen: Generic.Aux[A, Repr],
decodeRepr: Decoder[Repr]
): Decoder[A] = decodeRepr.map(gen.from)
}
and the execution:
object Main extends App {
val json = MsgProceed("Hello", MsgSuccess).asJson
println(json)
val adt = decode[MsgDoc](json.noSpaces)
println(adt)
}
as the result I've got:
{
"raw" : "Hello",
"status" : {
"MsgSuccess" : {
}
}
}
Left(DecodingFailure(CNil, List()))
As you can see, it does not decode properly.
The source code can be find https://gitlab.com/playscala/adtjson.
I'm not really sure what the MsgDocDerivation stuff is intended to do—it seems unnecessary and distracting—but I think the key problem is that circe's encoding (and decoding) is driven by static types, not the runtime class of the value being encoded (or decoded). This means that the following two JSON values will be different:
val value = MsgProceed("Hello", MsgSuccess)
val json1 = value.asJson
val json2 = (value: MsgDoc).asJson
In your case the following works just fine for me:
import cats.data.Chain
sealed trait MsgStatus
case object MsgSuccess extends MsgStatus
final case class MsgFailure(reasons: Chain[String]) extends MsgStatus
final case class MsgUnknown(reason: String) extends MsgStatus
sealed trait MsgDoc
final case class MsgPreFailure(raw: String, reasons: Chain[String]) extends MsgDoc
final case class MsgProceed(raw: String, status: MsgStatus) extends MsgDoc
import io.circe.generic.auto._, io.circe.jawn.decode, io.circe.syntax._
val value: MsgDoc = MsgProceed("Hello", MsgSuccess)
val json = value.asJson
val backToValue = decode[MsgDoc](json.noSpaces)
Note that json is different from what you were seeing:
scala> json
res0: io.circe.Json =
{
"MsgProceed" : {
"raw" : "Hello",
"status" : {
"MsgSuccess" : {
}
}
}
}
scala> backToValue
res1: Either[io.circe.Error,MsgDoc] = Right(MsgProceed(Hello,MsgSuccess))
This is because I've performed a (typesafe) upcast from MsgProceed to MsgDoc. This is typically how you work with ADTs, anyway—you're not passing around values statically typed as the case class subtypes, but rather as the sealed trait base type.
The thing is that MsgProceed as of type MsgProceed and as of type MsgDoc is encoded to different jsons. So you try to decode MsgDoc from wrong json.
val json = MsgProceed("Hello", MsgSuccess).asJson
println(json)
//{
// "raw" : "Hello",
// "status" : {
// "MsgSuccess" : {
//
// }
// }
//}
val json1 = (MsgProceed("Hello", MsgSuccess): MsgDoc).asJson
println(json1)
//{
// "MsgProceed" : {
// "raw" : "Hello",
// "status" : {
// "MsgSuccess" : {
//
// }
// }
// }
//}
val adt0 = decode[MsgProceed](json.noSpaces)
println(adt0)
//Right(MsgProceed(Hello,MsgSuccess))
val adt1 = decode[MsgDoc](json1.noSpaces)
println(adt1)
//Right(MsgProceed(Hello,MsgSuccess))
val adt = decode[MsgDoc](json.noSpaces)
println(adt)
//Left(DecodingFailure(CNil, List()))

How to create generic JSON serializer/deserializer in Scala?

I wanted to serialize and deserialize some case classes and realized I was repeating code. Unfortunately I cannot figure out a way to keep things DRY. Hoping someone can provide some assistance. Below I will provide a sample problem that is not DRY.
Sample Problem
import org.json4s.jackson.Serialization
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import org.json4s.JsonAST.JString
import org.json4s.{CustomSerializer, DefaultFormats}
case class Bar(bar: String, date: ZonedDateTime)
case class Foo(foo: String)
trait JsonParser {
private case object ZDTSerializer extends CustomSerializer[ZonedDateTime](_ => (
{ case JString(s) => ZonedDateTime.parse(s) },
{ case zdt: ZonedDateTime => JString(zdt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"))) }
))
implicit val formats = DefaultFormats + ZDTSerializer
}
object BarParser extends JsonParser {
def deserialize(jsonBar: String): Bar = {
Serialization.read[Bar](jsonBar)
}
def serialize(bar: Bar): String = {
Serialization.write[Bar](bar)
}
}
object FooParser extends JsonParser {
def deserialize(jsonFoo: String): Foo = {
Serialization.read[Foo](jsonFoo)
}
def serialize(foo: Foo): String = {
Serialization.write[Foo](foo)
}
}
object Main {
def main(args: Array[String]): Unit = {
val foo = Foo("foo")
println(FooParser.serialize(foo)) // {"foo":"foo"}
println(FooParser.deserialize(FooParser.serialize(foo))) // Foo(foo)
}
}
Above it is clear that the logic to serialize and deserialize is repeated. This is one of the things I've tried (which doesn't compile).
Attempt to Solve
case class Bar(product: String, date: ZonedDateTime)
case class Foo(title: String)
abstract class GenericJsonParser[T] {
private case object ZDTSerializer extends CustomSerializer[ZonedDateTime](_ => (
{ case JString(s) => ZonedDateTime.parse(s) },
{ case zdt: ZonedDateTime => JString(zdt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"))) }
))
implicit val formats = DefaultFormats + ZDTSerializer
def deserialize(json: String): T = {
Serialization.read[T](json) // No Manifest available for T
}
def serialize(thing: T): String = {
Serialization.write[T](thing) // type arguments [A] conform to the bounds of none of the overloaded alternatives ...
}
}
object BarJsonParser extends GenericJsonParser[Bar]
object FooParser extends GenericJsonParser[Foo]
Any guidance would be appreciated.
I think you can use Json.format[ACaseClass], for example:
import play.api.libs.json.{Format, Json}
case class ACaseClass(value: String, anotherValue: Int)
implicit val formatACaseClass = Json.format[ACaseClass]
I guess for Seralization.read and write you still have to pass an implicit value, jvm need must know how to read/write your object/

Play Framework JSON Format for Case Objects

I have a set of case objects that inherits from a trait as below:
sealed trait UserRole
case object SuperAdmin extends UserRole
case object Admin extends UserRole
case object User extends UserRole
I want to serialize this as JSON and I just used the Format mechanism:
implicit val userRoleFormat: Format[UserRole] = Json.format[UserRole]
But unfortunately, the compiler is not happy and it says:
No unapply or unapplySeq function found
What is wrong in my case objects?
Ok I figured out what has to be done!
Here it is:
implicit object UserRoleWrites extends Writes[UserRole] {
def writes(role: UserRole) = role match {
case Admin => Json.toJson("Admin")
case SuperAdmin => Json.toJson("SuperAdmin")
case User => Json.toJson("User")
}
}
Another option is to override def toString like this:
File: Status.scala
package models
trait Status
case object Active extends Status {
override def toString: String = this.productPrefix
}
case object InActive extends Status {
override def toString: String = this.productPrefix
}
this.productPrefix will give you case object name
File: Answer.scala
package models
import play.api.libs.json._
case class Answer(
id: Int,
label: String,
status: Status
) {
implicit val answerWrites = new Writes[Answer] {
def writes(answer: Answer): JsObject = Json.obj(
"id" -> answer.id,
"label" -> answer.label,
"status" -> answer.status.toString
)
}
def toJson = {
Json.toJson(this)
}
}
File: Controller.scala
import models._
val jsonAnswer = Answer(1, "Blue", Active).toJson
println(jsonAnswer)
you get:
{"id":1,"label":"Blue","status":"Active"}
Hope this helps!

Scala type class can't find implicit instances

I am trying to create a type class in Scala and use it to make a simple polymorphic case class. This example doesn't compile and gives "could not find implicit value for parameter writer:A$A228.this.ValueWriter[T]". I can't really figure out what could be going wrong or where to start.
trait Keeper
case class StringKeeper(measure: String) extends Keeper
case class StringLengthKeeper(measure: Int) extends Keeper
trait ValueWriter[A] {
def write(value: String): A
}
object DefaultValueWriters {
implicit val stringWriter = new ValueWriter[StringKeeper] {
def write(value: String) = StringKeeper(value)
}
implicit val stringLengthWriter = new ValueWriter[StringLengthKeeper] {
def write(value: String) = StringLengthKeeper(value.length)
}
}
object Write {
def toWrite[A](value: String)(implicit writer: ValueWriter[A]) = {
writer.write(value)
}
}
case class WriterOfKeepers[T <: Keeper](value: String) {
def run: T = {
Write.toWrite[T](value)
}
}
import DefaultValueWriters._
val writerLengthKeeper = WriterOfKeepers[StringLengthKeeper]("TestString")
writerLengthKeeper.run
There is no implicit ValueWriter in scope when you call Write.toWrite.
You need to have the implicit parameter in the constructor
case class WriterOfKeepers[T <: Keeper : ValueWriter](value: String) {
def run: T = {
Write.toWrite[T](value)
}
}
or in the method
case class WriterOfKeepers[T <: Keeper](value: String) {
def run(implicit writer: ValueWriter[T]): T = {
Write.toWrite(value)
}
}
or find some other way compatible with your requirements (and I don't know those).