Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProductFormats.jsonFormatN does not work with parameterized classes #34

Open
KimStebel opened this issue Nov 27, 2012 · 4 comments
Open

Comments

@KimStebel
Copy link

All jsonFormatN methods throw a MatchError if one tries to call them with a polymorphic class. Example:

case class Box[A](a:A)

jsonFormat1(Box[Int])
@sirthias
Copy link
Member

Kim,
I just added a test for this and cannot reproduce the issue.
Can you specify what spray-json version and what scala version you are running?
Can you show the MatchError stack trace?
Can you show a complete failing code snippet?

@KimStebel
Copy link
Author

I created a minimal example at https://github.com/KimStebel/spray-json-example

Scala version is 2.9.2 and spray-json version is 1.2.2.

scala.MatchError: [Ljava.lang.String;@55ab9655 (of class [Ljava.lang.String;)
at spray.json.ProductFormats$class.jsonFormat1(ProductFormats.scala:27)
at spray.json.DefaultJsonProtocol$.jsonFormat1(DefaultJsonProtocol.scala:30)
at Main$.main(Main.scala:7)
at Main.main(Main.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)

@jrudolph
Copy link
Member

The declaration case class Box[A: JsonFormat](a: A) in your example is expanded to Box(a: A)(implicit a: JsonFormat[A]). The call to jsonFormat1 then takes the implicit parameter as another constructor parameter for the box class which creates the error. It isn't possible to create JsonFormats with implicit parameters like this.

Do you really need the JsonFormat[A] instance inside of Box? What are you going to accomplish?

We have to improve the error message, though.

@KimStebel
Copy link
Author

I don't need the JsonFormat inside the case class, but I would still like to constrain the type parameter in that way. I guess I can just put the constraint on the implicit method that creates the JsonFormat for the case class, but that makes the API a little harder to understand. The use case is creating a library for CouchDB, so I need to constrain which types can be used as parameters.

A better error message would be nice.

@jrudolph jrudolph added this to the Next bugfix release milestone Mar 7, 2014
@ktoso ktoso modified the milestones: Next bugfix release, Next next Oct 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants