389 _jsonmap[0x5c] = '\\\\' |
389 _jsonmap[0x5c] = '\\\\' |
390 _jsonmap[0x08] = '\\b' |
390 _jsonmap[0x08] = '\\b' |
391 _jsonmap[0x0c] = '\\f' |
391 _jsonmap[0x0c] = '\\f' |
392 _jsonmap[0x0d] = '\\r' |
392 _jsonmap[0x0d] = '\\r' |
393 _paranoidjsonmap = _jsonmap[:] |
393 _paranoidjsonmap = _jsonmap[:] |
|
394 _paranoidjsonmap[0x3c] = '\\u003c' # '<' (e.g. escape "</script>") |
|
395 _paranoidjsonmap[0x3e] = '\\u003e' # '>' |
394 _jsonmap.extend(chr(x) for x in xrange(128, 256)) |
396 _jsonmap.extend(chr(x) for x in xrange(128, 256)) |
395 |
397 |
396 def jsonescape(s, paranoid=False): |
398 def jsonescape(s, paranoid=False): |
397 '''returns a string suitable for JSON |
399 '''returns a string suitable for JSON |
398 |
400 |
417 >>> jsonescape('utf-8: caf\\xc3\\xa9') |
419 >>> jsonescape('utf-8: caf\\xc3\\xa9') |
418 'utf-8: caf\\xc3\\xa9' |
420 'utf-8: caf\\xc3\\xa9' |
419 >>> jsonescape('') |
421 >>> jsonescape('') |
420 '' |
422 '' |
421 |
423 |
422 If paranoid, non-ascii characters are also escaped. This is suitable for |
424 If paranoid, non-ascii and common troublesome characters are also escaped. |
423 web output. |
425 This is suitable for web output. |
424 |
426 |
425 >>> jsonescape('escape boundary: \\x7e \\x7f \\xc2\\x80', paranoid=True) |
427 >>> jsonescape('escape boundary: \\x7e \\x7f \\xc2\\x80', paranoid=True) |
426 'escape boundary: ~ \\\\u007f \\\\u0080' |
428 'escape boundary: ~ \\\\u007f \\\\u0080' |
427 >>> jsonescape('a weird byte: \\xdd', paranoid=True) |
429 >>> jsonescape('a weird byte: \\xdd', paranoid=True) |
428 'a weird byte: \\\\udcdd' |
430 'a weird byte: \\\\udcdd' |
429 >>> jsonescape('utf-8: caf\\xc3\\xa9', paranoid=True) |
431 >>> jsonescape('utf-8: caf\\xc3\\xa9', paranoid=True) |
430 'utf-8: caf\\\\u00e9' |
432 'utf-8: caf\\\\u00e9' |
431 >>> jsonescape('non-BMP: \\xf0\\x9d\\x84\\x9e', paranoid=True) |
433 >>> jsonescape('non-BMP: \\xf0\\x9d\\x84\\x9e', paranoid=True) |
432 'non-BMP: \\\\ud834\\\\udd1e' |
434 'non-BMP: \\\\ud834\\\\udd1e' |
|
435 >>> jsonescape('<foo@example.org>', paranoid=True) |
|
436 '\\\\u003cfoo@example.org\\\\u003e' |
433 ''' |
437 ''' |
434 |
438 |
435 if paranoid: |
439 if paranoid: |
436 jm = _paranoidjsonmap |
440 jm = _paranoidjsonmap |
437 else: |
441 else: |