commit c37a6d68b15dc6d8f217e384e9a95ee2fa8ef226
Author: Claude Paroz <claude@2xlibre.net>
Date: Tue May 10 22:34:41 2011 +0200
Test JSON decoding error
diff --git a/django/core/serializers/json.py b/django/core/serializers/json.py
index 7b570f3..3e2febd 100644
|
a
|
b
|
from StringIO import StringIO
|
| 8 | 8 | |
| 9 | 9 | from django.core.serializers.python import Serializer as PythonSerializer |
| 10 | 10 | from django.core.serializers.python import Deserializer as PythonDeserializer |
| | 11 | from django.core.serializers import base |
| 11 | 12 | from django.utils import simplejson |
| 12 | 13 | from django.utils.timezone import is_aware |
| 13 | 14 | |
| … |
… |
class Serializer(PythonSerializer):
|
| 27 | 28 | if callable(getattr(self.stream, 'getvalue', None)): |
| 28 | 29 | return self.stream.getvalue() |
| 29 | 30 | |
| | 31 | |
| 30 | 32 | def Deserializer(stream_or_string, **options): |
| 31 | 33 | """ |
| 32 | 34 | Deserialize a stream or string of JSON data. |
| … |
… |
def Deserializer(stream_or_string, **options):
|
| 35 | 37 | stream = StringIO(stream_or_string) |
| 36 | 38 | else: |
| 37 | 39 | stream = stream_or_string |
| 38 | | for obj in PythonDeserializer(simplejson.load(stream), **options): |
| 39 | | yield obj |
| | 40 | try: |
| | 41 | for obj in PythonDeserializer(simplejson.load(stream), **options): |
| | 42 | yield obj |
| | 43 | except ValueError, e: |
| | 44 | # Map to deserializer error |
| | 45 | raise base.DeserializationError(e.args) |
| | 46 | |
| 40 | 47 | |
| 41 | 48 | class DjangoJSONEncoder(simplejson.JSONEncoder): |
| 42 | 49 | """ |
diff --git a/tests/regressiontests/serializers_regress/tests.py b/tests/regressiontests/serializers_regress/tests.py
index 9c9022d..318fd2e 100644
|
a
|
b
|
except ImportError:
|
| 19 | 19 | |
| 20 | 20 | from django.core import serializers |
| 21 | 21 | from django.core.serializers import SerializerDoesNotExist |
| | 22 | from django.core.serializers.base import DeserializationError |
| 22 | 23 | from django.db import connection, models |
| 23 | 24 | from django.test import TestCase |
| 24 | 25 | from django.utils.functional import curry |
| … |
… |
class SerializerTests(TestCase):
|
| 390 | 391 | with self.assertRaises(SerializerDoesNotExist): |
| 391 | 392 | serializers.get_deserializer("nonsense") |
| 392 | 393 | |
| | 394 | def test_json_deserializer_exception(self): |
| | 395 | with self.assertRaises(DeserializationError): |
| | 396 | data = serializers.deserialize("json", """[{"pk":1}""") |
| | 397 | for obj in data: |
| | 398 | pass |
| | 399 | |
| 393 | 400 | def serializerTest(format, self): |
| 394 | 401 | |
| 395 | 402 | # Create all the objects defined in the test data |