spray / spray-json

A lightweight, clean and simple JSON implementation in Scala
Apache License 2.0
972 stars 190 forks source link

Option[Int] cannot convert to java.lang.Integer #159

Open nykolaslima opened 9 years ago

nykolaslima commented 9 years ago

I have a case class with a id attribute that's an Option[Int]. When I try to serialize it as a JSON I got the following error:

case class Development(id: Option[Int], ...)
development.toJson
java.lang.ClassCastException: scala.Some cannot be cast to java.lang.Integer
at scala.runtime.BoxesRunTime.unboxToInt(BoxesRunTime.java:105)
at spray.json.BasicFormats$IntJsonFormat$.write(BasicFormats.scala:25)
at spray.json.ProductFormats$class.productElement2Field(ProductFormats.scala:45)
nykolaslima commented 9 years ago

Adding NullOptions make it work but it doesn't make sense at all. If the value is None I want it to do not render the field. Besides that, in this specific case the id field has a value(Some(1)) and even with that I got the ClassCastException.

This sounds like a bug in spray-json. What do you guys think?

sirthias commented 8 years ago

This looks strange. Could you provide us with a small complete example demonstrating the problem?

A4Abhiraj commented 6 years ago

Facing the same issue.

A4Abhiraj commented 6 years ago

@nykolaslima did you manage to solve this?

zbennett10 commented 5 years ago

I solved this by deleting my local ivy2 cache and running sbt clean compile