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

Snakeize should not modify key if the key is a negative number #495

saaqibz opened this issue Jan 19, 2018 · 2 comments


Copy link

commented Jan 19, 2018

json4s version 3.6

scala version 2.11

jdk version 8

When the key is a negative number, the negative(-) gets turned into an underscore (_). This test is an example:

To reproduce the error put the following test case in ParserBugs.scala and run it

    "-1 as key should remain such" in {
      implicit val formats = DefaultFormats.skippingEmptyValues
      val json = org.json4s.native.Serialization.write(Map(-1 -> true))
      val s = compact(org.json4s.native.JsonMethods.render(parse(json).snakizeKeys))
      s must_== """{"-1":true}"""

you'll see the following error where the -1 is converted to _1

'{"_1":true}' != '{"-1":true}'
Expected :{"-1":true}
Actual   :{"_1":true}
 <Click to see difference>

This comment has been minimized.

Copy link

commented Jun 29, 2019

In Json4s, if you use snakizeKeys, it does not judge whether the key is a number or not, so it will be converted to a uniform underscore.

Since snakizeKeys is specified, it can only be judged by the user code whether conversion is acceptable or not, so I think it is better to use the transformField method to perform your own conversion.


This comment has been minimized.

Copy link

commented Jul 3, 2019

Experience-wise, it's much more likely that a prepended - would not be meant to be an underscore and more likely be an issue that people have to figure out than a feature that people are glad existed.

But that said, if the priority is to keep the code simple with as few judgements as possible, then you're right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.