PlayJson 2.6.6 Reads with Sealed Hierarchy

Dear all,

I have a bit pattern sealed hierarchy

sealed abstract class Type(n: Int)
case object UND      extends Type(1)
case object NULL     extends Type(2)
case object STR      extends Type(4)
case object NUM      extends Type(8)
case object NUMSTR   extends Type(16)
case object BOOL     extends Type(32)
case object EMPTYSET extends Type(64)

for which

implicit val typeReads = Json.reads[Type]

gives me the error message in the P.S. Any idea? That’s all under Scala 2.11.8 and Play 2.6.6.

TIA,
–Hossein

P.S.

Multiple markers at this line:
:black_medium_small_square:cannot handle class secloud.UND$: no case accessor
:black_medium_small_square:cannot handle class secloud.NULL$: no case accessor
:black_medium_small_square:cannot handle class secloud.STR$: no case accessor
:black_medium_small_square:cannot handle class secloud.NUM$: no case accessor
:black_medium_small_square:No instance of Reads is available for secloud.NUM in the implicit scope (Hint: if declared in the same file, make sure it’s declared before)
:black_medium_small_square:cannot handle class secloud.EMPTYSET$: no case accessor
:black_medium_small_square:cannot handle class secloud.NUMSTR$: no case accessor
:black_medium_small_square:cannot handle class secloud.BOOL$: no case accessor
:black_medium_small_square:No instance of Reads is available for secloud.NUM in the implicit scope (Hint: if declared in the same file, make sure it’s declared before)

1 Like

Have you tried implementing get method n in abstract class? If you don’t do that there your objects basically contain NO ACCESSIBLE DATA.

My code is exactly what I have shown. So, no extra get method. Now, does them being case objects not automatically generate getters for them?

You don’t get a getter for n, because is only a constructor argument, not
a field. Note that Type is not a case class. Put a val in front to make
it a field.

I see. Thanks.

Did that and I still get the same error. What now?

I don’t know. Maybe Json.reads simply does not support that kind of type
hierarchy? Maybe the following works better:

sealed trait Type { def n: Int }
case object UND extends Type { override val n: Int = 1 }

etc.

Unfortunately, that doesn’t quite change the game. Here is the new error message:

No instance of Reads is available for secloud.UND in the implicit scope (Hint: if declared in the same file, make sure it’s declared before)

Anyone from the Play Framework please? How do you guys do enum types essentially?

1 Like