Comments (5)
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.
@bwbecker Can it be related to this https://github.com/orgs/playframework/discussions/12292?
from play-json.
@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.
@cchantep or also @ramazanyich what do you think about this?
from play-json.
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)
- add hook to allow play-json users to provide their own Jackson JsonFactory or ObjectMapper
- Case Class AnyVal Not Working with Json.reads/Json.writes
- Support Scala 3 Enums HOT 2
- compile error HOT 1
- Json.writes[myScala3Enum] crashes play-json / the compilation?
- JSON field ordering bug with Scala 3 HOT 1
- Constructing Writes fail to compile with Scala 3 HOT 1
- New release for play-json that updates jackson-databind version HOT 5
- new RC pubished for Scala 3.3.0 HOT 6
- java.io.NotSerializableException on play.api.libs.json.ImmutableLinkedHashMap HOT 6
- Please can the scala 3 lib include the scala 2 macros? HOT 16
- `LowPriorityKeyReads.readableKeyReads` should not exist
- "Exception occurred while executing macro expansion." HOT 2
- Add support for type constructors in macros for scala 3 HOT 3
- Generating a format for a sealed trait return compiler warning HOT 1
- Scala 3 macro cannot find Writes for Seq[Map[String, T]] HOT 1
- As a library user I want to configure the maximum size of deserialized JSONs HOT 1
- Exception is thrown when using Reads.DefaultInstantReads with an invalid Instant form HOT 1
- possible perf issues if you upgrade to Jackson 2.17
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from play-json.