| 249 | class LegacyTranscoder(Transcoder): |
| 250 | |
| 251 | def encode_value(self, |
| 252 | value # type: Any |
| 253 | ) -> Tuple[bytes, int]: |
| 254 | |
| 255 | if isinstance(value, str): |
| 256 | format = FMT_UTF8 |
| 257 | elif isinstance(value, (bytes, bytearray)): |
| 258 | format = FMT_BYTES |
| 259 | elif isinstance(value, (list, tuple, dict, bool, int, float)) or value is None: |
| 260 | format = FMT_JSON |
| 261 | else: |
| 262 | format = FMT_PICKLE |
| 263 | |
| 264 | if format == FMT_BYTES: |
| 265 | if isinstance(value, bytes): |
| 266 | pass |
| 267 | elif isinstance(value, bytearray): |
| 268 | value = bytes(value) |
| 269 | else: |
| 270 | raise ValueFormatException('Expected bytes') |
| 271 | return value, format |
| 272 | elif format == FMT_UTF8: |
| 273 | return value.encode('utf-8'), format |
| 274 | elif format == FMT_PICKLE: |
| 275 | return pickle.dumps(value), FMT_PICKLE |
| 276 | else: # default to JSON |
| 277 | return json.dumps(value, ensure_ascii=False).encode('utf-8'), FMT_JSON |
| 278 | |
| 279 | def decode_value(self, |
| 280 | value, # type: bytes |