Ticket #30190: patch.diff

File patch.diff, 2.1 KB (added by aliva, 6 years ago)

patch 1

  • django/core/serializers/__init__.py

    diff --git a/django/core/serializers/__init__.py b/django/core/serializers/__init__.py
    index 4b0ae078e2..bcf6928c5f 100644
    a b BUILTIN_SERIALIZERS = {  
    2828    "python": "django.core.serializers.python",
    2929    "json": "django.core.serializers.json",
    3030    "yaml": "django.core.serializers.pyyaml",
     31    "jsonl": "django.core.serializers.jsonl",
    3132}
    3233
    3334_serializers = {}
  • new file django/core/serializers/jsonl.py

    diff --git a/django/core/serializers/jsonl.py b/django/core/serializers/jsonl.py
    new file mode 100644
    index 0000000000..15c981a8d8
    - +  
     1"""
     2Serialize data to/from JSON lines
     3"""
     4
     5import json
     6
     7from django.core.serializers.base import DeserializationError
     8from django.core.serializers.json import DjangoJSONEncoder
     9from django.core.serializers.python import (
     10    Deserializer as PythonDeserializer, Serializer as PythonSerializer,
     11)
     12
     13
     14class Serializer(PythonSerializer):
     15    internal_use_only = False
     16
     17    def start_serialization(self):
     18        self._current = None
     19        self.json_kwargs = self.options.copy()
     20        self.json_kwargs.pop('stream', None)
     21        self.json_kwargs.pop('fields', None)
     22        self.options.pop('indent', None)
     23        self.json_kwargs.setdefault('cls', DjangoJSONEncoder)
     24
     25    def end_object(self, obj):
     26        json.dump(self.get_dump_object(obj), self.stream, **self.json_kwargs)
     27        self.stream.write("\n")
     28        self._current = None
     29
     30    def getvalue(self):
     31        # Grandparent super
     32        return super(PythonSerializer, self).getvalue()
     33
     34
     35def Deserializer(stream_or_string, **options):
     36    if isinstance(stream_or_string, (bytes, str)):
     37        stream_or_string = stream_or_string.split('\n')
     38
     39    for line in stream_or_string:
     40        line = line.strip()
     41        if line == '':
     42            continue
     43        try:
     44            yield list(PythonDeserializer([json.loads(line), ], **options))[0]
     45        except (GeneratorExit, DeserializationError):
     46            raise
     47        except Exception as exc:
     48            raise DeserializationError() from exc
Back to Top