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

Serialization inconsistent when dash is used #3

Open
mck- opened this issue Oct 29, 2013 · 4 comments
Open

Serialization inconsistent when dash is used #3

mck- opened this issue Oct 29, 2013 · 4 comments

Comments

@mck-
Copy link

mck- commented Oct 29, 2013

JSON> "{\"hello-world\" : 888}"
"{\"hello-world\" : 888}"
JSON> (decode-json-from-string *)
((:HELLO-WORLD . 888))
JSON> (encode-json-to-string *)
"{\"helloWorld\":888}"

"hello-world" becomes "helloWorld" after serialization through CL-JSON

@mck-
Copy link
Author

mck- commented Oct 29, 2013

Sorry, I just noticed this in the documentation: The functions are not strictly inverse.

Any reason why not?

@rpgoldman
Copy link
Contributor

There's some freedom here in how to translate strings from what JavaScript offers. I'd argue that there are three different alternatives:

  1. Strings. "hello-world" => "hello-world"
  2. Lisp symbols preserving original case. I.e., |hello-world| here. Some would find this awkward -- it can be cumbersome to work with literals for case-sensitive lisp symbols.
  3. Lisp symbols "rectified" to take into account the fact that CL coders tend to treat symbols as case-insensitive. Doing this, one typically wants to do something to preserve the information in theCamelCase. A common trick is to replace the camel-case boundaries with hyphens, so "helloWorld" becomes HELLO-WORLD. Unfortunately, as your example points out, "hello-world" would ''also'' become HELLO-WORLD. I see two further alternatives here:
  4. Ignore the problem. Non-lispers tend to use camel case or underscores where lispers use hyphens and tend to avoid embedded hyphens. In this alternative, you lose if there are embedded hyphens in the string, but you figure it won't happen. Alternatively, you could trap embedded hyphens and raise an exception.
  5. Resolve the ambiguity. One solution would be "hello-world" becoming HELLO--WORLD.
    Another problem with rectification is that "helloWorld" and "HelloWorld" both map to HELLO-WORLD. I guess one could map the latter to -HELLO-WORLD.

Because of this ambiguity, it will be best for programmers to explicitly specify the translation method (I have forgotten the details of the code, but I thought there was a way to specify translation methods explicitly). However, it would also be good for the default method to do something reasonable that at least mostly preserves round-tripping.

@mck-
Copy link
Author

mck- commented Oct 30, 2013

Thanks for the treatise :) For now I'll probably resort to i. to avoid trouble.. ii. would be ideal (although the -- solution is gonna collide with the "_".. so another symbol would be needed)

@aslakg
Copy link

aslakg commented Jun 28, 2021

I noticed the round-trip problem as well, caused me some headache to pin down:
(funcall cl-json:lisp-identifier-name-to-json (funcall cl-json:json-identifier-name-to-lisp "kqgbldkt2Hn7sne"))
"kqgbldkt2Hn7Sne"
The s changes to uppercase after the number 7. It's not ideal default behaviour. I am aware there are workarounds however.

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

3 participants