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

DSL-JSON doesn't support scala.collection.immutable.IntMap and scala.collection.mutable.LongMap #129

Open
plokhotnyuk opened this issue Apr 9, 2019 · 2 comments

Comments

@plokhotnyuk
Copy link
Contributor

No description provided.

@plokhotnyuk plokhotnyuk changed the title DSL-JSON doesn't support scala.collection.immutable.IntMap and scala.collection.mutable.LongMap, DSL-JSON doesn't support scala.collection.immutable.IntMap and scala.collection.mutable.LongMap Apr 9, 2019
@zapov
Copy link
Member

zapov commented Apr 9, 2019

I see there are some missing arguments so this could be easily supported even from outside the lib, which I'll look to add for next version.
But I'm not sure where to draw the line between nice to have and stuff which must be supported.
Library was built to be extendable and for this example that would look like:

 def primitiveMapWriter(manifest: Type, json: DslJson[_]) = {
   manifest match {
     case pt: ParameterizedType if pt.getRawType == classOf[IntMap[_]] =>
       val valueWriter = json.tryFindWriter(pt.getActualTypeArguments.head)
       val encoder = new ScalaMapEncoder[Int, Any](
         json,
         true,
         Some(NumberConverter.INT_WRITER.asInstanceOf[JsonWriter.WriteObject[Int]]),
         Some(valueWriter.asInstanceOf[JsonWriter.WriteObject[Any]]))
       json.registerWriter(manifest, encoder)
       encoder
     case _ => null
   }
 }

dslJson.registerWriterFactory(primitiveMapWriter)

@plokhotnyuk
Copy link
Contributor Author

plokhotnyuk commented Apr 10, 2019

@zapov thanks for your help!

It isn't so easy to make it work properly for any types...
I have tried your implementation here, but it throws the following exception in runtime:

java.lang.ClassCastException: scala.Tuple2 cannot be cast to java.lang.Boolean
	at com.dslplatform.json.BoolConverter$3.write(BoolConverter.java:25)
	at com.dslplatform.json.runtime.IterableEncoder.write(IterableEncoder.scala:34)
	at com.dslplatform.json.runtime.IterableEncoder.write(IterableEncoder.scala:6)
	at com.github.plokhotnyuk.jsoniter_scala.benchmark.DslPlatformJson$.dslJsonEncode(DslPlatformJson.scala:96)
	at com.github.plokhotnyuk.jsoniter_scala.benchmark.IntMapOfBooleansBenchmark.writeDslJsonScala(IntMapOfBooleansBenchmark.scala:68)
	at com.github.plokhotnyuk.jsoniter_scala.benchmark.IntMapOfBooleansBenchmarkSpec.$anonfun$new$3(IntMapOfBooleansBenchmarkSpec.scala:26)

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

No branches or pull requests

2 participants