2
Copyright (c) 2006, Gustavo Niemeyer <gustavo@niemeyer.net>
6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions are met:
9
* Redistributions of source code must retain the above copyright notice,
10
this list of conditions and the following disclaimer.
11
* Redistributions in binary form must reproduce the above copyright notice,
12
this list of conditions and the following disclaimer in the documentation
13
and/or other materials provided with the distribution.
14
* Neither the name of the copyright holder nor the names of its
15
contributors may be used to endorse or promote products derived from
16
this software without specific prior written permission.
18
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
def dumps(obj, _dt=dumps_table):
38
return _dt[type(obj)](obj)
40
raise ValueError, "Unsupported type: %s" % e
43
def loads(str, _lt=loads_table):
45
raise ValueError, "Can't load empty string"
47
return _lt[str[0]](str, 0)[0]
49
raise ValueError, "Unknown type character: %s" % e
51
raise ValueError, "Corrupted data"
54
return "b%d" % int(obj)
63
return "s%s:%s" % (len(obj), obj)
65
def dumps_unicode(obj):
66
obj = obj.encode("utf-8")
67
return "u%s:%s" % (len(obj), obj)
69
def dumps_list(obj, _dt=dumps_table):
70
return "l%s;" % "".join([_dt[type(val)](val) for val in obj])
72
def dumps_tuple(obj, _dt=dumps_table):
73
return "t%s;" % "".join([_dt[type(val)](val) for val in obj])
75
def dumps_dict(obj, _dt=dumps_table):
82
append(_dt[type(key)](key))
83
append(_dt[type(val)](val))
84
return "d%s;" % "".join(res)
89
def loads_bool(str, pos):
90
return bool(int(str[pos+1])), pos+2
92
def loads_int(str, pos):
93
endpos = str.index(";", pos)
94
return int(str[pos+1:endpos]), endpos+1
96
def loads_float(str, pos):
97
endpos = str.index(";", pos)
98
return float(str[pos+1:endpos]), endpos+1
100
def loads_str(str, pos):
101
startpos = str.index(":", pos)+1
102
endpos = startpos+int(str[pos+1:startpos-1])
103
return str[startpos:endpos], endpos
105
def loads_unicode(str, pos):
106
startpos = str.index(":", pos)+1
107
endpos = startpos+int(str[pos+1:startpos-1])
108
return str[startpos:endpos].decode("utf-8"), endpos
110
def loads_list(str, pos, _lt=loads_table):
114
while str[pos] != ";":
115
obj, pos = _lt[str[pos]](str, pos)
119
def loads_tuple(str, pos, _lt=loads_table):
123
while str[pos] != ";":
124
obj, pos = _lt[str[pos]](str, pos)
126
return tuple(res), pos+1
128
def loads_dict(str, pos, _lt=loads_table):
131
while str[pos] != ";":
132
key, pos = _lt[str[pos]](str, pos)
133
val, pos = _lt[str[pos]](str, pos)
137
def loads_none(str, pos):
141
dumps_table.update({ bool: dumps_bool,
146
unicode: dumps_unicode,
150
type(None): dumps_none })
152
loads_table.update({ "b": loads_bool,