diff options
author | Russ Cox <rsc@golang.org> | 2011-05-13 11:17:06 -0400 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2011-05-13 11:17:06 -0400 |
commit | db8f46e4fe4581c103c8ecbdad13673105a1a137 (patch) | |
tree | f8ce94f465c0bb2695b8b9328e5fac38f0f1a17d /lib/codereview/codereview.py | |
parent | fac1cb649cd0140dba9de854bc6455aac7475924 (diff) | |
download | go-db8f46e4fe4581c103c8ecbdad13673105a1a137.tar.gz |
codereview: handle 'null as missing field' in rietveld json
R=golang-dev, r
CC=golang-dev
http://codereview.appspot.com/4543046
Diffstat (limited to 'lib/codereview/codereview.py')
-rw-r--r-- | lib/codereview/codereview.py | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/lib/codereview/codereview.py b/lib/codereview/codereview.py index f6e69a417..ba2bbbaad 100644 --- a/lib/codereview/codereview.py +++ b/lib/codereview/codereview.py @@ -2025,25 +2025,35 @@ def JSONGet(ui, path): try: data = MySend(path, force_auth=False) typecheck(data, str) - d = coerce_to_utf8(json.loads(data)) + d = fix_json(json.loads(data)) except: ui.warn("JSONGet %s: %s\n" % (path, ExceptionDetail())) return None return d -def coerce_to_utf8(x): +# Clean up json parser output to match our expectations: +# * all strings are UTF-8-encoded str, not unicode. +# * missing fields are missing, not None, +# so that d.get("foo", defaultvalue) works. +def fix_json(x): if type(x) in [str, int, float, bool, type(None)]: pass elif type(x) is unicode: x = x.encode("utf-8") elif type(x) is list: for i in range(len(x)): - x[i] = coerce_to_utf8(x[i]) + x[i] = fix_json(x[i]) elif type(x) is dict: + todel = [] for k in x: - x[k] = coerce_to_utf8(x[k]) + if x[k] is None: + todel.append(k) + else: + x[k] = fix_json(x[k]) + for k in todel: + del x[k] else: - raise util.Abort("unknown type " + str(type(x)) + " in coerce_to_utf8") + raise util.Abort("unknown type " + str(type(x)) + " in fix_json") if type(x) is str: x = x.replace('\r\n', '\n') return x |