MCPcopy Create free account
hub / github.com/simplejson/simplejson / py_encode_basestring

Function py_encode_basestring

simplejson/encoder.py:53–77  ·  view source on GitHub ↗

Return a JSON representation of a Python string

(s, _PY3=PY3, _q=u'"')

Source from the content-addressed store, hash-verified

51 _dict_types = dict
52
53def py_encode_basestring(s, _PY3=PY3, _q=u'"'):
54 """Return a JSON representation of a Python string
55
56 """
57 if _PY3:
58 if isinstance(s, bytes):
59 s = str(s, 'utf-8')
60 elif type(s) is not str:
61 # convert an str subclass instance to exact str
62 # raise a TypeError otherwise
63 s = str.__str__(s)
64 else:
65 if isinstance(s, str) and HAS_UTF8.search(s) is not None:
66 s = unicode(s, 'utf-8')
67 elif type(s) not in (str, unicode):
68 # convert an str subclass instance to exact str
69 # convert a unicode subclass instance to exact unicode
70 # raise a TypeError otherwise
71 if isinstance(s, str):
72 s = str.__str__(s)
73 else:
74 s = unicode.__getnewargs__(s)[0]
75 def replace(match):
76 return ESCAPE_DCT[match.group(0)]
77 return _q + ESCAPE.sub(replace, s) + _q
78
79
80def py_encode_basestring_ascii(s, _PY3=PY3):

Callers

nothing calls this directly

Calls 1

__str__Method · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…