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

Why '/' not valid in JSON key? #179

Open
abs opened this issue Dec 2, 2023 · 2 comments
Open

Why '/' not valid in JSON key? #179

abs opened this issue Dec 2, 2023 · 2 comments

Comments

@abs
Copy link

abs commented Dec 2, 2023

Heya!

I'm compiling some generated code that looks sort of like this:

@derive Jason.Encoder
defstruct [
:hello,
:"hello/world"
]

It's failing because of:

image

It seems that '/' is perfectly valid in a JSON key - what's the reason for having this check here?

@abs abs changed the title Why '/' not valid as a JSON key? Why '/' not valid in JSON key? Dec 2, 2023
@michalmuskala
Copy link
Owner

Because the derived or compile-time encoded keys need to be the same across all the escape options since they won't be re-escaped during encoding. In particular for escape: :html_safe the "hello/world" key would need to be escaped as "hello\/world".

@abs
Copy link
Author

abs commented Dec 3, 2023

Thanks Michał

Because the derived or compile-time encoded keys need to be the same across all the escape options since they won't be re-escaped during encoding. In particular for escape: :html_safe the "hello/world" key would need to be escaped as "hello\/world".

I have two follow-up questions:

  1. When I run this:

Jason.encode(%{"aaa/bbb" => "ccc/ddd"}, escape: :html_safe)

I get:

{:ok, "{\"aaa\\/bbb\":\"ccc\\/ddd\"}"}

Wouldn't it work the same way if "aaa/bbb" was allowed through @derive Jason.Encoder?

  1. I'm just curious whether it might make sense to make the limitation a bit more targeted - for example, as a footnote or special case to the :html_safe option - instead of forbidding all use of keys containing the / in the @derive directive?

(I'm using https://github.com/OpenAPITools/openapi-generator-cli to generate servers based on OpenAPI specifications, and if a specification happens to have a key with a slash in it, the generated Elixir code simply doesn't compile.)

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