Skip to content

Commit

Permalink
Fixed path when map_key is first element
Browse files Browse the repository at this point in the history
gen->path always contains at least 1 element, and when a map_start event is
found a new None element is added to the list. Thus the logic to find whether
this map key is the first element of the prefix returned by this generator was
wrong and generated ".key" for a JSON like '{"key": 1}'

I've added a unit test to make sure this problem doesn't arise again.

Signed-off-by: Rodrigo Tobar <rtobar@icrar.org>
  • Loading branch information
rtobar committed Nov 3, 2016
1 parent 92458af commit 508f2f4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ijson/backends/_yajl2.c
Expand Up @@ -504,7 +504,7 @@ static PyObject* parsegen_iternext(PyObject *self) {
// new_path = path_stack[-2] + to_append
PyObject *last_path;
N_N(last_path = PySequence_GetItem(gen->path, npaths-2));
if( npaths > 1 ) {
if( npaths > 2 ) {
PyObject *last_path_dot;
CONCAT(last_path_dot, last_path, dot);
last_path = last_path_dot;
Expand Down
7 changes: 6 additions & 1 deletion tests.py
Expand Up @@ -192,6 +192,12 @@ def test_api(self):
self.assertTrue(list(self.backend.items(BytesIO(JSON), '')))
self.assertTrue(list(self.backend.parse(BytesIO(JSON))))

def test_items_twodictlevels(self):
f = BytesIO(b'{"meta":{"view":{"columns":[{"id": -1}, {"id": -2}]}}}')
ids = list(self.backend.items(f, 'meta.view.columns.item.id'))
self.assertEqual(2, len(ids))
self.assertListEqual([-2,-1], sorted(ids))

# Generating real TestCase classes for each importable backend
for name in ['python', 'yajl', 'yajl2', 'yajl2_cffi', 'yajl2_c']:
try:
Expand Down Expand Up @@ -265,7 +271,6 @@ def test_items(self):
None,
])


class Stream(unittest.TestCase):
def test_bytes(self):
l = Lexer(BytesIO(JSON))
Expand Down

0 comments on commit 508f2f4

Please sign in to comment.