mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-10 17:45:35 +00:00
Since I can't think of a good way to deal with this, let's just give a nice error message to point people in the right direction. Closes #51
39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
from vcr.request import Request
|
|
from . import compat
|
|
try:
|
|
import simplejson as json
|
|
except ImportError:
|
|
import json
|
|
|
|
|
|
def _json_default(obj):
|
|
if isinstance(obj, frozenset):
|
|
return dict(obj)
|
|
return obj
|
|
|
|
|
|
def deserialize(cassette_string):
|
|
data = json.loads(cassette_string)
|
|
requests = [Request._from_dict(r['request']) for r in data]
|
|
responses = [compat.convert_to_bytes(r['response']) for r in data]
|
|
return requests, responses
|
|
|
|
|
|
def serialize(cassette_dict):
|
|
data = ([{
|
|
'request': request._to_dict(),
|
|
'response': compat.convert_to_unicode(response),
|
|
} for request, response in zip(
|
|
cassette_dict['requests'],
|
|
cassette_dict['responses']
|
|
)])
|
|
try:
|
|
return json.dumps(data, indent=4, default=_json_default)
|
|
except UnicodeDecodeError as e:
|
|
raise UnicodeDecodeError(
|
|
"Error serializing cassette to JSON. ",
|
|
"Does this HTTP interaction contain binary data? ",
|
|
"If so, use a different serializer (like the yaml serializer) for",
|
|
"this request"
|
|
)
|