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

JSONObject::quote(). Why special case for "</" but not "/" #26

Closed
budric opened this issue Aug 5, 2011 · 5 comments
Closed

JSONObject::quote(). Why special case for "</" but not "/" #26

budric opened this issue Aug 5, 2011 · 5 comments

Comments

@budric
Copy link

budric commented Aug 5, 2011

Hi,
I'm having trouble understanding this code:
case '/':
if (b == '<') {
sb.append('');
}
sb.append(c);
break;

Why is escape character '' only appended when the previous character is <. Looking at JSON control characters, the '/' is always a control character and should be prefixed with . Yet the code doesn't do that.

Thanks,

@douglascrockford
Copy link
Contributor

There is no requirement to escape /, but it may be escaped. This is useful when embedding JSON in HTML.

@budric
Copy link
Author

budric commented Aug 5, 2011

I thought the grammar: http://json.org/ required it to be escaped. It's in the same list and no different from \b, \t, , etc. I'm worried about parser interoperability between different platforms like Java generated JSON on C++...

@douglascrockford
Copy link
Contributor

You are mistaken.

@Doogiemuc
Copy link

No he is right. JsonObject DOES quote forward slashes in JsonStringer.string(String value) method.

@johnjaylward
Copy link
Contributor

@Doogiemuc the forward slash escape is technically optional in the JSON spec, which is what Douglas was talking about. The reason that this library escapes them is because embedding JSON directly into HTML can cause "remote code execution" issues when you don't escape them. We escape them by default for safety, not because it's required by the spec.

For more information see: https://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-escaped#1580682

BGehrels pushed a commit to BGehrels/JSON-java that referenced this issue Apr 29, 2020
Fixes NPE in XML for pull request stleary#160 in the json-java project
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

4 participants