24 |
24 |
25 charencode = policy.importmod(r'charencode') |
25 charencode = policy.importmod(r'charencode') |
26 |
26 |
27 asciilower = charencode.asciilower |
27 asciilower = charencode.asciilower |
28 asciiupper = charencode.asciiupper |
28 asciiupper = charencode.asciiupper |
29 _jsonescapeu8fast = charencodepure.jsonescapeu8fast # TODO: no "pure" |
29 _jsonescapeu8fast = charencode.jsonescapeu8fast |
30 |
30 |
31 _sysstr = pycompat.sysstr |
31 _sysstr = pycompat.sysstr |
32 |
32 |
33 if pycompat.ispy3: |
33 if pycompat.ispy3: |
34 unichr = chr |
34 unichr = chr |
402 |
402 |
403 >>> jsonescape('this is a test') |
403 >>> jsonescape('this is a test') |
404 'this is a test' |
404 'this is a test' |
405 >>> jsonescape('escape characters: \\0 \\x0b \\x7f') |
405 >>> jsonescape('escape characters: \\0 \\x0b \\x7f') |
406 'escape characters: \\\\u0000 \\\\u000b \\\\u007f' |
406 'escape characters: \\\\u0000 \\\\u000b \\\\u007f' |
407 >>> jsonescape('escape characters: \\t \\n \\r \\" \\\\') |
407 >>> jsonescape('escape characters: \\b \\t \\n \\f \\r \\" \\\\') |
408 'escape characters: \\\\t \\\\n \\\\r \\\\" \\\\\\\\' |
408 'escape characters: \\\\b \\\\t \\\\n \\\\f \\\\r \\\\" \\\\\\\\' |
409 >>> jsonescape('a weird byte: \\xdd') |
409 >>> jsonescape('a weird byte: \\xdd') |
410 'a weird byte: \\xed\\xb3\\x9d' |
410 'a weird byte: \\xed\\xb3\\x9d' |
411 >>> jsonescape('utf-8: caf\\xc3\\xa9') |
411 >>> jsonescape('utf-8: caf\\xc3\\xa9') |
412 'utf-8: caf\\xc3\\xa9' |
412 'utf-8: caf\\xc3\\xa9' |
413 >>> jsonescape('') |
413 >>> jsonescape('') |
414 '' |
414 '' |
415 |
415 |
416 If paranoid, non-ascii and common troublesome characters are also escaped. |
416 If paranoid, non-ascii and common troublesome characters are also escaped. |
417 This is suitable for web output. |
417 This is suitable for web output. |
418 |
418 |
|
419 >>> s = 'escape characters: \\0 \\x0b \\x7f' |
|
420 >>> assert jsonescape(s) == jsonescape(s, paranoid=True) |
|
421 >>> s = 'escape characters: \\b \\t \\n \\f \\r \\" \\\\' |
|
422 >>> assert jsonescape(s) == jsonescape(s, paranoid=True) |
419 >>> jsonescape('escape boundary: \\x7e \\x7f \\xc2\\x80', paranoid=True) |
423 >>> jsonescape('escape boundary: \\x7e \\x7f \\xc2\\x80', paranoid=True) |
420 'escape boundary: ~ \\\\u007f \\\\u0080' |
424 'escape boundary: ~ \\\\u007f \\\\u0080' |
421 >>> jsonescape('a weird byte: \\xdd', paranoid=True) |
425 >>> jsonescape('a weird byte: \\xdd', paranoid=True) |
422 'a weird byte: \\\\udcdd' |
426 'a weird byte: \\\\udcdd' |
423 >>> jsonescape('utf-8: caf\\xc3\\xa9', paranoid=True) |
427 >>> jsonescape('utf-8: caf\\xc3\\xa9', paranoid=True) |