Skip to content
This repository has been archived by the owner on Mar 8, 2018. It is now read-only.

Commit

Permalink
Finish up encoding of dictionaries and strings
Browse files Browse the repository at this point in the history
  • Loading branch information
R. Tyler Ballance committed Jul 12, 2009
1 parent 075cdcf commit c3c6853
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
18 changes: 17 additions & 1 deletion encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ static yajl_gen_status ProcessObject(_YajlEncoder *self, PyObject *object)
if (object == Py_False) {
return yajl_gen_bool(handle, 0);
}
if (PyString_Check(object)) {
const unsigned char *buffer;
int length;
PyString_AsStringAndSize(object, (char **)&buffer, &length);

return yajl_gen_string(handle, buffer, (unsigned int)(length));
}
if (PyInt_Check(object)) {
fprintf(stderr, "int\n");
return yajl_gen_integer(handle, PyInt_AsLong(object));
}
if (PyLong_Check(object)) {
Expand All @@ -79,7 +85,17 @@ static yajl_gen_status ProcessObject(_YajlEncoder *self, PyObject *object)
return status;
}
if (PyDict_Check(object)) {
PyObject *key, *value;
int position = 0;

status = yajl_gen_map_open(handle);
while (PyDict_Next(object, &position, &key, &value)) {
status = ProcessObject(self, key);
status = ProcessObject(self, value);
}
return yajl_gen_map_close(handle);
}


exit:
return yajl_gen_in_error_state;
Expand Down
7 changes: 7 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ def test_Null(self):
def test_List(self):
self.assertEncodesTo([1,2], '[1,2]')

def test_Dict(self):
self.assertEncodesTo({'key' : 'value'}, '{"key":"value"}')

def test_NestedDictAndList(self):
self.assertEncodesTo({'key' : {'subkey' : [1,2,3]}},
'{"key":{"subkey":[1,2,3]}}')

class ErrorCasesTests(unittest.TestCase):
def setUp(self):
self.d = yajl.Decoder()
Expand Down

0 comments on commit c3c6853

Please sign in to comment.