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

A default Map[Int,X] format is available at compile time, but fails at runtime #125

Open
acruise opened this issue Nov 14, 2014 · 14 comments
Labels

Comments

@acruise
Copy link

acruise commented Nov 14, 2014

scala> import spray.json._
scala> import spray.json.DefaultJsonProtocol._
scala> val jf = implicitly[JsonFormat[Map[Int,String]]]
jf: spray.json.JsonFormat[Map[Int,String]] = spray.json.CollectionFormats$$anon$3@2de7fe0e

scala> jf.write(Map(1 -> "Foo"))
spray.json.SerializationException: Map key must be formatted as JsString, not '1'
  at spray.json.CollectionFormats$$anon$3$$anonfun$write$3.apply(CollectionFormats.scala:53)
  at spray.json.CollectionFormats$$anon$3$$anonfun$write$3.apply(CollectionFormats.scala:50)
  at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
  at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
  at scala.collection.immutable.Map$Map1.foreach(Map.scala:116)
  at scala.collection.TraversableLike$class.map(TraversableLike.scala:245)
  at scala.collection.AbstractTraversable.map(Traversable.scala:104)
  at spray.json.CollectionFormats$$anon$3.write(CollectionFormats.scala:50)
  at spray.json.CollectionFormats$$anon$3.write(CollectionFormats.scala:48)
  ... 35 elided

scala> jf.read("""{"2": "Two"}""".parseJson)
spray.json.DeserializationException: Expected Int as JsNumber, but got "2"
  at spray.json.package$.deserializationError(package.scala:23)
  at spray.json.BasicFormats$IntJsonFormat$.read(BasicFormats.scala:29)
  at spray.json.BasicFormats$IntJsonFormat$.read(BasicFormats.scala:25)
  at spray.json.JsValue.convertTo(JsValue.scala:31)
  at spray.json.CollectionFormats$$anon$3$$anonfun$read$3.apply(CollectionFormats.scala:59)
  at spray.json.CollectionFormats$$anon$3$$anonfun$read$3.apply(CollectionFormats.scala:58)
  at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
  at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
  at scala.collection.immutable.Map$Map1.foreach(Map.scala:116)
  at scala.collection.TraversableLike$class.map(TraversableLike.scala:245)
  at scala.collection.AbstractTraversable.map(Traversable.scala:104)
  at spray.json.CollectionFormats$$anon$3.read(CollectionFormats.scala:60)
  at spray.json.CollectionFormats$$anon$3.read(CollectionFormats.scala:48)
  ... 35 elided
@sirthias
Copy link
Member

Yes, thanks for the report!

@sirthias sirthias added the Bug label Nov 17, 2014
@acruise
Copy link
Author

acruise commented Nov 17, 2014

Presumably the same applies to any old AnyVal... Using a Long as a key will be extremely common too. :)

@maciejjaskowski
Copy link

This is not a bug, this is JSON: http://json.org/

@acruise
Copy link
Author

acruise commented Apr 1, 2015

@maciejjaskowski, the bug is that Spray will find a format at compile time that cannot possibly succeed at runtime. :)

@initialcontext
Copy link

Any movement on this? I have encountered the same issue with spray-json_2.10 v1.3.2 when serializing or deserializing maps with non-String keys

@minisaw
Copy link

minisaw commented Jul 3, 2015

so annoying not being able to serialize non-String keyed maps.

@kardapoltsev
Copy link

+1

3 similar comments
@hopkirk
Copy link

hopkirk commented Nov 10, 2015

+1

@blbarker
Copy link

blbarker commented Dec 5, 2015

+1

@ppiotrow
Copy link

+1

@wlk
Copy link

wlk commented Mar 15, 2016

I stumbled on the same bug just now, this is my approach to reproduce: https://github.com/wlk/spray-serialization-bug/blob/master/src/main/scala/Main.scala

@fmarmann
Copy link

+1

@satyagraha
Copy link

If I copy the code from ancane@0b6c600 into my source and have it visible at implicit resolution time, then it gets picked up OK. If using IntelliJ I found I had to rebuild my entire project as it seems to cache stuff.

janjaali added a commit to janjaali/spray-json that referenced this issue May 19, 2020
janjaali added a commit to janjaali/spray-json that referenced this issue May 19, 2020
janjaali added a commit to janjaali/spray-json that referenced this issue May 19, 2020
@janjaali
Copy link

janjaali commented May 19, 2020

I just tried to resolve the issue and improve the extensibility of the Map format #331. It would be kind if you would take a look at it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests