File: defaultjson.py

package info (click to toggle)
gevent-socketio 0.3.6-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 716 kB
  • ctags: 551
  • sloc: python: 2,031; makefile: 134
file content (21 lines) | stat: -rw-r--r-- 625 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
### default json loaders
try:
    import simplejson as json
    json_decimal_args = {"use_decimal": True}  # pragma: no cover
except ImportError:
    import json
    import decimal

    class DecimalEncoder(json.JSONEncoder):
        def default(self, o):
            if isinstance(o, decimal.Decimal):
                return float(o)
            return super(DecimalEncoder, self).default(o)
    json_decimal_args = {"cls": DecimalEncoder}

def default_json_dumps(data):
    return json.dumps(data, separators=(',', ':'),
                      **json_decimal_args)

def default_json_loads(data):
    return json.loads(data)