summaryrefslogtreecommitdiff
path: root/django/core/serializers/jsonl.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/core/serializers/jsonl.py')
-rw-r--r--django/core/serializers/jsonl.py31
1 files changed, 21 insertions, 10 deletions
diff --git a/django/core/serializers/jsonl.py b/django/core/serializers/jsonl.py
index c264c2ccaf..7bc9bed79f 100644
--- a/django/core/serializers/jsonl.py
+++ b/django/core/serializers/jsonl.py
@@ -39,19 +39,30 @@ 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 isinstance(stream_or_string, bytes):
- stream_or_string = stream_or_string.decode()
- if isinstance(stream_or_string, (bytes, str)):
- stream_or_string = stream_or_string.split("\n")
- for line in stream_or_string:
- if not line.strip():
- continue
+ def __init__(self, stream_or_string, **options):
+ if isinstance(stream_or_string, bytes):
+ stream_or_string = stream_or_string.decode()
+ if isinstance(stream_or_string, str):
+ stream_or_string = stream_or_string.splitlines()
+ super().__init__(Deserializer._get_lines(stream_or_string), **options)
+
+ def _handle_object(self, obj):
try:
- yield from PythonDeserializer([json.loads(line)], **options)
+ yield from super()._handle_object(obj)
except (GeneratorExit, DeserializationError):
raise
except Exception as exc:
- raise DeserializationError() from exc
+ raise DeserializationError(f"Error deserializing object: {exc}") from exc
+
+ @staticmethod
+ def _get_lines(stream):
+ for line in stream:
+ if not line.strip():
+ continue
+ try:
+ yield json.loads(line)
+ except Exception as exc:
+ raise DeserializationError() from exc