From 5bdd0d6b6a8d5cfa88077c6837a21041c7bfd562 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 15 Jun 2012 14:41:16 +0200 Subject: [PATCH] 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. --- django/core/serializers/json.py | 13 ++++++------- django/core/serializers/pyyaml.py | 8 ++++---- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/django/core/serializers/json.py b/django/core/serializers/json.py index 1b9a61750d..937d839239 100644 --- a/django/core/serializers/json.py +++ b/django/core/serializers/json.py @@ -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 diff --git a/django/core/serializers/pyyaml.py b/django/core/serializers/pyyaml.py index e490b8607d..b639ad2dae 100644 --- a/django/core/serializers/pyyaml.py +++ b/django/core/serializers/pyyaml.py @@ -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: