Return a JSON string representation of a Python data structure. >>> from json.encoder import JSONEncoder >>> JSONEncoder().encode({"foo": ["bar", "baz"]}) '{"foo": ["bar", "baz"]}'
(self, o)
| 183 | f'is not JSON serializable') |
| 184 | |
| 185 | def encode(self, o): |
| 186 | """Return a JSON string representation of a Python data structure. |
| 187 | |
| 188 | >>> from json.encoder import JSONEncoder |
| 189 | >>> JSONEncoder().encode({"foo": ["bar", "baz"]}) |
| 190 | '{"foo": ["bar", "baz"]}' |
| 191 | |
| 192 | """ |
| 193 | # This is for extremely simple cases and benchmarks. |
| 194 | if isinstance(o, str): |
| 195 | if self.ensure_ascii: |
| 196 | return encode_basestring_ascii(o) |
| 197 | else: |
| 198 | return encode_basestring(o) |
| 199 | # This doesn't pass the iterator directly to ''.join() because the |
| 200 | # exceptions aren't as detailed. The list call should be roughly |
| 201 | # equivalent to the PySequence_Fast that ''.join() would do. |
| 202 | chunks = self.iterencode(o, _one_shot=True) |
| 203 | if not isinstance(chunks, (list, tuple)): |
| 204 | chunks = list(chunks) |
| 205 | return ''.join(chunks) |
| 206 | |
| 207 | def iterencode(self, o, _one_shot=False): |
| 208 | """Encode the given object and yield each string |
no test coverage detected