sile / jsone

Erlang JSON library
MIT License
291 stars 71 forks source link

How to prevent "/" from being escaped on encode #43

Closed alexchowle closed 5 years ago

alexchowle commented 5 years ago

Even using native_utf8 option, I can't seem to prevent the encoder from escaping the forward slash.

Example:

Map = #{<<"K">> => #{<<"K1">> => <<"V1">>, <<"K2">> => <<"1/2">>}},
jsone:encode(Map, [native_utf8]).

Encoding produced for K2 is \"1\\/2\".

alexchowle commented 5 years ago

I appreciate that solidus can/should be escaped... I'm just wondering if it's possible to prevent it.

sile commented 5 years ago

Thank you for your question. Since the escaping logic of / is hardcoded here, there is no way to prevent it.

the-kenny commented 5 years ago

Just wanted to chime in that I'm running into the same issue because the generated JSON string gets hashed. I'm using jsone as it's able to preserve the order of the incoming objects.

The escaping behaviour mentioned in this issue prevents me from using the library and forcing me to fork.

Is there any reason to escape a single forward slash by default?

sile commented 5 years ago

@the-kenny

Is there any reason to escape a single forward slash by default?

The primary reason is the following (quotes from stack overflow):

JSON doesn't require you to do that, it allows you to do that. It also allows you to use "\u0061" for "A", but it's not required. Allowing \/ helps when embedding JSON in a <script> tag, which doesn't allow </ inside strings, like Seb points out.

JSON: why are forward slashes escaped?

However, I think it is good to add an option that prevents escaping "/".

sile commented 5 years ago

Created: #45