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

using generators - missing iterencode _one_shot argument #314

Open
mdobrzanski opened this issue Apr 14, 2023 · 1 comment
Open

using generators - missing iterencode _one_shot argument #314

mdobrzanski opened this issue Apr 14, 2023 · 1 comment

Comments

@mdobrzanski
Copy link

mdobrzanski commented Apr 14, 2023

The _one_shot was recently removed. Is there an another way to disable use of c_make_encoder at runtime?

The only idea I have is it to disable it globally with simplejson.encoder.c_make_encoder = None but then it's always disabled and that is not perfect either.

That option allowed to use generators and stream json output without allocating too much memory.

import types
import simplejson


def gen():
    yield 1
    yield 2
    yield 3


encoder = simplejson.JSONEncoder(iterable_as_array=True)
output = encoder.iterencode(gen(), _one_shot=False)

assert isinstance(output, types.GeneratorType)

print(''.join(output))
@etrepum
Copy link
Member

etrepum commented Apr 14, 2023

There's no public interface for this (and there never really has been), but any setting for indent other than None will force the use of the Python implementation. The tests use a private function simplejson._toggle_speedups(False) which disables the C extension globally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants