| 1 |
""" |
| 2 |
Serialize data to/from JSON Lines |
| 3 |
""" |
| 4 |
|
| 5 |
import json |
| 6 |
|
| 7 |
from django.core.serializers.base import DeserializationError |
| 8 |
from django.core.serializers.json import DjangoJSONEncoder |
| 9 |
from django.core.serializers.python import Deserializer as PythonDeserializer |
| 10 |
from django.core.serializers.python import Serializer as PythonSerializer |
| 11 |
|
| 12 |
|
| 13 |
class Serializer(PythonSerializer): |
| 14 |
"""Convert a queryset to JSON Lines.""" |
| 15 |
|
| 16 |
internal_use_only = False |
| 17 |
|
| 18 |
def _init_options(self): |
| 19 |
self._current = None |
| 20 |
self.json_kwargs = self.options.copy() |
| 21 |
self.json_kwargs.pop("stream", None) |
| 22 |
self.json_kwargs.pop("fields", None) |
| 23 |
self.json_kwargs.pop("indent", None) |
| 24 |
self.json_kwargs["separators"] = (",", ": ") |
| 25 |
self.json_kwargs.setdefault("cls", DjangoJSONEncoder) |
| 26 |
self.json_kwargs.setdefault("ensure_ascii", False) |
| 27 |
|
| 28 |
def start_serialization(self): |
| 29 |
self._init_options() |
| 30 |
|
| 31 |
def end_object(self, obj): |
| 32 |
# self._current has the field data |
| 33 |
json.dump(self.get_dump_object(obj), self.stream, **self.json_kwargs) |
| 34 |
self.stream.write("\n") |
| 35 |
self._current = None |
| 36 |
|
| 37 |
def getvalue(self): |
| 38 |
# Grandparent super |
| 39 |
return super(PythonSerializer, self).getvalue() |
| 40 |
|
| 41 |
|
| 42 |
class Deserializer(PythonDeserializer): |
| 43 |
"""Deserialize a stream or string of JSON data.""" |
| 44 |
|
| 45 |
def __init__(self, stream_or_string, **options): |
| 46 |
if isinstance(stream_or_string, bytes): |
| 47 |
stream_or_string = stream_or_string.decode() |
| 48 |
if isinstance(stream_or_string, str): |
| 49 |
stream_or_string = stream_or_string.splitlines() |
| 50 |
super().__init__(Deserializer._get_lines(stream_or_string), **options) |
| 51 |
|
| 52 |
def _handle_object(self, obj): |
| 53 |
try: |
| 54 |
yield from super()._handle_object(obj) |
| 55 |
except (GeneratorExit, DeserializationError): |
| 56 |
raise |
| 57 |
except Exception as exc: |
| 58 |
raise DeserializationError(f"Error deserializing object: {exc}") from exc |
| 59 |
|
| 60 |
@staticmethod |
| 61 |
def _get_lines(stream): |
| 62 |
for line in stream: |
| 63 |
if not line.strip(): |
| 64 |
continue |
| 65 |
try: |
| 66 |
yield json.loads(line) |
| 67 |
except Exception as exc: |
| 68 |
raise DeserializationError() from exc |