1
1
"""Priority based json library imports.
3
Use jsonapi.loads() and jsonapi.dumps() for guaranteed symmetry.
5
Priority: simplejson > jsonlib2 > json
7
Always serializes to bytes instead of unicode for zeromq compatibility.
9
jsonapi.loads/dumps provide kwarg-compatibility with stdlib json.
11
To override pyzmq's choice of json library, you can simply override the loads/dumps
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
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
10
# Copyright (c) 2010 Min Ragan-Kelley, Brian Granger
12
# This file is part of pyzmq.
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.
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.
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/>.
32
#-----------------------------------------------------------------------------
33
# Copyright (c) 2010-2012 Brian Granger, Min Ragan-Kelley
35
# This file is part of pyzmq
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
#-----------------------------------------------------------------------------
28
41
#-----------------------------------------------------------------------------
30
43
#-----------------------------------------------------------------------------
32
45
from zmq.utils.strtypes import bytes, unicode
33
# priority: jsonlib2 > jsonlib > simplejson > json
38
import jsonlib2 as jsonmod
49
priority = ['simplejson', 'jsonlib2', 'json']
41
import jsonlib as jsonmod
52
jsonmod = __import__(mod)
42
53
except ImportError:
44
import simplejson as jsonmod
47
import json as jsonmod
51
58
def _squash_unicode(s):
52
59
if isinstance(s, unicode):
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.
61
return _squash_unicode(jsonmod.dumps(o,**kwargs))
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.
68
return _squash_unicode(jsonmod.dumps(o, separators=(',',':'),**kwargs))
69
if 'separators' not in kwargs:
70
kwargs['separators'] = (',', ':')
72
return _squash_unicode(jsonmod.dumps(o, **kwargs))
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.
74
79
if str is unicode and isinstance(s, bytes):
75
80
s = s.decode('utf8')
76
return jsonmod.loads(s,**kwargs)
78
if jsonmod is not None and jsonmod.__name__== 'jsonlib':
81
__all__ = ['jsonmod', 'dumps', 'dumps']
81
return jsonmod.loads(s, **kwargs)
83
__all__ = ['jsonmod', 'dumps', 'loads']