Return a JSON string representation of a Python data structure. >>> from simplejson import JSONEncoder >>> JSONEncoder().encode({"foo": ["bar", "baz"]}) '{"foo": ["bar", "baz"]}'
(self, o)
| 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 |
no test coverage detected