com-lihaoyi / upickle

uPickle: a simple, fast, dependency-free JSON & Binary (MessagePack) serialization library for Scala
https://com-lihaoyi.github.io/upickle
MIT License
707 stars 158 forks source link

class upickle.core.Types$$anon$3 cannot be cast to class upickle.core.Types$TaggedReader #469

Closed Quafadas closed 1 year ago

Quafadas commented 1 year ago

I'm not sure about a better issue title;

There was a brief discussion on discord which lead to this gist;
scala-cli run https://gist.github.com/Quafadas/b43f74ce8473060f18aba4da49447799

As far as I can tell, the key difference is using an OptionPickler (it follows the definition from the test suite) to read JSON which is not encoded as an array. However, I am not able to infer much from the error;

Exception in thread "main" java.lang.ClassCastException: class upickle.core.Types$$anon$3 cannot be cast to class upickle.core.Types$TaggedReader (upickle.core.Types$$anon$3 and upickle.core.Types$TaggedReader are in unnamed module of loader 'app')
    at upickleOption$package$.main(b43f74ce8473060f18aba4da49447799-main/upickleOption.scala:39)
    at main.main(b43f74ce8473060f18aba4da49447799-main/upickleOption.scala:31)
lihaoyi commented 1 year ago

Removing import OptionPickler.{*, given} should fix your problem.

A minimal repro

    case class TopLevelElement() derives upickle.default.Reader
    import upickle.default.given
    val x = implicitly[upickle.default.Reader[TopLevelElement]]

To fix

    case class TopLevelElement() derives upickle.default.Reader
    val x = implicitly[upickle.default.Reader[TopLevelElement]]

In general you should not need to blanket import things like that

The error message is still a problem and should be fixed

Quafadas commented 1 year ago

Many thanks!