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

Creating codex for Map[Integer, String] #1072

Closed
iblislin opened this issue Aug 24, 2023 · 3 comments
Closed

Creating codex for Map[Integer, String] #1072

iblislin opened this issue Aug 24, 2023 · 3 comments
Labels

Comments

@iblislin
Copy link

Hi,

I got errors on using Java Integer as Map key

scala> JsonCodecMaker.make[Map[java.lang.Integer, String]]
-- Error: ----------------------------------------------------------------------
  1 |JsonCodecMaker.make[Map[java.lang.Integer, String]]
    |^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |Exception occurred while executing macro expansion.
    |java.lang.Exception: Expr cast exception: in.readKeyAsInt()
    |of type: scala.Int
    |did not conform to type: java.lang.Integer
    |
    |
    |---------------------------------------------------------------------------
    |Inline stack trace
    |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    |This location contains code that was inlined from JsonCodecMaker.scala:511
     ---------------------------------------------------------------------------

The java.lang.Long got simliar issue

scala> JsonCodecMaker.make[Map[java.lang.Long, String]]
-- Error: ----------------------------------------------------------------------
  1 |JsonCodecMaker.make[Map[java.lang.Long, String]]
    |^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...

Since I'm working with Kafka Java library, I got some Java Integer from it.

@plokhotnyuk
Copy link
Owner

@iblislin Hi, Iblis! Thanks for reporting!

I've reproduced it locally for Scala 3 (Scala 2 is not affected) and going to fix and cut a release this evening.

As a workaround you can try the following custom codec for keys:

implicit val keyCodec: JsonKeyCodec[_root_.java.lang.Integer] = new JsonKeyCodec[_root_.java.lang.Integer] {
  override def decodeKey(in: JsonReader): _root_.java.lang.Integer = _root_.java.lang.Integer.valueOf(in.readKeyAsInt())

  override def encodeKey(x: _root_.java.lang.Integer, out: JsonWriter): _root_.scala.Unit = out.writeKey(x.intValue())
}

@plokhotnyuk
Copy link
Owner

@iblislin Please peek and try the latest release with a fix: https://github.com/plokhotnyuk/jsoniter-scala/releases/tag/v2.23.3

@iblislin
Copy link
Author

I tested it, works fine for me. Thanks! 🙏

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

2 participants