I am trying to
write a play Json formatter for the enum and classes
below. I want to de-serialize a Json file to an instance of ImageDisplay
(see below). When I run the following code, Play complains that I
cannot find a deserializer for ImageDisplay[T]. I have written different
formats but none of them actually worked. Please help.
environment.resourceAsStream("cdn.images.json").flatMap { is =>
try {
// play.api.libs.json.Json.parse(IOUtils.toString(is)).as[ImageDisplay[T]]
Json.fromJson[models.ImageDisplay[T]](Json.parse(IOUtils.toByteArray(is))).asOpt
} finally {
is.close()
}
}
object Enum extends Enumeration {
val Primary = Value("primary", "0")
case class Category(name: String, val key: String) extends Val(nextId, name)
protected final def Value(name: String, key: String): Category = new Category(name, key)
}
case class BaseImage(category: Enum.Category, url: String, height: Int, width: Int, units: String, format: String)
abstract class ImageSet
case class ProductImageSet(swatchImage: BaseImage, smallImage: BaseImage, mediumImage: BaseImage, largeImage: BaseImage) extends ImageSet
case class ProfileImageSet(smallImage: BaseImage, mediumImage: BaseImage) extends ImageSet
case class ImageDisplay[T <: ImageSet](id: String, imageSets: Seq[T])--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/f383f996-03a1-4c55-af30-03e28882c260%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/CAJmgB62NKZCG%3DTidHQm-Hgy6KpfY_9ACxa0__rt_bemF1zSFYQ%40mail.gmail.com.
object ProfileImageSet {
implicit def profileImageSetFormat: Format[ProfileImageSet] = (
(__ \ "smallImage").format[BaseImage] ~
(__ \ "mediumImage").format[BaseImage])(ProfileImageSet.apply _, unlift(ProfileImageSet.unapply _))
}
object ProductImageSet {
implicit def productImageSetFormat: Format[ProductImageSet] = (
(__ \ "swatchImage").format[BaseImage] ~
(__ \ "smallImage").format[BaseImage] ~
(__ \ "mediumImage").format[BaseImage] ~
(__ \ "largeImage").format[BaseImage])(ProductImageSet.apply _, unlift(ProductImageSet.unapply _))
}
object BaseImage {
implicit def baseImageFormat: Format[BaseImage] = (
(__ \ "category").format[Enum.Category] ~
(__ \ "url").format[String] ~
(__ \ "height").format[Int] ~
(__ \ "width").format[Int] ~
(__ \ "units").format[String] ~
(__ \ "format").format[String])(BaseImage.apply, unlift(BaseImage.unapply))
}
object ImageDisplay {
implicit def imageDisplayFormat[T: Format]: Format[ImageDisplay[T]] =
((__ \ "pin").format[String] ~
(__ \ "imageSets").format[Seq[T]])(ImageDisplay.apply _, unlift(ImageDisplay.unapply _))
implicit def profileImageDisplayFormat[T: Format]: Format[ImageDisplay[ProfileImageSet]] =
((__ \ "pin").format[String] ~
(__ \ "imageSets").format[Seq[ProfileImageSet]])(ImageDisplay.apply _, unlift(ImageDisplay.unapply _))
implicit def productImageDisplayFormat[T: Format]: Format[ImageDisplay[ProductImageSet]] =
((__ \ "pin").format[String] ~
(__ \ "imageSets").format[Seq[ProductImageSet]])(ImageDisplay.apply _, unlift(ImageDisplay.unapply _))
}