root / lib / serializer.py @ fc8a6b8f
History | View | Annotate | Download (1.9 kB)
1 | 8d14b30d | Iustin Pop | #
|
---|---|---|---|
2 | 8d14b30d | Iustin Pop | #
|
3 | 8d14b30d | Iustin Pop | |
4 | 8d14b30d | Iustin Pop | # Copyright (C) 2007, 2008 Google Inc.
|
5 | 8d14b30d | Iustin Pop | #
|
6 | 8d14b30d | Iustin Pop | # This program is free software; you can redistribute it and/or modify
|
7 | 8d14b30d | Iustin Pop | # it under the terms of the GNU General Public License as published by
|
8 | 8d14b30d | Iustin Pop | # the Free Software Foundation; either version 2 of the License, or
|
9 | 8d14b30d | Iustin Pop | # (at your option) any later version.
|
10 | 8d14b30d | Iustin Pop | #
|
11 | 8d14b30d | Iustin Pop | # This program is distributed in the hope that it will be useful, but
|
12 | 8d14b30d | Iustin Pop | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 8d14b30d | Iustin Pop | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 8d14b30d | Iustin Pop | # General Public License for more details.
|
15 | 8d14b30d | Iustin Pop | #
|
16 | 8d14b30d | Iustin Pop | # You should have received a copy of the GNU General Public License
|
17 | 8d14b30d | Iustin Pop | # along with this program; if not, write to the Free Software
|
18 | 8d14b30d | Iustin Pop | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 8d14b30d | Iustin Pop | # 02110-1301, USA.
|
20 | 8d14b30d | Iustin Pop | |
21 | 8d14b30d | Iustin Pop | """Serializer abstraction module
|
22 | 8d14b30d | Iustin Pop |
|
23 | 8d14b30d | Iustin Pop | This module introduces a simple abstraction over the serialization
|
24 | 8d14b30d | Iustin Pop | backend (currently json).
|
25 | 8d14b30d | Iustin Pop |
|
26 | 8d14b30d | Iustin Pop | """
|
27 | fe267188 | Iustin Pop | # pylint: disable-msg=C0103
|
28 | fe267188 | Iustin Pop | |
29 | fe267188 | Iustin Pop | # C0103: Invalid name, since pylint doesn't see that Dump points to a
|
30 | fe267188 | Iustin Pop | # function and not a constant
|
31 | 8d14b30d | Iustin Pop | |
32 | 8d14b30d | Iustin Pop | import simplejson |
33 | 8d14b30d | Iustin Pop | import re |
34 | 8d14b30d | Iustin Pop | |
35 | 071448fb | Michael Hanselmann | |
36 | 8d14b30d | Iustin Pop | # Check whether the simplejson module supports indentation
|
37 | 8d14b30d | Iustin Pop | _JSON_INDENT = 2
|
38 | 8d14b30d | Iustin Pop | try:
|
39 | 8d14b30d | Iustin Pop | simplejson.dumps(1, indent=_JSON_INDENT)
|
40 | 8d14b30d | Iustin Pop | except TypeError: |
41 | 8d14b30d | Iustin Pop | _JSON_INDENT = None
|
42 | 8d14b30d | Iustin Pop | |
43 | e91ffe49 | Michael Hanselmann | _RE_EOLSP = re.compile('[ \t]+$', re.MULTILINE)
|
44 | 8d14b30d | Iustin Pop | |
45 | 8d14b30d | Iustin Pop | |
46 | 071448fb | Michael Hanselmann | def DumpJson(data, indent=True): |
47 | 8d14b30d | Iustin Pop | """Serialize a given object.
|
48 | 8d14b30d | Iustin Pop |
|
49 | c41eea6e | Iustin Pop | @param data: the data to serialize
|
50 | c41eea6e | Iustin Pop | @param indent: whether to indent output (depends on simplejson version)
|
51 | c41eea6e | Iustin Pop |
|
52 | c41eea6e | Iustin Pop | @return: the string representation of data
|
53 | 071448fb | Michael Hanselmann |
|
54 | 8d14b30d | Iustin Pop | """
|
55 | 071448fb | Michael Hanselmann | if not indent or _JSON_INDENT is None: |
56 | 8d14b30d | Iustin Pop | txt = simplejson.dumps(data) |
57 | 8d14b30d | Iustin Pop | else:
|
58 | 8d14b30d | Iustin Pop | txt = simplejson.dumps(data, indent=_JSON_INDENT) |
59 | 071448fb | Michael Hanselmann | |
60 | e91ffe49 | Michael Hanselmann | txt = _RE_EOLSP.sub("", txt)
|
61 | 8d14b30d | Iustin Pop | if not txt.endswith('\n'): |
62 | 8d14b30d | Iustin Pop | txt += '\n'
|
63 | 8d14b30d | Iustin Pop | return txt
|
64 | 8d14b30d | Iustin Pop | |
65 | 8d14b30d | Iustin Pop | |
66 | 228538cf | Michael Hanselmann | def LoadJson(txt): |
67 | 8d14b30d | Iustin Pop | """Unserialize data from a string.
|
68 | 8d14b30d | Iustin Pop |
|
69 | c41eea6e | Iustin Pop | @param txt: the json-encoded form
|
70 | c41eea6e | Iustin Pop |
|
71 | c41eea6e | Iustin Pop | @return: the original data
|
72 | c41eea6e | Iustin Pop |
|
73 | 8d14b30d | Iustin Pop | """
|
74 | 8d14b30d | Iustin Pop | return simplejson.loads(txt)
|
75 | 228538cf | Michael Hanselmann | |
76 | 228538cf | Michael Hanselmann | |
77 | 228538cf | Michael Hanselmann | Dump = DumpJson |
78 | 228538cf | Michael Hanselmann | Load = LoadJson |