Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17422
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!multikabel.net!newsfeed10.multikabel.net!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <edriscoll@wisc.edu> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; '*not*': 0.05; 'function,': 0.07; 'json': 0.07; 'content-type:multipart/signed': 0.09; 'filename:fname piece:signature': 0.09; 'newline': 0.09; 'output': 0.10; 'library': 0.13; '2.7': 0.13; 'class,': 0.15; 'ideally': 0.15; '*am*': 0.16; '42,': 0.16; 'content-type:application/pgp- signature': 0.16; 'filename:fname piece:asc': 0.16; 'filename:fname:signature.asc': 0.16; 'incremental': 0.16; 'newlines': 0.16; 'object)': 0.16; 'parallel,': 0.16; 'stdin': 0.16; 'suggested,': 0.16; '>>>': 0.18; 'level,': 0.18; '(most': 0.21; 'input': 0.22; 'end,': 0.23; 'string': 0.24; 'there.': 0.24; 'traceback': 0.24; "i'm": 0.26; '(and': 0.28; 'array': 0.30; 'objects.': 0.30; 'subject:skip:i 10': 0.30; 'typeerror:': 0.30; 'words,': 0.32; 'familiar': 0.32; 'objects': 0.32; 'header:User- Agent:1': 0.33; 'instead': 0.33; 'there': 0.33; 'object': 0.33; 'match': 0.34; 'to:addr:python-list': 0.34; 'last):': 0.34; 'however,': 0.36; 'file': 0.36; '...': 0.36; 'skip:" 10': 0.37; 'two': 0.37; 'but': 0.37; 'run': 0.37; 'received:192': 0.37; 'another': 0.37; 'not,': 0.37; 'received:128': 0.37; 'could': 0.37; 'using': 0.38; 'some': 0.38; 'received:192.168.0': 0.38; 'processing': 0.39; "i'd": 0.39; 'to:addr:python.org': 0.40; 'received:192.168': 0.40; 'communicate': 0.40; 'within': 0.60; 'high': 0.67; 'information:': 0.68; 'transfer': 0.72; 'programs,': 0.80; 'expects': 0.84; 'subject:stream': 0.84 |
| Date | Sat, 17 Dec 2011 19:29:53 -0600 |
| From | Evan Driscoll <edriscoll@wisc.edu> |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Parsing stream of JSON objects incrementally |
| X-Enigmail-Version | 1.3.4 |
| Content-Type | multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enig3835606BD468F277AD2EECDA" |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://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 | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3772.1324171874.27778.python-list@python.org> (permalink) |
| Lines | 74 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1324171874 news.xs4all.nl 6953 [2001:888:2000:d::a6]:35349 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:17422 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
I'm interested in writing two programs, A and B, which communicate using
JSON. At a high level, A wants to transfer an array to B.
However, I would very much like to make it possible for A and B to run
in parallel, so my current plan is to have A output and B read a
*sequence* of JSON objects. In other words, instead of
[ {"a": 0}, {"b":0}, {"c": 0} ]
it would just send
{"a": 0}
{"b": 0}
{"c": 0}
I know about the raw_decode() object inside the json.JSONParser class,
and that gets me most of the way there there.
However, what I'm *not* sure about is the best way to get the input to
the raw_decode() function, which expects a "string or buffer":
>>> d = json.JSONDecoder()
>>> d.raw_decode(sys.stdin)
Traceback (most recent call last):
...
File "json\scanner.py", line 42, in iterscan
match = self.scanner.scanner(string, idx).match
TypeError: expected string or buffer
Now I'm not very familiar with the buffer and how it could be used (and
whether a file or stdin could be used as one in an incremental fashion),
but the best way I can come up with is the following:
1. Read a line of input
2. Try to decode it
3. If not, read another line, concatenate it to the end, and try again
4. etc.
That seems... inelegant at least.
Some other information:
* I'm looking for a 2.7 solution ideally
* I'd prefer not to use a different JSON library entirely
* As suggested, I *am* willing to wait for a newline to do processing
* However, I don't want to require exactly one object per line (and
want to allow both multiple objects on one line and newlines within
an object)
Evan
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Parsing stream of JSON objects incrementally Evan Driscoll <edriscoll@wisc.edu> - 2011-12-17 19:29 -0600 Re: Parsing stream of JSON objects incrementally Miki Tebeka <miki.tebeka@gmail.com> - 2011-12-19 15:18 -0800 Re: Parsing stream of JSON objects incrementally Miki Tebeka <miki.tebeka@gmail.com> - 2011-12-19 15:18 -0800
csiph-web