11

Since forward slashes can only occur in strings inside a JSON serialized object and are not escaped (in the default settings), using

json.dump(some_dict).replace('/', r'\/')

reliably works, but it looks hacky.

I know that forward slashes don't have to be escaped, but you may escape them, and for my usecase I'd like to have them escaped.

Is there a way to to let the JSONEncoder escape forward slashes without manually escaping them?

3
  • 1
    Why do you need to escape them? What use case requires escaping? Any reasonable JSON decoder should be able to handle unescaped forward slashes.
    – Kevin
    Nov 25, 2014 at 14:49
  • escaping forward slashes is not required in json, so there's no reason to expect a json encoder to support escaping slashes (or any other arbitrary character).
    – Marc B
    Nov 25, 2014 at 14:51
  • 1
    dumps('</script>') == '"</script>"'. I have not encountered any problems with that , but I would sleep better if dumps('</script>') == '"<\\/script>"'. :-) I would not call the forward slash an "arbitrary character". It's one of the six ASCII characters which have an escape code though you don't have to escape them.
    – Kijewski
    Nov 25, 2014 at 14:59

2 Answers 2

10

Only escape forward slashes when encode_html_chars=True

Check out this- https://github.com/esnme/ultrajson/pull/114

The JSON spec says forward slashes shall be escaped implicitly.

Here is a solution to do it in JSONEncoder itself. Its just that you create an ESCAPE DICTIONARY and do computation before hand and do the encoding later.

https://chromium.googlesource.com/external/googleappengine/python/+/dc33addea2da464ca07e869cb11832e1ae82da9d/lib/django/django/utils/simplejson/encoder.py

Hope it helps.

-

Adding to the above solution, there is another reason to escape the characters. As kay said, it gives us some extra sleep. It prevents the attack. So the solution above takes care of all issues.

ESCAPE_DCT = {
    # escape all forward slashes to prevent </script> attack
    '/': '\\/',
    '\\': '\\\\',
    '"': '\\"',
    '\b': '\\b',
    '\f': '\\f',
    '\n': '\\n',
    '\r': '\\r',
    '\t': '\\t',
}
2
  • Thank you very much! I'll use UltraJSON.
    – Kijewski
    Nov 25, 2014 at 15:06
  • My pleasure Kay. Can my answer be marked as solution if it helped you?
    – bozzmob
    Nov 25, 2014 at 15:09
5

Use escape_forward_slashes as per ujson doc,

escape_forward_slashes Controls whether forward slashes (/) are escaped. Default is True:

>>> ujson.dumps("http://esn.me")
'"http:\/\/esn.me"'
>>> ujson.dumps("http://esn.me", escape_forward_slashes=False)
'"http://esn.me"'

See here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.