Comments (9)
Well it's supported, here is a counter example:
import play.api.libs.json._
def dontSerialize[T](fallback: T): Format[T] = new Format[T]{
def writes(v: T): JsValue = JsNull
def reads(json: JsValue): JsResult[T] = JsSuccess(fallback)
}
def dontSerializeOption[T]: Format[Option[T]] = dontSerialize(None)
class ThisIsNotSerializable()
type WorkArround = Option[ThisIsNotSerializable]
case class Foo(v: WorkArround, field1: Int, field2: Int)
implicit val dontSerializeFur: Format[WorkArround] = dontSerializeOption[ThisIsNotSerializable]
implicit val formatFoo = Json.format[Foo]
from play-json.
Message is updated in #89
from play-json.
Hi,
In such case the details of the error is useful:
error: No instance of play.api.libs.json.Format is available for scala.Option[Fur] in the implicit scope (Hint: if declared in the same file, make sure it's declared before)
implicit val formatFoo = Json.format[Foo]
As indicated in the guide, please note that this tracker is for confirmed issue. Rather use the MailingList to ask question/for help.
It happens that:
- The case class
Fur
as-is doesn't make sense, without any property (either at least one property or acase object
). - A
Format[Fur]
is not provided, whereas required to resolve the missingFormat[Option[Fur]]
.
e.g.:
import play.api.libs.json._
def dontSerialize[T](fallback: T): Format[T] = new Format[T]{
def writes(v: T): JsValue = JsNull
def reads(json: JsValue): JsResult[T] = JsSuccess(fallback)
}
def dontSerializeOption[T]: Format[Option[T]] = dontSerialize(None)
// Does not compile
case class Fur(v: Int)
implicit val requiredFormat: Format[Fur] = Json.format[Fur]
case class Foo(v: Option[Fur])
implicit val dontSerializeFur: Format[Option[Fur]] = dontSerializeOption[Fur]
implicit val formatFoo = Json.format[Foo]
from play-json.
- Edited to make it obvious what Fur is: ThisIsNotSerializable
import play.api.libs.json._
def dontSerialize[T](fallback: T): Format[T] = new Format[T]{
def writes(v: T): JsValue = JsNull
def reads(json: JsValue): JsResult[T] = JsSuccess(fallback)
}
def dontSerializeOption[T]: Format[Option[T]] = dontSerialize(None)
class ThisIsNotSerializable()
case class Foo(v: Option[ThisIsNotSerializable], field1: Int, field2: Int)
implicit val dontSerializeFur: Format[Option[ThisIsNotSerializable]] = dontSerializeOption[ThisIsNotSerializable]
implicit val formatFoo = Json.format[Foo]
- Reported error:
No instance of play.api.libs.json.Format is available for scala.Option[Playground.ThisIsNotSerializable] in the implicit scope (Hint: if declared in the same file, make sure it's declared before)
from play-json.
... and so ? The error makes sense for me.
Please use the MailingList to ask question/for helper.
from play-json.
are you sure ?
implicit val dontSerializeThisIsNotSerializable: Format[Option[ThisIsNotSerializable]] = dontSerializeOption[ThisIsNotSerializable]
No instance of play.api.libs.json.Format is available for scala.Option[Playground.ThisIsNotSerializable]
from play-json.
The macros are automatically managing Option
al case class properties, provided that for a property whose type is Option[T]
there are typeclass (Writes
and/or Reads
) for T
.
Overriding the automatic support of Option
al properties is not supported (as would be error prone).
Either use the macro in this way, or you need to manage the whole format manually.
Please use the MailingList to ask question/for helper.
from play-json.
Because the alias prevent it (probably a missing .dealias
), anyway the expected behaviour is the automatic one.
from play-json.
Still, I think the error message is incorrect. What’s missing is a Format[ThisIsNotSerializable]
(as opposed to a Format[scala.Option[Playground.ThisIsNotSerializable]]
, like the error message says)
from play-json.
Related Issues (20)
- Scala 3 stable release HOT 16
- JacksonJson cannot be used with akka serialization as a jackson module
- Missing release for 2.9.3 HOT 1
- Binary compatibility is broken between 2.9.3 & 2.10.0-RC6 HOT 14
- pathWrites for Writes
- support jackson-core fast number parsing
- ClassCastException while executing macro expansion on Scala3 HOT 5
- `JsBoolean` on Scala 3: `productElementNames` throw an java.lang.IndexOutOfBoundsException: 0 exception HOT 4
- 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
- Difference between scala 3.3.1 and 2.13.12 when case class constructor is overridden with apply HOT 5
- "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
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.