Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #78477

Re: JSON-encoding very long iterators

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'desired.': 0.07; 'encoded': 0.07; 'json': 0.07; 'modifying': 0.07; 'string': 0.09; 'encode': 0.09; 'strings.': 0.09; 'try:': 0.09; 'def': 0.12; 'desirable,': 0.16; 'iterable': 0.16; 'iterator': 0.16; 'iterators': 0.16; 'iterators,': 0.16; 'o):': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'fit': 0.20; 'memory': 0.22; 'error': 0.23; "aren't": 0.24; "shouldn't": 0.24; 'mon,': 0.24; 'this:': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'ideal': 0.29; 'raise': 0.29; 'said,': 0.30; 'streaming': 0.30; 'message-id:@mail.gmail.com': 0.30; 'included': 0.31; 'code': 0.31; 'easier': 0.31; 'doc': 0.31; 'sep': 0.31; 'file': 0.32; 'class': 0.32; 'lists': 0.32; 'option': 0.32; 'cases': 0.33; 'could': 0.34; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'library.': 0.36; 'skip:j 20': 0.36; 'method': 0.36; 'possible': 0.36; 'should': 0.36; 'changing': 0.37; 'example,': 0.37; 'list': 0.37; 'implement': 0.38; 'sometimes': 0.38; 'e.g.': 0.38; 'lists.': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'ability': 0.39; 'to:addr:python.org': 0.39; 'first': 0.61; 'here:': 0.62; 'save': 0.62; 'more': 0.64; 'chance': 0.65; 'default': 0.69; 'url:a': 0.72; 'silently': 0.84; 'subject:long': 0.84; 'subject:very': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=t3tP11DgdP0uGR1C6vCzcQGf/DJC422bso3jUaFbajQ=; b=TGpKBIwffEIq2Ue2pvN2+SpZPYVljREl8Md1VIxphCm0j7+hJTO5n4N8Sa6EyWYldo UJ818UBIMMU65XmtLKYvMssT/Q6U+CdHjuh/rVcbZRtpfwKYqnVbMSd4HjtAm7B+RJA4 qW8Ju6KJ669lqs9lFZQFhlO5wajwOve2MJ7NVOed/iZeYdmdUvy5HlrhJBsYwkGx0oek +K+h96FcGGDHHYuvE56bfPizv5zgAdfLAxHeLspz/4unmgWB4VTU623w+Q6K+XbdPARi ouegcHsPJ5GR+y5bWsNh2UQWWchWGd0fEVuiGK2iEz2twNXnsuPv/Skn2cSksN1Ff8fB ZaWA==
X-Received by 10.68.212.134 with SMTP id nk6mr66947114pbc.61.1412042368073; Mon, 29 Sep 2014 18:59:28 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <4232c92a-101c-4a1b-89b2-e80d67ef7bc6@googlegroups.com>
References <4232c92a-101c-4a1b-89b2-e80d67ef7bc6@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Mon, 29 Sep 2014 19:58:47 -0600
Subject Re: JSON-encoding very long iterators
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.14479.1412042377.18130.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1412042377 news.xs4all.nl 2940 [2001:888:2000:d::a6]:50760
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:78477

Show key headers only | View raw


On Mon, Sep 29, 2014 at 7:19 PM,  <alfred@54.org> wrote:
> I would like to add the ability to JSONEncode large iterators. Right now there is no way to do this without modifying the code.
>
> The JSONEncoder.default() doc string suggests to do this:
>         For example, to support arbitrary iterators, you could
>         implement default like this::
>             def default(self, o):
>                 try:
>                     iterable = iter(o)
>                 except TypeError:
>                     pass
>                 else:
>                     return list(iterable)
>                 # Let the base class default method raise the TypeError
>                 return JSONEncoder.default(self, o)
>
> but this method requires the whole serialized object to fit in memory and it's a good chance that your iterator is an iterator to save on memory in the first place.
>
> By changing the code to accept iterators it is then possible to stream json as I did here:
> http://stackoverflow.com/a/26094558/289240
>
> This would ideal if it were included in the standard library. Is there any reason why it shouldn't be?

This would cause things that aren't lists to be encoded as lists.
Sometimes that may be desirable, but in general if e.g. a file object
sneaks its way into your JSON encode call, it is more likely correct
to raise an error than to silently encode the file as if it were a
list of strings.  So it should not be the default behavior. That said,
it sounds like it could be made easier to enable streaming from
iterators as an option for those cases where it's desired.

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

JSON-encoding very long iterators alfred@54.org - 2014-09-29 18:19 -0700
  Re: JSON-encoding very long iterators Ian Kelly <ian.g.kelly@gmail.com> - 2014-09-29 19:58 -0600
    Re: JSON-encoding very long iterators Alfred Morgan <alfred@54.org> - 2014-10-01 03:13 -0700
      Re: JSON-encoding very long iterators Chris Angelico <rosuav@gmail.com> - 2014-10-01 23:06 +1000
        Re: JSON-encoding very long iterators Alfred Morgan <alfred@54.org> - 2014-10-01 15:01 -0700
          Re: JSON-encoding very long iterators Chris Angelico <rosuav@gmail.com> - 2014-10-02 08:54 +1000
            Re: JSON-encoding very long iterators Alfred Morgan <alfred@54.org> - 2014-10-01 23:05 -0700
              Re: JSON-encoding very long iterators Chris Angelico <rosuav@gmail.com> - 2014-10-02 16:33 +1000
                Re: JSON-encoding very long iterators Alfred Morgan <alfred@54.org> - 2014-10-01 23:49 -0700

csiph-web