Favored text (StringIO) over binary content for deserialization

This is also more Python 3 compatible, as the json module in
Python 3 is expecting text. Thanks Vinay Sajip for noticing it.
This commit is contained in:
Claude Paroz 2012-06-15 14:41:16 +02:00
parent fd6a9d35d9
commit 5bdd0d6b6a
2 changed files with 10 additions and 11 deletions

View File

@ -8,7 +8,6 @@ from __future__ import absolute_import
import datetime
import decimal
import json
from io import BytesIO
from django.core.serializers.base import DeserializationError
from django.core.serializers.python import Serializer as PythonSerializer
@ -63,13 +62,13 @@ def Deserializer(stream_or_string, **options):
Deserialize a stream or string of JSON data.
"""
if isinstance(stream_or_string, bytes):
stream = BytesIO(stream_or_string)
elif isinstance(stream_or_string, unicode):
stream = BytesIO(smart_str(stream_or_string))
else:
stream = stream_or_string
stream_or_string = stream_or_string.decode('utf-8')
try:
for obj in PythonDeserializer(json.load(stream), **options):
if isinstance(stream_or_string, basestring):
objects = json.loads(stream_or_string)
else:
objects = json.load(stream_or_string)
for obj in PythonDeserializer(objects, **options):
yield obj
except GeneratorExit:
raise

View File

@ -6,7 +6,7 @@ Requires PyYaml (http://pyyaml.org/), but that's checked for in __init__.
import decimal
import yaml
from io import BytesIO
from io import StringIO
from django.db import models
from django.core.serializers.base import DeserializationError
@ -51,9 +51,9 @@ def Deserializer(stream_or_string, **options):
Deserialize a stream or string of YAML data.
"""
if isinstance(stream_or_string, bytes):
stream = BytesIO(stream_or_string)
if isinstance(stream_or_string, unicode):
stream = BytesIO(smart_str(stream_or_string))
stream_or_string = stream_or_string.decode('utf-8')
if isinstance(stream_or_string, basestring):
stream = StringIO(stream_or_string)
else:
stream = stream_or_string
try: