Passed
Push — master ( b3caf6...57589b )
by Fabio
02:22
created

benedict.core.dump   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 19
dl 0
loc 27
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A _encoder() 0 3 2
A dump() 0 13 2
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