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

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

Open nykolaslima opened this issue 10 years ago • 5 comments

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 avatar Jun 30 '15 20:06 nykolaslima

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?

nykolaslima avatar Jun 30 '15 21:06 nykolaslima

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

sirthias avatar Nov 02 '15 13:11 sirthias

Facing the same issue.

A4Abhiraj avatar Oct 28 '17 13:10 A4Abhiraj

@nykolaslima did you manage to solve this?

A4Abhiraj avatar Oct 28 '17 13:10 A4Abhiraj

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

zbennett10 avatar Feb 18 '19 18:02 zbennett10