diff --git a/json5/lib.py b/json5/lib.py index 9418b26..6edf531 100644 --- a/json5/lib.py +++ b/json5/lib.py @@ -14,7 +14,6 @@ import math import re -import json import sys import unicodedata @@ -244,14 +243,39 @@ def _dumpkey(k, ensure_ascii): def _dump_str(obj, ensure_ascii): - return json.dumps(obj, ensure_ascii) - single = "'" in obj - double = '"' in obj - if single and double: - return json.dumps(obj, ensure_ascii=ensure_ascii) - elif double: - return "'" + obj + "'" - return '"' + obj + '"' + ret = ['"'] + for ch in obj: + if ch == '\\': + ret.append('\\\\') + elif ch == '"': + ret.append('\\"') + elif ch == u'\u2028': + ret.append('\\u2028') + elif ch == u'\u2029': + ret.append('\\u2029') + elif ch == '\n': + ret.append('\\n') + elif ch == '\r': + ret.append('\\r') + elif ch == '\b': + ret.append('\\b') + elif ch == '\f': + ret.append('\\f') + elif ch == '\t': + ret.append('\\t') + elif ch == '\v': + ret.append('\\v') + elif ch == '\0': + ret.append('\\0') + elif not ensure_ascii: + ret.append(ch) + elif ord(ch) < 128: + ret.append(ch) + elif ord(ch) < 65536: + ret.append('\u' + '%04x' % ord(ch)) + else: + ret.append('\U' + '%08x' % ord(ch)) + return u''.join(ret) + '"' def _is_ident(k): diff --git a/json5/tests/json5_test.py b/json5/tests/json5_test.py index e0fc74a..086eeb7 100644 --- a/json5/tests/json5_test.py +++ b/json5/tests/json5_test.py @@ -260,7 +260,7 @@ def _custom_serializer(obj): def test_ensure_ascii(self): self.check(u'\u00fc', '"\\u00fc"') self.assertEquals(json5.dumps(u'\u00fc', ensure_ascii=False), - '"\u00fc"') + u'"\u00fc"') def test_numbers(self): self.check(15, '15')