jsonwrap.py
1.67 KB
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
'''
json wrapper to be used instead of a direct call.
Author: Pontus Stenetorp <pontus is s u-tokyo ac jp>
Version: 2011-04-21
'''
# use ultrajson if set up
try:
from sys import path as sys_path
from os.path import join as path_join
from os.path import dirname
sys_path.append(path_join(dirname(__file__),
'../lib/ujson'))
from ujson import dumps as _lib_dumps
from ujson import loads as _lib_loads
# ultrajson doesn't have encoding
lib_dumps = _lib_dumps
lib_loads = _lib_loads
except ImportError, e:
# fall back to native json if available
try:
from json import dumps as _lib_dumps
from json import loads as _lib_loads
except ImportError:
# We are on an older Python, use our included lib
from sys import path as sys_path
from os.path import join as path_join
from os.path import dirname
sys_path.append(path_join(dirname(__file__),
'../lib/simplejson-2.1.5'))
from simplejson import dumps as _lib_dumps
from simplejson import loads as _lib_loads
# Wrap the loads and dumps to expect utf-8
from functools import partial
lib_dumps = partial(_lib_dumps, encoding='utf-8')#, ensure_ascii=False)
lib_loads = partial(_lib_loads, encoding='utf-8')#, ensure_ascii=False)
#ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, separators[, encoding
def dumps(dic):
# ultrajson has neither sort_keys nor indent
# return lib_dumps(dic, sort_keys=True, indent=2)
return lib_dumps(dic)
def loads(s):
return lib_loads(s)
# TODO: Unittest that tries the import, encoding etc.