[cl-json-devel] (encode-json-to-string "</div>") => "\"<\\/div>\"" ?
Boris Smilga
boris.smilga at gmail.com
Wed Jun 17 19:17:10 UTC 2009
On Tue, Jun 16, 2009 at 10:41 PM, Henrik Hjelte<henrik at evahjelte.com> wrote:
> On Tue, Jun 16, 2009 at 7:38 PM, Leslie P.
> Polzer<sky at viridian-project.de> wrote:
>>
>> Where do those two extra backslashes in front of the slash
>> come from? Is this intended?
>
> Json allows / in strings, but if I am reading the diagram on json.org
> correctly prefers that it is prefixed by a backslash
> like this \/ . Probably prefered, because why would it be in the list
> of backslashed characters otherwise? See the mentioning about solidus
> in the string diagram. I don't really know why.
The most plausible explanation is that a JavaScript embedded in an
HTML page can not include the sequence of characters LESS-THAN SIGN,
SOLIDUS: per HTML 4.01 § 18.2.1, the contents of a SCRIPT element is
CDATA which means (§ 6.2) that in the following text no markup is
interpreted—up to the first occurence of “</”. Hence, if you'd want
to put a literal string with a closing tag in your embedded script
you'd have to somehow avoid that sequence. Escaping the solidus with
the backslash seems one of the most unobtrusive means to do that. The
same rationale applies in any case where one sends JSON embedded in a
well-formed SGML document.
As to why ENCODE-JSON chooses to print the escape backslash where it
is not required to: quite simply, such implementation resulted in more
concise code with less special cases.
Yours,
- B. Smilga.
More information about the cl-json-devel
mailing list