How to write Reads[T] and Writes[T] in scala Enumeration (play framework 2.1)
By : Benny
Date : March 29 2020, 07:55 AM
I wish this helpful for you Short answer: use something like Play Enumeration Utils. Long answer, instead of putting a Reads in your enum, you can create a re-useable Reads for Enumeration types: code :
object EnumA extends Enumeration {
type EnumA = Value
val VAL1, VAL2, VAL3 = Value
}
object EnumUtils {
def enumReads[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")
}
}
}
import some.thing.EnumUtils
implicit val myEnumReads: Reads[EnumA.Value] = EnumUtils.enumReads(EnumA)
val myValue: EnumA.Value = someJsonObject.as[EnumA.Value]
val myValue: EnumA.Value = someJsonObject.asOpt[EnumA.Value].getOrElse(sys.error("Oh noes! Invalid value!"))
object EnumUtils {
...
implicit def enumWrites[E <: Enumeration]: Writes[E#Value] = new Writes[E#Value] {
def writes(v: E#Value): JsValue = JsString(v.toString)
}
}
import EnumUtils.enumWrites
val myEnumJson: JsValue = Json.toJson(EnumA.VAL1)
object EnumUtils {
....
implicit def enumFormat[E <: Enumeration](enum: E): Format[E#Value] = {
Format(EnumReader.enumReads(enum), EnumWriter.enumWrites)
}
}
|
Generic way to implement a Pk Writes in Play Scala
By : Peaceful_Warrior
Date : March 29 2020, 07:55 AM
will be helpful for those in need The only thing you need to do is ensuring that there is deserializer for type E in scope, because now you can't be sure that each type you use is writeable. You can try it: code :
class Value[A](value: A){
def get[A] = value
}
implicit def valueWrites[E](implicit longWrites: Writes[E]) = new Writes[Value[E]]{
def writes(value: Value[E]): JsValue = Json.toJson(value.get)
}
Json.toJson(new Value(5L))
implicit def valueToJson[E: Writes] = new Writes[Value[E]]{
def writes(value: Value[E]): JsValue = Json.toJson(value.get)
}
|
Serialize set to json with a custom Writes in Scala Play 2.4
By : karoles
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You are reimplementing writes for traversable that are trivial anyway but still. Another option is to reuse that Writes code :
val users: Set[User] = ???
Json.toJson(users)(Writes.traversableWrites(userSafeWrites))
implicit def traversableWrites[A: Writes] = Writes[Traversable[A]] { as =>
JsArray(as.map(toJson(_)).toSeq)
}
case class User(num: Int)
object User {
implicit val writes = Json.writes[User]
}
object OtherWrites {
implicit val custom: Writes[User] = new Writes[User] {
override def writes(o: User): JsValue = JsNull
}
}
object Client extends App {
val obj = List(User(1))
print(Json.toJson(obj))
}
object Client extends App {
import test.OtherWrites._
val obj = List(User(1))
print(Json.toJson(obj))
}
|
Scala Compiler (2.11.7) anomaly with Play JSON Writes
By : Sunil kumar
Date : March 29 2020, 07:55 AM
Hope this helps I am pretty sure the issue comes from .getOrElse(JsNull) I have successfully compiled this code: code :
import play.api.libs.json.{JsNull, Json, JsValue, Writes}
case class Cost(cost: Option[Double])
case object Cost {
def writes = new Writes[Cost] {
override def writes(r: Cost): JsValue = {
Json.obj(
"cost" -> r.cost.map(Json.toJson(_))
)
}
}
}
scala> Cost(Some(5))
res2: Cost = Cost(Some(5.0))
scala> Json.toJson(res2)(Cost.writes)
res5: play.api.libs.json.JsValue = {"cost":5}
val cost = r.cost.map(t => Json.toJson(t))
Json.obj(
"cost" -> cost
)
cost.getOrElse[JsValue](JsNull)
cost.getOrElse(JsNull).asInstanceOf[JsValue]
[error] (...) type mismatch;
[error] found : Object
[error] required: play.api.libs.json.Json.JsValueWrapper
|
Scala Play Json implicit writes type mismatch
By : Jared Weinfurtner
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You're trying to serialize the type Subscription, rather than the request body, which you stored as the value subscription. Try replacing the last line with Ok(Json.toJson(subscription)).
|