diff options
| author | Ali Vakilzade <ali@vakilzade.com> | 2020-06-16 15:51:58 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-16 16:51:58 +0200 |
| commit | e29637681be07606674cdccb47d1e53acb930f5b (patch) | |
| tree | 6a0fc79e94b4bfd5b137a09ee83955100e9b39cc /django | |
| parent | ea3beb4f5a61870c87ba028369de4d2c2f316ad0 (diff) | |
Fixed #30190 -- Added JSONL serializer.
Diffstat (limited to 'django')
| -rw-r--r-- | django/core/serializers/__init__.py | 1 | ||||
| -rw-r--r-- | django/core/serializers/jsonl.py | 57 |
2 files changed, 58 insertions, 0 deletions
diff --git a/django/core/serializers/__init__.py b/django/core/serializers/__init__.py index 5e16a7560f..793f6dc2bd 100644 --- a/django/core/serializers/__init__.py +++ b/django/core/serializers/__init__.py @@ -28,6 +28,7 @@ BUILTIN_SERIALIZERS = { "python": "django.core.serializers.python", "json": "django.core.serializers.json", "yaml": "django.core.serializers.pyyaml", + "jsonl": "django.core.serializers.jsonl", } _serializers = {} diff --git a/django/core/serializers/jsonl.py b/django/core/serializers/jsonl.py new file mode 100644 index 0000000000..ff0d9eb605 --- /dev/null +++ b/django/core/serializers/jsonl.py @@ -0,0 +1,57 @@ +""" +Serialize data to/from JSON Lines +""" + +import json + +from django.core.serializers.base import DeserializationError +from django.core.serializers.json import DjangoJSONEncoder +from django.core.serializers.python import ( + Deserializer as PythonDeserializer, Serializer as PythonSerializer, +) + + +class Serializer(PythonSerializer): + """Convert a queryset to JSON Lines.""" + internal_use_only = False + + def _init_options(self): + self._current = None + self.json_kwargs = self.options.copy() + self.json_kwargs.pop('stream', None) + self.json_kwargs.pop('fields', None) + self.json_kwargs.pop('indent', None) + self.json_kwargs['separators'] = (',', ': ') + self.json_kwargs.setdefault('cls', DjangoJSONEncoder) + self.json_kwargs.setdefault('ensure_ascii', False) + + def start_serialization(self): + self._init_options() + + def end_object(self, obj): + # self._current has the field data + json.dump(self.get_dump_object(obj), self.stream, **self.json_kwargs) + self.stream.write("\n") + self._current = None + + def getvalue(self): + # Grandparent super + return super(PythonSerializer, self).getvalue() + + +def Deserializer(stream_or_string, **options): + """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 + try: + yield list(PythonDeserializer([json.loads(line), ], **options))[0] + except (GeneratorExit, DeserializationError): + raise + except Exception as exc: + raise DeserializationError() from exc |
