serialize data tofrom json convert a queryset to json internaluseonly false def initoptionsself self current none self jsonkwargs self options copy self jsonkwargs popstream none self jsonkwargs popfields none if self options getindent prevent trailing spaces self jsonkwargsseparators self jsonkwargs setdefaultcls djangojsonencoder self jsonkwargs setdefaultensureascii false def startserializationself self initoptions self stream write def endserializationself if self options getindent self stream writen self stream write if self options getindent self stream writen def endobjectself obj self current has the field data indent self options getindent if not self first self stream write if not indent self stream write if indent self stream writen json dumpself getdumpobjectobj self stream self jsonkwargs self current none def getvalueself grandparent super return superpythonserializer self getvalue def deserializerstreamorstring options jsonencoder subclass that knows how to encode datetime decimal types and uuids see date time string format in the ecma262 specification convert a queryset to json prevent trailing spaces self _current has the field data grandparent super deserialize a stream or string of json data jsonencoder subclass that knows how to encode date time decimal types and uuids see date time string format in the ecma 262 specification
import datetime
import decimal
import json
import uuid

from django.core.serializers.base import DeserializationError
from django.core.serializers.python import Deserializer as PythonDeserializer
from django.core.serializers.python import Serializer as PythonSerializer
from django.utils.duration import duration_iso_string
from django.utils.functional import Promise
from django.utils.timezone import is_aware


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)
        if self.options.get("indent"):
            
            self.json_kwargs["separators"] = (",", ": ")
        self.json_kwargs.setdefault("cls", DjangoJSONEncoder)
        self.json_kwargs.setdefault("ensure_ascii", False)

    def start_serialization(self):
        self._init_options()
        self.stream.write("[")

    def end_serialization(self):
        if self.options.get("indent"):
            self.stream.write("\n")
        self.stream.write("]")
        if self.options.get("indent"):
            self.stream.write("\n")

    def end_object(self, obj):
        
        indent = self.options.get("indent")
        if not self.first:
            self.stream.write(",")
            if not indent:
                self.stream.write(" ")
        if indent:
            self.stream.write("\n")
        json.dump(self.get_dump_object(obj), self.stream, **self.json_kwargs)
        self._current = None

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


def Deserializer(stream_or_string, **options):
    
    if not isinstance(stream_or_string, (bytes, str)):
        stream_or_string = stream_or_string.read()
    if isinstance(stream_or_string, bytes):
        stream_or_string = stream_or_string.decode()
    try:
        objects = json.loads(stream_or_string)
        yield from PythonDeserializer(objects, **options)
    except (GeneratorExit, DeserializationError):
        raise
    except Exception as exc:
        raise DeserializationError() from exc


class DjangoJSONEncoder(json.JSONEncoder):
    

    def default(self, o):
        
        if isinstance(o, datetime.datetime):
            r = o.isoformat()
            if o.microsecond:
                r = r[:23] + r[26:]
            if r.endswith("+00:00"):
                r = r.removesuffix("+00:00") + "Z"
            return r
        elif isinstance(o, datetime.date):
            return o.isoformat()
        elif isinstance(o, datetime.time):
            if is_aware(o):
                raise ValueError("JSON can't represent timezone-aware times.")
            r = o.isoformat()
            if o.microsecond:
                r = r[:12]
            return r
        elif isinstance(o, datetime.timedelta):
            return duration_iso_string(o)
        elif isinstance(o, (decimal.Decimal, uuid.UUID, Promise)):
            return str(o)
        else:
            return super().default(o)