summaryrefslogtreecommitdiff
path: root/zmq/utils/jsonapi.py
blob: 865ca6d581f0c6e7b0c6925b02661d8de6090d90 (plain)
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
51
52
53
54
55
56
57
58
59
"""Priority based json library imports.

Always serializes to bytes instead of unicode for zeromq compatibility
on Python 2 and 3.

Use ``jsonapi.loads()`` and ``jsonapi.dumps()`` for guaranteed symmetry.

Priority: ``simplejson`` > ``jsonlib2`` > stdlib ``json``

``jsonapi.loads/dumps`` provide kwarg-compatibility with stdlib json.

``jsonapi.jsonmod`` will be the module of the actual underlying implementation.
"""

# Copyright (C) PyZMQ Developers
# Distributed under the terms of the Modified BSD License.

from zmq.utils.strtypes import bytes, unicode

jsonmod = None

priority = ['simplejson', 'jsonlib2', 'json']
for mod in priority:
    try:
        jsonmod = __import__(mod)
    except ImportError:
        pass
    else:
        break

def dumps(o, **kwargs):
    """Serialize object to JSON bytes (utf-8).
    
    See jsonapi.jsonmod.dumps for details on kwargs.
    """
    
    if 'separators' not in kwargs:
        kwargs['separators'] = (',', ':')
    
    s = jsonmod.dumps(o, **kwargs)
    
    if isinstance(s, unicode):
        s = s.encode('utf8')
    
    return s

def loads(s, **kwargs):
    """Load object from JSON bytes (utf-8).
    
    See jsonapi.jsonmod.loads for details on kwargs.
    """
    
    if str is unicode and isinstance(s, bytes):
        s = s.decode('utf8')
    
    return jsonmod.loads(s, **kwargs)

__all__ = ['jsonmod', 'dumps', 'loads']