MCPcopy
hub / github.com/pyload/pyload / encode

Method encode

module/lib/simplejson/encoder.py:204–232  ·  view source on GitHub ↗

Return a JSON string representation of a Python data structure. >>> from simplejson import JSONEncoder >>> JSONEncoder().encode({"foo": ["bar", "baz"]}) '{"foo": ["bar", "baz"]}'

(self, o)

Source from the content-addressed store, hash-verified

202 raise TypeError(repr(o) + " is not JSON serializable")
203
204 def encode(self, o):
205 """Return a JSON string representation of a Python data structure.
206
207 >>> from simplejson import JSONEncoder
208 >>> JSONEncoder().encode({"foo": ["bar", "baz"]})
209 '{"foo": ["bar", "baz"]}'
210
211 """
212 # This is for extremely simple cases and benchmarks.
213 if isinstance(o, basestring):
214 if isinstance(o, str):
215 _encoding = self.encoding
216 if (_encoding is not None
217 and not (_encoding == 'utf-8')):
218 o = o.decode(_encoding)
219 if self.ensure_ascii:
220 return encode_basestring_ascii(o)
221 else:
222 return encode_basestring(o)
223 # This doesn't pass the iterator directly to ''.join() because the
224 # exceptions aren't as detailed. The list call should be roughly
225 # equivalent to the PySequence_Fast that ''.join() would do.
226 chunks = self.iterencode(o, _one_shot=True)
227 if not isinstance(chunks, (list, tuple)):
228 chunks = list(chunks)
229 if self.ensure_ascii:
230 return ''.join(chunks)
231 else:
232 return u''.join(chunks)
233
234 def iterencode(self, o, _one_shot=False):
235 """Encode the given object and yield each string

Callers 15

getResultMethod · 0.45
fs_encodeFunction · 0.45
saveConfigMethod · 0.45
writeStringMethod · 0.45
myquoteFunction · 0.45
myurlencodeFunction · 0.45
setRequestContextMethod · 0.45
parseHeaderMethod · 0.45
handle_charrefMethod · 0.45
handle_entityrefMethod · 0.45
popMethod · 0.45
feedMethod · 0.45

Calls 4

iterencodeMethod · 0.95
encode_basestringFunction · 0.85
decodeMethod · 0.80
joinMethod · 0.80

Tested by

no test coverage detected