serialize data tofrom json lines convert a queryset to json lines internaluseonly false def initoptionsself self current none self jsonkwargs self options copy self jsonkwargs popstream none self jsonkwargs popfields none self jsonkwargs popindent none self jsonkwargsseparators self jsonkwargs setdefaultcls djangojsonencoder self jsonkwargs setdefaultensureascii false def startserializationself self initoptions def endobjectself obj self current has the field data json dumpself getdumpobjectobj self stream self jsonkwargs self stream writen self current none def getvalueself grandparent super return superpythonserializer self getvalue def deserializerstreamorstring options convert a queryset to json lines self _current has the field data grandparent super deserialize a stream or string of json data
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
from django.core.serializers.python import Serializer as PythonSerializer


class Serializer(PythonSerializer):
    

    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):
        
        json.dump(self.get_dump_object(obj), self.stream, **self.json_kwargs)
        self.stream.write("\n")
        self._current = None

    def getvalue(self):
        
        return super(PythonSerializer, self).getvalue()


def Deserializer(stream_or_string, **options):
    
    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 from PythonDeserializer([json.loads(line)], **options)
        except (GeneratorExit, DeserializationError):
            raise
        except Exception as exc:
            raise DeserializationError() from exc