Ticket #11970: 11970-with-test3.diff

File 11970-with-test3.diff, 2.5 KB (added by Claude Paroz, 12 years ago)

Updated to current trunk

  • django/core/serializers/json.py

    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  
    88
    99from django.core.serializers.python import Serializer as PythonSerializer
    1010from django.core.serializers.python import Deserializer as PythonDeserializer
     11from django.core.serializers import base
    1112from django.utils import simplejson
    1213from django.utils.timezone import is_aware
    1314
    class Serializer(PythonSerializer):  
    2728        if callable(getattr(self.stream, 'getvalue', None)):
    2829            return self.stream.getvalue()
    2930
     31
    3032def Deserializer(stream_or_string, **options):
    3133    """
    3234    Deserialize a stream or string of JSON data.
    def Deserializer(stream_or_string, **options):  
    3537        stream = StringIO(stream_or_string)
    3638    else:
    3739        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
    4047
    4148class DjangoJSONEncoder(simplejson.JSONEncoder):
    4249    """
  • tests/regressiontests/serializers_regress/tests.py

    diff --git a/tests/regressiontests/serializers_regress/tests.py b/tests/regressiontests/serializers_regress/tests.py
    index 9c9022d..318fd2e 100644
    a b except ImportError:  
    1919
    2020from django.core import serializers
    2121from django.core.serializers import SerializerDoesNotExist
     22from django.core.serializers.base import DeserializationError
    2223from django.db import connection, models
    2324from django.test import TestCase
    2425from django.utils.functional import curry
    class SerializerTests(TestCase):  
    390391        with self.assertRaises(SerializerDoesNotExist):
    391392            serializers.get_deserializer("nonsense")
    392393
     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
    393400def serializerTest(format, self):
    394401
    395402    # Create all the objects defined in the test data
Back to Top