1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-09 09:13:23 +00:00
Files
vcrpy/vcr/serializers/jsonserializer.py
Aliaksandr Buhayeu 9daf301deb Fix for Serialization errors with JSON adapter
This patch aims to fix the issue#222,
where json data in request can not be serialized
because of TypeError in py3
2016-05-01 14:22:17 -10:00

30 lines
780 B
Python

try:
import simplejson as json
except ImportError:
import json
def deserialize(cassette_string):
return json.loads(cassette_string)
def serialize(cassette_dict):
error_message = (
"Does this HTTP interaction contain binary data? "
"If so, use a different serializer (like the yaml serializer) "
"for this request?"
)
try:
return json.dumps(cassette_dict, indent=4)
except UnicodeDecodeError as original: # py2
raise UnicodeDecodeError(
original.encoding,
b"Error serializing cassette to JSON",
original.start,
original.end,
original.args[-1] + error_message
)
except TypeError as original: # py3
raise TypeError(error_message)