Skip to content

Commit

Permalink
Favored text (StringIO) over binary content for deserialization
Browse files Browse the repository at this point in the history
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
claudep committed Jun 15, 2012
1 parent fd6a9d3 commit 5bdd0d6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
13 changes: 6 additions & 7 deletions django/core/serializers/json.py
Expand Up @@ -8,7 +8,6 @@
import datetime import datetime
import decimal import decimal
import json import json
from io import BytesIO


from django.core.serializers.base import DeserializationError from django.core.serializers.base import DeserializationError
from django.core.serializers.python import Serializer as PythonSerializer from django.core.serializers.python import Serializer as PythonSerializer
Expand Down Expand Up @@ -63,13 +62,13 @@ def Deserializer(stream_or_string, **options):
Deserialize a stream or string of JSON data. Deserialize a stream or string of JSON data.
""" """
if isinstance(stream_or_string, bytes): if isinstance(stream_or_string, bytes):
stream = BytesIO(stream_or_string) stream_or_string = stream_or_string.decode('utf-8')
elif isinstance(stream_or_string, unicode):
stream = BytesIO(smart_str(stream_or_string))
else:
stream = stream_or_string
try: 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 yield obj
except GeneratorExit: except GeneratorExit:
raise raise
Expand Down
8 changes: 4 additions & 4 deletions django/core/serializers/pyyaml.py
Expand Up @@ -6,7 +6,7 @@


import decimal import decimal
import yaml import yaml
from io import BytesIO from io import StringIO


from django.db import models from django.db import models
from django.core.serializers.base import DeserializationError from django.core.serializers.base import DeserializationError
Expand Down Expand Up @@ -51,9 +51,9 @@ def Deserializer(stream_or_string, **options):
Deserialize a stream or string of YAML data. Deserialize a stream or string of YAML data.
""" """
if isinstance(stream_or_string, bytes): if isinstance(stream_or_string, bytes):
stream = BytesIO(stream_or_string) stream_or_string = stream_or_string.decode('utf-8')
if isinstance(stream_or_string, unicode): if isinstance(stream_or_string, basestring):
stream = BytesIO(smart_str(stream_or_string)) stream = StringIO(stream_or_string)
else: else:
stream = stream_or_string stream = stream_or_string
try: try:
Expand Down

0 comments on commit 5bdd0d6

Please sign in to comment.