Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

only escape forward slashes when encode_html_chars=True #114

Closed
wants to merge 1 commit into from

2 participants

@ktkonrad

Addresses #110.

@jskorpan
Collaborator

The JSON spec says forward slahes shall be escaped, it's very clear on this

@jskorpan jskorpan closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 17, 2013
  1. @ktkonrad
This page is out of date. Refresh to see the latest.
Showing with 24 additions and 5 deletions.
  1. +22 −3 lib/ultrajsonenc.c
  2. +2 −2 tests/tests.py
View
25 lib/ultrajsonenc.c
@@ -183,14 +183,21 @@ int Buffer_EscapeStringUnvalidated (JSONObjectEncoder *enc, const char *io, cons
}
case '\"': (*of++) = '\\'; (*of++) = '\"'; break;
case '\\': (*of++) = '\\'; (*of++) = '\\'; break;
- case '/': (*of++) = '\\'; (*of++) = '/'; break;
+ case '/':
+ {
+ if (enc->encodeHTMLChars)
+ {
+ (*of++) = '\\';
+ }
+ (*of++) = '/'; break;
+ }
case '\b': (*of++) = '\\'; (*of++) = 'b'; break;
case '\f': (*of++) = '\\'; (*of++) = 'f'; break;
case '\n': (*of++) = '\\'; (*of++) = 'n'; break;
case '\r': (*of++) = '\\'; (*of++) = 'r'; break;
case '\t': (*of++) = '\\'; (*of++) = 't'; break;
- case 0x26: // '/'
+ case 0x26: // '&'
case 0x3c: // '<'
case 0x3e: // '>'
{
@@ -413,6 +420,19 @@ int Buffer_EscapeStringValidated (JSOBJ obj, JSONObjectEncoder *enc, const char
io ++;
continue;
}
+ case 24:
+ {
+ if (enc->encodeHTMLChars)
+ {
+ // Fall through to \u00XX case 22 below.
+ }
+ else
+ {
+ // Same as case 1 above.
+ *(of++) = (*io++);
+ continue;
+ }
+ }
case 10:
case 12:
case 14:
@@ -420,7 +440,6 @@ int Buffer_EscapeStringValidated (JSOBJ obj, JSONObjectEncoder *enc, const char
case 18:
case 20:
case 22:
- case 24:
{
*(of++) = *( (char *) (g_escapeChars + utflen + 0));
*(of++) = *( (char *) (g_escapeChars + utflen + 1));
View
4 tests/tests.py
@@ -41,7 +41,7 @@ def test_encodeDecimal(self):
def test_encodeStringConversion(self):
input = "A string \\ / \b \f \n \r \t </script> &"
- not_html_encoded = '"A string \\\\ \\/ \\b \\f \\n \\r \\t <\\/script> &"'
+ not_html_encoded = '"A string \\\\ / \\b \\f \\n \\r \\t </script> &"'
html_encoded = '"A string \\\\ \\/ \\b \\f \\n \\r \\t \\u003c\\/script\\u003e \\u0026"'
def helper(expected_output, **encode_kwargs):
@@ -174,7 +174,7 @@ def test_encodeStringConversion(self):
input = "A string \\ / \b \f \n \r \t"
output = ujson.encode(input)
self.assertEquals(input, json.loads(output))
- self.assertEquals(output, '"A string \\\\ \\/ \\b \\f \\n \\r \\t"')
+ self.assertEquals(output, '"A string \\\\ / \\b \\f \\n \\r \\t"')
self.assertEquals(input, ujson.decode(output))
def test_decodeUnicodeConversion(self):
Something went wrong with that request. Please try again.