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
=== modified file 'Nevow/nevow/json.py'
--- Nevow/nevow/json.py 2008-05-22 18:46:50 +0000
+++ Nevow/nevow/json.py 2011-07-10 13:58:36 +0000
@@ -235,6 +235,7 @@
235235
236_translation = dict([(o, u'\\x%02x' % (o,)) for o in range(0x20)])236_translation = dict([(o, u'\\x%02x' % (o,)) for o in range(0x20)])
237237
238# Characters which cannot appear as literals in the output
238_translation.update({239_translation.update({
239 ord(u'\\'): u'\\\\',240 ord(u'\\'): u'\\\\',
240 ord(u'"'): ur'\"',241 ord(u'"'): ur'\"',
@@ -243,6 +244,10 @@
243 ord(u'\n'): ur'\n',244 ord(u'\n'): ur'\n',
244 ord(u'\t'): ur'\t',245 ord(u'\t'): ur'\t',
245 ord(u'\r'): ur'\r',246 ord(u'\r'): ur'\r',
247 # The next two are sneaky, see
248 # http://timelessrepo.com/json-isnt-a-javascript-subset
249 ord(u'\u2028'): u'\\u2028',
250 ord(u'\u2029'): u'\\u2029',
246 })251 })
247252
248def stringEncode(s):253def stringEncode(s):
249254
=== modified file 'Nevow/nevow/test/test_json.py'
--- Nevow/nevow/test/test_json.py 2010-07-12 19:00:11 +0000
+++ Nevow/nevow/test/test_json.py 2011-07-10 13:58:36 +0000
@@ -107,6 +107,24 @@
107 self.assertEquals(unstruct, struct, failMsg)107 self.assertEquals(unstruct, struct, failMsg)
108 self.assert_(isinstance(unstruct, unicode), failMsg)108 self.assert_(isinstance(unstruct, unicode), failMsg)
109109
110
111 def test_lineTerminators(self):
112 """
113 When passed a unicode string containing a line terminator,
114 L{json.serialize} emits an escape sequence representing that character
115 (not a UTF-8 sequence directly representing that the line terminator
116 code point).
117
118 Literal line terminators are allowed in JSON, but some parsers do not
119 handle them properly.
120 """
121 # These are the four line terminators currently in Unicode.
122 self.assertEqual('"\\r"', json.serialize(u"\r"))
123 self.assertEqual('"\\n"', json.serialize(u"\n"))
124 self.assertEqual('"\\u2028"', json.serialize(u"\u2028"))
125 self.assertEqual('"\\u2029"', json.serialize(u"\u2029"))
126
127
110 def testScientificNotation(self):128 def testScientificNotation(self):
111 self.assertEquals(json.parse('1e10'), 10**10)129 self.assertEquals(json.parse('1e10'), 10**10)
112 self.assertEquals(json.parse('1e0'), 1)130 self.assertEquals(json.parse('1e0'), 1)

Subscribers

People subscribed via source and target branches

to all changes: