JanHenryNystrom / jhn_stdlib

A few thought experiments solidified as code.
Apache License 2.0
19 stars 8 forks source link

Encoding of slash results in extra backslash #39

Closed mwik closed 4 years ago

mwik commented 4 years ago

It seems that encoding a slash will get it escaped with double backslashes. Should slash be escaped at all?

1> json:encode(#{slash => <<"/">>},[maps,binary]).     
<<"{\"slash\":\"\\/\"}">>

Shouldn't it be <<"{\"slash\":\"/\"}">>?

JanHenryNystrom commented 4 years ago

That is escape by the Erlang printing not json:encode.

E.g. 3> [92]. "\"

mwik commented 4 years ago

But how come it is still there when printed with io:format? Like

1> io:format("~s~n",[json:encode(#{slash => <<"/">>},[maps,binary])]).
{"slash":"\/"}

I had expected it to be {"slash":"/"}

JanHenryNystrom commented 4 years ago

Ah, misread

Well that should be escaped as stated in the RFC 7159 on strings: "All Unicode characters may be placed within the quotation marks, except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F)."

Where reverse solidus is "/" and of course then you have the solidus escapes added by the Erlang printing.

mwik commented 4 years ago

Reverse solidus is backslash not forward slash. So slash should not be quoted.

JanHenryNystrom commented 4 years ago

Partially correct in that yes of course it is backslash that has to be quoted, however as the standard states "Any character may be escaped" so escaping solidus or froward slash is still correct JSON and at the time most encoders where doing that if I remember correctly and I went with the majority :-) I am a bit hesitant when it comes to change this, I will have a ponder.

mwik commented 4 years ago

Ah! True that. That explains why it hasn't been a problem in practice. I got to it when trying to understand why our json-formatted log output looked so garbled up with seemingly extraneous backslashes. Thanks for your effort of clearing up my confusion!

JanHenryNystrom commented 4 years ago

No problem, it is good to revisit stuff and now that I have thought about it I seem to remember that what made me include solidus amongst the escapes was that something that expected it broke without it.