Skip to content
Browse files

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.
  • Loading branch information...
1 parent fd6a9d3 commit 5bdd0d6b6a8d5cfa88077c6837a21041c7bfd562 @claudep claudep committed Jun 15, 2012
Showing with 10 additions and 11 deletions.
  1. +6 −7 django/core/serializers/json.py
  2. +4 −4 django/core/serializers/pyyaml.py
View
13 django/core/serializers/json.py
@@ -8,7 +8,6 @@
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
8 django/core/serializers/pyyaml.py
@@ -6,7 +6,7 @@
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:

0 comments on commit 5bdd0d6

Please sign in to comment.
Something went wrong with that request. Please try again.