/usr/lib/python3/dist-packages/maasserver/json.py is in python3-django-maas 2.4.0~beta2-6865-gec43e47e6-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | # Copyright 2013-2016 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Extension of Django's JSON serializer to support MAAS custom data types.
We register this as a replacement for Django's own JSON serialization by
setting it in the SERIALIZATION_MODULES setting.
"""
__all__ = [
'Deserializer',
'MAASJSONEncoder',
'Serializer',
]
import json
import django.core.serializers.json
from maasserver.fields import MAC
class MAASJSONEncoder(django.core.serializers.json.DjangoJSONEncoder):
"""MAAS-specific JSON encoder.
Compared to Django's encoder, it adds support for representing a
`MAC` in JSON.
"""
def default(self, value):
if isinstance(value, MAC):
return value.get_raw()
else:
return super(MAASJSONEncoder, self).default(value)
class Serializer(django.core.serializers.json.Serializer):
"""A copy of Django's serializer for JSON, but using our own encoder."""
def end_serialization(self):
if json.__version__.split('.') >= ['2', '1', '3']:
# Use JS strings to represent Python Decimal instances
# (ticket #16850)
self.options.update({'use_decimal': False})
json.dump(
self.objects, self.stream, cls=MAASJSONEncoder, **self.options)
# Keep using Django's deserializer. Loading a MAC from JSON will produce a
# string.
Deserializer = django.core.serializers.json.Deserializer
|