Skip to content

Commit

Permalink
Raise BadRequest if JSON decode fails (closes django-tastypie#1053)
Browse files Browse the repository at this point in the history
Previously anything which caused an error in the underlying
`simplejson.loads` call was not caught. Now ValueErrors are converted
into BadRequest so they'll generate an HTTP 400
  • Loading branch information
acdha committed Oct 18, 2013
1 parent d9844dc commit e9048fd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion tastypie/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,10 @@ def from_json(self, content):
"""
Given some JSON data, returns a Python dictionary of the decoded data.
"""
return simplejson.loads(content)
try:
return simplejson.loads(content)
except ValueError:
raise BadRequest

def to_jsonp(self, data, options=None):
"""
Expand Down
5 changes: 5 additions & 0 deletions tests/core/tests/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ def test_from_json(self):
self.assertEqual(sample_1['date_joined'], u'2010-03-27')
self.assertEqual(sample_1['snowman'], u'☃')

def test_from_broken_json(self):
serializer = Serializer()
data = '{"foo": "bar",NO CARRIER'
self.assertRaises(BadRequest, serializer.from_json, data)

def test_round_trip_xml(self):
serializer = Serializer()
sample_data = self.get_sample2()
Expand Down

0 comments on commit e9048fd

Please sign in to comment.