GithubHelp home page GithubHelp logo

Comments (5)

bwbecker avatar bwbecker commented on May 28, 2024

I modified the above example to use a regular class rather than a case class by adding an unapply method. The code works with Scala 2.13.12 but with Scala 3.3.1 I get the following compile error:

bwbecker@beta caseApply % scala-cli --scala 3.3.1 nonCaseApply.sc
Compiling project (Scala 3.3.1, JVM (11))
[error] ./nonCaseApply.sc:23:31
[error] Instance not found: 'Conversion[nonCaseApply_.A, _ <: Product]'
[error] implicit val fmtA:Format[A] = Json.format[A]
[error]                               ^^^^^^^^^^^^^^
Error compiling project (Scala 3.3.1, JVM (11))
Compilation failed
bwbecker@beta caseApply % 

Here's the modified code:

//> using dep     com.typesafe.play::play-json:2.10.4

import play.api.libs.json._

class A(val a:String) {
    println("A.constructor")
    override def toString:String = a
}

object A {
    def apply(a:String):A = {
        println("A.apply")
        new A(a.toUpperCase())
    }

    def unapply(t:A):Option[(String)] = {
        println("A.unapply")
        Some((t.a))
    }
}


implicit val fmtA:Format[A] = Json.format[A]

println("Instantiate A without playJson")
val a = A("lowercase")
println(s"a = ${a}")

println("\nInstantiate A using playJson")
val jsString = """{"a":"json"}"""

val b = Json.parse(jsString).as[A]
println(s"b=${b}")

from play-json.

ihostage avatar ihostage commented on May 28, 2024

@bwbecker Can it be related to this https://github.com/orgs/playframework/discussions/12292?

from play-json.

bwbecker avatar bwbecker commented on May 28, 2024

@ihostage, thanks for the reply. That might be part of the underlying cause. I'm not familiar with the playJson codebase, so I don't know.

A work-around is to define a Reads myself, similar to the one at https://www.playframework.com/documentation/2.8.x/ScalaJsonCombinators#Complex-Reads

Luckily, I have only one where this is causing me a problem. If I had many, it would be painful.

from play-json.

mkurz avatar mkurz commented on May 28, 2024

@cchantep or also @ramazanyich what do you think about this?

from play-json.

cchantep avatar cchantep commented on May 28, 2024

apply/unapply are not used in Scala 3 derivation/macros.
The class must either extend Product (as any case class), or Conversion must be available.
Documentation can be updated.

from play-json.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.