root / lib / serializer.py @ e5b7c4ca
History | View | Annotate | Download (1.7 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 | 8d14b30d | Iustin Pop | |
28 | 8d14b30d | Iustin Pop | import simplejson |
29 | 8d14b30d | Iustin Pop | import re |
30 | 8d14b30d | Iustin Pop | |
31 | 071448fb | Michael Hanselmann | |
32 | 8d14b30d | Iustin Pop | # Check whether the simplejson module supports indentation
|
33 | 8d14b30d | Iustin Pop | _JSON_INDENT = 2
|
34 | 8d14b30d | Iustin Pop | try:
|
35 | 8d14b30d | Iustin Pop | simplejson.dumps(1, indent=_JSON_INDENT)
|
36 | 8d14b30d | Iustin Pop | except TypeError: |
37 | 8d14b30d | Iustin Pop | _JSON_INDENT = None
|
38 | 8d14b30d | Iustin Pop | |
39 | e91ffe49 | Michael Hanselmann | _RE_EOLSP = re.compile('[ \t]+$', re.MULTILINE)
|
40 | 8d14b30d | Iustin Pop | |
41 | 8d14b30d | Iustin Pop | |
42 | 071448fb | Michael Hanselmann | def DumpJson(data, indent=True): |
43 | 8d14b30d | Iustin Pop | """Serialize a given object.
|
44 | 8d14b30d | Iustin Pop |
|
45 | c41eea6e | Iustin Pop | @param data: the data to serialize
|
46 | c41eea6e | Iustin Pop | @param indent: whether to indent output (depends on simplejson version)
|
47 | c41eea6e | Iustin Pop |
|
48 | c41eea6e | Iustin Pop | @return: the string representation of data
|
49 | 071448fb | Michael Hanselmann |
|
50 | 8d14b30d | Iustin Pop | """
|
51 | 071448fb | Michael Hanselmann | if not indent or _JSON_INDENT is None: |
52 | 8d14b30d | Iustin Pop | txt = simplejson.dumps(data) |
53 | 8d14b30d | Iustin Pop | else:
|
54 | 8d14b30d | Iustin Pop | txt = simplejson.dumps(data, indent=_JSON_INDENT) |
55 | 071448fb | Michael Hanselmann | |
56 | e91ffe49 | Michael Hanselmann | txt = _RE_EOLSP.sub("", txt)
|
57 | 8d14b30d | Iustin Pop | if not txt.endswith('\n'): |
58 | 8d14b30d | Iustin Pop | txt += '\n'
|
59 | 8d14b30d | Iustin Pop | return txt
|
60 | 8d14b30d | Iustin Pop | |
61 | 8d14b30d | Iustin Pop | |
62 | 228538cf | Michael Hanselmann | def LoadJson(txt): |
63 | 8d14b30d | Iustin Pop | """Unserialize data from a string.
|
64 | 8d14b30d | Iustin Pop |
|
65 | c41eea6e | Iustin Pop | @param txt: the json-encoded form
|
66 | c41eea6e | Iustin Pop |
|
67 | c41eea6e | Iustin Pop | @return: the original data
|
68 | c41eea6e | Iustin Pop |
|
69 | 8d14b30d | Iustin Pop | """
|
70 | 8d14b30d | Iustin Pop | return simplejson.loads(txt)
|
71 | 228538cf | Michael Hanselmann | |
72 | 228538cf | Michael Hanselmann | |
73 | 228538cf | Michael Hanselmann | Dump = DumpJson |
74 | 228538cf | Michael Hanselmann | Load = LoadJson |