GithubHelp home page GithubHelp logo

Comments (9)

MasseGuillaume avatar MasseGuillaume commented on May 13, 2024 2

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.

cchantep avatar cchantep commented on May 13, 2024 2

Message is updated in #89

from play-json.

cchantep avatar cchantep commented on May 13, 2024

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:

  1. The case class Fur as-is doesn't make sense, without any property (either at least one property or a case object).
  2. A Format[Fur] is not provided, whereas required to resolve the missing Format[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.

MasseGuillaume avatar MasseGuillaume commented on May 13, 2024
  1. 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]
  1. 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.

cchantep avatar cchantep commented on May 13, 2024

... and so ? The error makes sense for me.

Please use the MailingList to ask question/for helper.

from play-json.

MasseGuillaume avatar MasseGuillaume commented on May 13, 2024

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.

cchantep avatar cchantep commented on May 13, 2024

The macros are automatically managing Optional 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 Optional 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.

cchantep avatar cchantep commented on May 13, 2024

Because the alias prevent it (probably a missing .dealias), anyway the expected behaviour is the automatic one.

from play-json.

julienrf avatar julienrf commented on May 13, 2024

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)

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.