| 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 ( |
| 10 | Deserializer as PythonDeserializer, Serializer as PythonSerializer, |
| 11 | ) |
| 12 | |
| 13 | |
| 14 | class 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 | |
| 35 | def 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 |