Merge lp:~divmod-dev/divmod.org/nevow-json into lp:divmod.org

Proposed by Jean-Paul Calderone
Status: Merged
Approved by: Tristan Seligmann
Approved revision: 2676
Merged at revision: 2676
Proposed branch: lp:~divmod-dev/divmod.org/nevow-json
Merge into: lp:divmod.org
Diff against target: 51 lines (+23/-0)
2 files modified
Nevow/nevow/json.py (+5/-0)
Nevow/nevow/test/test_json.py (+18/-0)
To merge this branch: bzr merge lp:~divmod-dev/divmod.org/nevow-json
Reviewer Review Type Date Requested Status
Tristan Seligmann Approve
Review via email: mp+66843@code.launchpad.net

Description of the change

I added U+2028 and U+2029 to nevow.json.serialize's special cased code points. It already handled \r and \n. I added a test for these four cases specifically, too.

To post a comment you must log in.
Revision history for this message
Tristan Seligmann (mithrandi) wrote :

Looks good! (I would say "Please merge", but I'm about to set the merge proposal to Approved, which I expect means the same thing)

One thing does occur to me: if you have a reference handy documenting the characters that need to be escaped, it might be useful leaving it in a comment near this code for anyone trying to make sense of it later on.

review: Approve
lp:~divmod-dev/divmod.org/nevow-json updated
2677. By Jean-Paul Calderone

Explain the two new characters a bit

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Nevow/nevow/json.py'
2--- Nevow/nevow/json.py 2008-05-22 18:46:50 +0000
3+++ Nevow/nevow/json.py 2011-07-10 13:58:36 +0000
4@@ -235,6 +235,7 @@
5
6 _translation = dict([(o, u'\\x%02x' % (o,)) for o in range(0x20)])
7
8+# Characters which cannot appear as literals in the output
9 _translation.update({
10 ord(u'\\'): u'\\\\',
11 ord(u'"'): ur'\"',
12@@ -243,6 +244,10 @@
13 ord(u'\n'): ur'\n',
14 ord(u'\t'): ur'\t',
15 ord(u'\r'): ur'\r',
16+ # The next two are sneaky, see
17+ # http://timelessrepo.com/json-isnt-a-javascript-subset
18+ ord(u'\u2028'): u'\\u2028',
19+ ord(u'\u2029'): u'\\u2029',
20 })
21
22 def stringEncode(s):
23
24=== modified file 'Nevow/nevow/test/test_json.py'
25--- Nevow/nevow/test/test_json.py 2010-07-12 19:00:11 +0000
26+++ Nevow/nevow/test/test_json.py 2011-07-10 13:58:36 +0000
27@@ -107,6 +107,24 @@
28 self.assertEquals(unstruct, struct, failMsg)
29 self.assert_(isinstance(unstruct, unicode), failMsg)
30
31+
32+ def test_lineTerminators(self):
33+ """
34+ When passed a unicode string containing a line terminator,
35+ L{json.serialize} emits an escape sequence representing that character
36+ (not a UTF-8 sequence directly representing that the line terminator
37+ code point).
38+
39+ Literal line terminators are allowed in JSON, but some parsers do not
40+ handle them properly.
41+ """
42+ # These are the four line terminators currently in Unicode.
43+ self.assertEqual('"\\r"', json.serialize(u"\r"))
44+ self.assertEqual('"\\n"', json.serialize(u"\n"))
45+ self.assertEqual('"\\u2028"', json.serialize(u"\u2028"))
46+ self.assertEqual('"\\u2029"', json.serialize(u"\u2029"))
47+
48+
49 def testScientificNotation(self):
50 self.assertEquals(json.parse('1e10'), 10**10)
51 self.assertEquals(json.parse('1e0'), 1)

Subscribers

People subscribed via source and target branches

to all changes: