Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with 2.5 #12

Closed
caduvieira opened this issue Sep 19, 2019 · 3 comments
Closed

Error with 2.5 #12

caduvieira opened this issue Sep 19, 2019 · 3 comments
Labels
invalid This doesn't seem right

Comments

@caduvieira
Copy link

Code Example:

test_ijson.py

import json
import ijson
import codecs

with codecs.open('test.json', encoding="utf-8") as json_file:
    form = ijson.items(json_file, 'menu.test_items.item')
    print(form)
    forms = (o for o in form)
    print(forms)
    for objects in forms:
        pass

test.json

{"menu": {"header": "SVG Viewer","test_items": [{"id": "Open"},{"id": "OpenNew", "label": "Open New"},{"id": "ZoomIn", "label": "Zoom In"},{"id": "ZoomOut", "label": "Zoom Out"},{"id": "OriginalView", "label": "Original View"},{"id": "Quality"},{"id": "Pause"},{"id": "Mute"},{"id": "Find", "label": "Find..."}]}}

Result:

<_yajl2.items object at 0x7fb21010f650>
<generator object at 0x7fb2100e0c00>
Traceback (most recent call last):
File "test_ijson.py", line 11, in
for objects in forms:
File "test_ijson.py", line 9, in
forms = (o for o in form)
TypeError: expected bytes, str found

If I import the python backend as ijson everthing went fine.

@jpmckinney
Copy link

You need to open the file in binary mode e.g. codecs.open('test.json', mode='rb', encoding='utf-8')

@rtobar
Copy link

rtobar commented Sep 20, 2019

Or simply with open('test.json', 'rb')

@rtobar rtobar added the invalid This doesn't seem right label Sep 20, 2019
@rtobar rtobar closed this as completed Sep 20, 2019
@caduvieira
Copy link
Author

Don't work if opens with encoding. Only works with binary and without encoding.
Thanks for the reply!

rtobar added a commit that referenced this issue Oct 1, 2019
All ijson backends, except for the python backend, need a bytes reader
in order to work. With the change of default backends in ijson 2.5 it
became clear that many people relied on the ability of the python
backend to accept string readers, and thus ijson's public API broke.

This commit adds a new function to turn a utf8-encoded string reader
into a bytes reader. It is mostly intended for use within ijson, but
users might find some of it useful.

Related issues: #12, #13

Signed-off-by: Rodrigo Tobar <rtobar@icrar.org>
rtobar added a commit that referenced this issue Oct 1, 2019
All backends, except for the python backend, require a bytes reader, but
users didn't expect this and some were give us a utf8-encoded string
reader instead. With this commit we check and automatically translate
the given reader so users don't have to bother (yet) doing it. Together
with the translation a DeprecationWarning is issued to make it clear
that this automatic translation won't be there for long.

Related issues: #12, #13

Signed-off-by: Rodrigo Tobar <rtobar@icrar.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

3 participants