This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Convert tuples to lists before encoding them

The `json` module in Python 2.6 functions in a similar fashion:

    >>> json.dumps([(2,3,)])
    '[[2, 3]]'
    >>>

Reported by indigoviolet in issue #8

Change-Id: Ia5f0b649d137e42a692a38b52ccff71874dd4c71
  • Loading branch information...
rtyler committed Mar 22, 2010
1 parent 6d05e9f commit e5bfbe1ac69b6cac829d32acc813caff8cbb1a52
Showing with 22 additions and 0 deletions.
  1. +13 −0 encoder.c
  2. +9 −0 tests/unit.py
View
@@ -105,6 +105,19 @@ static yajl_gen_status ProcessObject(_YajlEncoder *self, PyObject *object)
status = yajl_gen_array_close(handle);
return status;
}
+ if (PyTuple_Check(object)) {
+ /*
+ * If we have a tuple, convert to a list
+ */
+ Py_ssize_t size = PyTuple_Size(object);
+ PyObject *converted = PyList_New(size);
+ unsigned int i = 0;
+
+ for (; i < size; ++i) {
+ PyList_SET_ITEM(converted, (Py_ssize_t)(i), PyTuple_GetItem(object, i));
+ }
+ return ProcessObject(self, converted);
+ }
if (PyDict_Check(object)) {
PyObject *key, *value;
Py_ssize_t position = 0;
View
@@ -202,6 +202,15 @@ def test_none_indent(self):
self.assertEquals(self.stream.getvalue(), '{"foo":"bar"}')
+class IssueEightTest(unittest.TestCase):
+ def runTest(self):
+ ''' http://github.com/rtyler/py-yajl/issues#issue/8 '''
+ encoded = yajl.dumps([(2,3,)])
+ decoded = yajl.loads(encoded)
+ self.assertEquals(len(decoded), 1)
+ self.assertEquals(decoded[0][0], 2)
+ self.assertEquals(decoded[0][1], 3)
+
if __name__ == '__main__':
verbosity = '-v' in sys.argv and 2 or 1
runner = unittest.TextTestRunner(verbosity=verbosity)

0 comments on commit e5bfbe1

Please sign in to comment.