diff options
| author | Amir Karimi <amk9978@gmail.com> | 2024-09-12 10:56:18 +0200 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2024-09-17 11:00:49 +0200 |
| commit | ee5147cfd7de2add74a285537a8968ec074e70cd (patch) | |
| tree | 64129a4de2722fb78dc486c83656f9d907aab296 /django/core/serializers/json.py | |
| parent | a060a22ee2dde7aa29a5a29120087c4864887325 (diff) | |
Fixed #29522 -- Refactored the Deserializer functions to classes.
Co-authored-by: Emad Mokhtar <emad.mokhtar@veneficus.nl>
Diffstat (limited to 'django/core/serializers/json.py')
| -rw-r--r-- | django/core/serializers/json.py | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/django/core/serializers/json.py b/django/core/serializers/json.py index afac821465..7683368e62 100644 --- a/django/core/serializers/json.py +++ b/django/core/serializers/json.py @@ -59,19 +59,27 @@ class Serializer(PythonSerializer): return super(PythonSerializer, self).getvalue() -def Deserializer(stream_or_string, **options): +class Deserializer(PythonDeserializer): """Deserialize a stream or string of JSON data.""" - if not isinstance(stream_or_string, (bytes, str)): - stream_or_string = stream_or_string.read() - if isinstance(stream_or_string, bytes): - stream_or_string = stream_or_string.decode() - try: - objects = json.loads(stream_or_string) - yield from PythonDeserializer(objects, **options) - except (GeneratorExit, DeserializationError): - raise - except Exception as exc: - raise DeserializationError() from exc + + def __init__(self, stream_or_string, **options): + if not isinstance(stream_or_string, (bytes, str)): + stream_or_string = stream_or_string.read() + if isinstance(stream_or_string, bytes): + stream_or_string = stream_or_string.decode() + try: + objects = json.loads(stream_or_string) + except Exception as exc: + raise DeserializationError() from exc + super().__init__(objects, **options) + + def _handle_object(self, obj): + try: + yield from super()._handle_object(obj) + except (GeneratorExit, DeserializationError): + raise + except Exception as exc: + raise DeserializationError(f"Error deserializing object: {exc}") from exc class DjangoJSONEncoder(json.JSONEncoder): |
