| Total Complexity | 7 |
| Total Lines | 35 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | # -*- coding: utf-8 -*- |
||
| 2 | |||
| 3 | from __future__ import absolute_import |
||
| 4 | |||
| 5 | from benedict.serializers.abstract import AbstractSerializer |
||
| 6 | from benedict.utils import type_util |
||
| 7 | |||
| 8 | from six import text_type |
||
| 9 | |||
| 10 | import json |
||
| 11 | |||
| 12 | |||
| 13 | class JSONSerializer(AbstractSerializer): |
||
| 14 | |||
| 15 | def __init__(self): |
||
| 16 | super(JSONSerializer, self).__init__() |
||
| 17 | |||
| 18 | def decode(self, s, **kwargs): |
||
| 19 | data = json.loads(s, **kwargs) |
||
| 20 | return data |
||
| 21 | |||
| 22 | def encode(self, d, **kwargs): |
||
| 23 | kwargs.setdefault('default', self._encode_default) |
||
| 24 | data = json.dumps(d, **kwargs) |
||
| 25 | return data |
||
| 26 | |||
| 27 | def _encode_default(self, obj): |
||
| 28 | if type_util.is_set(obj): |
||
| 29 | return list(obj) |
||
| 30 | elif type_util.is_datetime(obj): |
||
| 31 | return obj.isoformat() |
||
| 32 | elif type_util.is_decimal(obj): |
||
| 33 | return text_type(obj) |
||
| 34 | return text_type(obj) |
||
| 35 |