mandubian / play-json-alone

Sample of Play2.2-SNAPSHOT JSON API used stand-alone
24 stars 0 forks source link

case class companion object and apply doesn't work #6

Open hgavert opened 11 years ago

hgavert commented 11 years ago

Hi!

I was able to find working version of this, "com.typesafe.play" %% "play-json" % "2.2.0-RC2", but I stumbled across a problem. This doesn't seem to compile:

import play.api.libs.json. import play.api.libs.functional.syntax.

case class Person(name: String, age: Int) object Person { def apply(p: String) = new Person(p, 0) implicit val personReads = Json.reads[Person] }

Yet, according to this: http://play.lighthouseapp.com/projects/82401/tickets/932-new-json-api-and-companion-objects it should have been fixed. Am I doing something wrong? I really think I need the apply method in the companion object.

mandubian commented 11 years ago

No I think this is normal... You override "apply" function and the macro is enable to detect that. As soon as you do that, you must write a custom Reads[T]

regards Pascal

On Wed, Sep 18, 2013 at 10:58 PM, hgavert notifications@github.com wrote:

Hi!

I was able to find working version of this, "com.typesafe.play" %% "play-json" % "2.2.0-RC2", but I stumbled across a problem. This doesn't seem to compile:

import play.api.libs.json. import play.api.libs.functional.syntax.

case class Person(name: String, age: Int) object Person { def apply(p: String) = new Person(p, 0) implicit val personReads = Json.reads[Person] }

Yet, according to this: http://play.lighthouseapp.com/projects/82401/tickets/932-new-json-api-and-companion-objectsit should have been fixed. Am I doing something wrong? I really think I need the apply method in the companion object.

— Reply to this email directly or view it on GitHubhttps://github.com/mandubian/play-json-alone/issues/6 .

hgavert commented 11 years ago

I thought there was a way to fix that? That results in large boilerplate code to be maintained. Don't really want that.

mandubian commented 11 years ago

It's hard to fix it because we would have to decide which apply you use. So some people would be happy about the choice and others wouldn't be.

For example, in your case, what apply should be used by the macro?

The best solution IMHO is to name your custom apply with another name: For ex, def default(p: String) = Person(p, 0)

On Thu, Sep 19, 2013 at 7:39 AM, hgavert notifications@github.com wrote:

I thought there was a way to fix that? That results in large boilerplate code to be maintained. Don't really want that.

— Reply to this email directly or view it on GitHubhttps://github.com/mandubian/play-json-alone/issues/6#issuecomment-24718184 .