| Total Complexity | 4 |
| Total Lines | 27 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | # -*- coding: utf-8 -*- |
||
| 2 | |||
| 3 | from benedict.utils import type_util |
||
| 4 | from six import text_type |
||
| 5 | |||
| 6 | import json |
||
| 7 | |||
| 8 | |||
| 9 | def _encoder(obj): |
||
| 10 | if not type_util.is_json_serializable(obj): |
||
| 11 | return text_type(obj) |
||
| 12 | |||
| 13 | |||
| 14 | def dump(obj, **kwargs): |
||
| 15 | options = { |
||
| 16 | 'indent': 4, |
||
| 17 | 'sort_keys': True, |
||
| 18 | 'default': _encoder, |
||
| 19 | } |
||
| 20 | options.update(**kwargs) |
||
| 21 | try: |
||
| 22 | output = json.dumps(obj, **options) |
||
| 23 | except TypeError: |
||
| 24 | options['sort_keys'] = False |
||
| 25 | output = json.dumps(obj, **options) |
||
| 26 | return output |
||
| 27 |