forked from samose-kachori/Quillpad-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quilljson.py
executable file
·47 lines (41 loc) · 982 Bytes
/
quilljson.py
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
# -*- coding: utf-8 -*-
# @Date : Nov 22 2012
# @Author : Ram Prakash
# @Version : 1
def dictToJSON(d):
str = "{"
for i in d:
str += jsonmap[type(i)](i) + ": "
str += jsonmap[type(d[i])](d[i]) + ", "
if len(d.keys()) > 0:
str = str[:-2]
str += "}"
return str
def boolToJSON(b):
if b:
return "true"
else:
return "false"
def listToJSON(b):
str = "["
for i in b:
str += jsonmap[type(i)](i) + ", "
if len(b) > 0:
str = str[:-2]
str += "]"
return str
jsonmap = {
type([]): listToJSON,
type(()): listToJSON,
type({}): dictToJSON,
type(""): lambda x: '"%s"' % (x),
type(0): lambda x: str(x),
type(u""): lambda x: '"' + x + '"',
type(0.1): lambda x: str(x),
type(True): boolToJSON,
type(None): lambda x: "null"
}
def encode(dict):
if type(dict) != type({}):
raise Exception("Expected a dictionary")
return dictToJSON(dict)