~ubuntu-branches/ubuntu/wily/pyzmq/wily

« back to all changes in this revision

Viewing changes to zmq/utils/jsonapi.py

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2013-02-24 19:23:15 UTC
  • mfrom: (1.2.1) (9 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: package-import@ubuntu.com-20130224192315-qhmwp3m3ymk8r60d
Tags: 2.2.0.1-1
* New upstream release
* relicense debian packaging to LGPL-3
* update watch file to use github directly
  thanks to Bart Martens for the file
* add autopkgtests
* drop obsolete DM-Upload-Allowed
* bump standard to 3.9.4, no changes required

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""Priority based json library imports.
2
2
 
 
3
Use jsonapi.loads() and jsonapi.dumps() for guaranteed symmetry.
 
4
 
 
5
Priority: simplejson > jsonlib2 > json
 
6
 
 
7
Always serializes to bytes instead of unicode for zeromq compatibility.
 
8
 
 
9
jsonapi.loads/dumps provide kwarg-compatibility with stdlib json.
 
10
 
 
11
To override pyzmq's choice of json library, you can simply override the loads/dumps
 
12
methods, e.g.::
 
13
 
 
14
    import ujson
 
15
    from zmq.utils import jsonapi
 
16
    jsonapi.jsonmod = ujson
 
17
    # ujson doesn't support the `separators` kwarg we use, so force its own dumps:
 
18
    jsonapi.dumps = ujson.dumps
 
19
 
 
20
To select the super-fast ujson module.  Note that using a different module such
 
21
as ujson that does not support the same kwargs as stdlib json may break
 
22
compatibility with other tools that depend on this, if used in the same process.
 
23
A safer route is to just serialize your own messages yourself with your favorite
 
24
library.
 
25
 
3
26
Authors
4
27
-------
5
28
* MinRK
6
29
* Brian Granger
7
30
"""
8
31
 
9
 
#
10
 
#    Copyright (c) 2010 Min Ragan-Kelley, Brian Granger
11
 
#
12
 
#    This file is part of pyzmq.
13
 
#
14
 
#    pyzmq is free software; you can redistribute it and/or modify it under
15
 
#    the terms of the Lesser GNU General Public License as published by
16
 
#    the Free Software Foundation; either version 3 of the License, or
17
 
#    (at your option) any later version.
18
 
#
19
 
#    pyzmq is distributed in the hope that it will be useful,
20
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 
#    Lesser GNU General Public License for more details.
23
 
#
24
 
#    You should have received a copy of the Lesser GNU General Public License
25
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 
#
 
32
#-----------------------------------------------------------------------------
 
33
#  Copyright (c) 2010-2012 Brian Granger, Min Ragan-Kelley
 
34
#
 
35
#  This file is part of pyzmq
 
36
#
 
37
#  Distributed under the terms of the New BSD License.  The full license is in
 
38
#  the file COPYING.BSD, distributed as part of this software.
 
39
#-----------------------------------------------------------------------------
27
40
 
28
41
#-----------------------------------------------------------------------------
29
42
# Imports
30
43
#-----------------------------------------------------------------------------
31
44
 
32
45
from zmq.utils.strtypes import bytes, unicode
33
 
# priority: jsonlib2 > jsonlib > simplejson > json
34
46
 
35
47
jsonmod = None
36
48
 
37
 
try:
38
 
    import jsonlib2 as jsonmod
39
 
except ImportError:
 
49
priority = ['simplejson', 'jsonlib2', 'json']
 
50
for mod in priority:
40
51
    try:
41
 
        import jsonlib as jsonmod
 
52
        jsonmod = __import__(mod)
42
53
    except ImportError:
43
 
        try:
44
 
            import simplejson as jsonmod
45
 
        except ImportError:
46
 
            try:
47
 
                import json as jsonmod
48
 
            except ImportError:
49
 
                pass
 
54
        pass
 
55
    else:
 
56
        break
50
57
 
51
58
def _squash_unicode(s):
52
59
    if isinstance(s, unicode):
54
61
    else:
55
62
        return s
56
63
 
57
 
def jsonlib_dumps(o,**kwargs):
58
 
    """This one is separate because jsonlib doesn't allow specifying separators.
59
 
    See jsonlib.dumps for details on kwargs.
60
 
    """
61
 
    return _squash_unicode(jsonmod.dumps(o,**kwargs))
62
 
 
63
64
def dumps(o, **kwargs):
64
 
    """Serialize object to JSON str.
 
65
    """Serialize object to JSON bytes.
65
66
    See %s.dumps for details on kwargs.
66
 
    """%jsonmod
67
 
    
68
 
    return _squash_unicode(jsonmod.dumps(o, separators=(',',':'),**kwargs))
 
67
    """ % jsonmod
 
68
    
 
69
    if 'separators' not in kwargs:
 
70
        kwargs['separators'] = (',', ':')
 
71
    
 
72
    return _squash_unicode(jsonmod.dumps(o, **kwargs))
69
73
 
70
 
def loads(s,**kwargs):
 
74
def loads(s, **kwargs):
71
75
    """Load object from JSON str.
72
76
    See %s.loads for details on kwargs.
73
 
    """%jsonmod
 
77
    """ % jsonmod
 
78
    
74
79
    if str is unicode and isinstance(s, bytes):
75
80
        s = s.decode('utf8')
76
 
    return jsonmod.loads(s,**kwargs)
77
 
 
78
 
if jsonmod is not None and jsonmod.__name__== 'jsonlib':
79
 
    dumps = jsonlib_dumps
80
 
 
81
 
__all__ = ['jsonmod', 'dumps', 'dumps']
 
81
    return jsonmod.loads(s, **kwargs)
 
82
 
 
83
__all__ = ['jsonmod', 'dumps', 'loads']
82
84