Module: check_mk
Branch: master
Commit: c1ae70efba787e92a8cef5bc49f7aca048a86aab
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=c1ae70efba787e…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Fri Feb 4 15:26:30 2011 +0100
Correct format of Multisite JSON output
---
web/plugins/views/webservice.py | 20 ++++----------------
1 files changed, 4 insertions(+), 16 deletions(-)
diff --git a/web/plugins/views/webservice.py b/web/plugins/views/webservice.py
index d9dab8b..53511ea 100644
--- a/web/plugins/views/webservice.py
+++ b/web/plugins/views/webservice.py
@@ -30,23 +30,12 @@ multisite_layouts["python"] = {
}
-json_escape = re.compile(r'[\x00-\x1f\\"\b\f\n\r\t]')
-ESCAPE_DCT = {
- '\\': '\\\\',
- '"': '\\"',
- '\b': '\\b',
- '\f': '\\f',
- '\n': '\\n',
- '\r': '\\r',
- '\t': '\\t',
-}
-for i in range(0x20):
- ESCAPE_DCT.setdefault(chr(i), '\\u{0:04x}'.format(i))
+json_escape = re.compile(r'[\\"\r\n\t\b\f\x00-\x1f]')
+json_encoding_table = dict([(chr(i), '\\u%04x' % i) for i in range(32)])
+json_encoding_table.update({'\b': '\\b', '\f': '\\f',
'\n': '\\n', '\r': '\\r', '\t': '\\t',
'\\': '\\\\', '"': '\\"' })
def encode_string_json(s):
- def replace(match):
- return ESCAPE_DCT[match.group(0)]
- return '"' + ESCAPE.sub(replace, s) + '"'
+ return '"' + json_escape.sub(lambda m: json_encoding_table[m.group(0)],
s) + '"'
def render_json(data, view, group_painters, painters, num_columns):
@@ -64,7 +53,6 @@ def render_json(data, view, group_painters, painters, num_columns):
tdclass, content = p[0]["paint"](row)
stripped = htmllib.strip_tags(content)
utf8 = stripped.encode("utf-8")
- utf8 += r'\"'
html.write(encode_string_json(utf8))
html.write("]")
html.write("\n]\n")