spray-json icon indicating copy to clipboard operation
spray-json copied to clipboard

ProductFormats.jsonFormatN does not work with parameterized classes

Open KimStebel opened this issue 13 years ago • 4 comments

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])

KimStebel avatar Nov 27 '12 20:11 KimStebel

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?

sirthias avatar Nov 28 '12 15:11 sirthias

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)

KimStebel avatar Nov 28 '12 15:11 KimStebel

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.

jrudolph avatar Nov 28 '12 16:11 jrudolph

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.

KimStebel avatar Nov 28 '12 17:11 KimStebel