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


Groups > comp.lang.python > #39678

Re: Good cross-version ASCII serialisation protocol for simple types

Path csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'string.': 0.04; 'encoded': 0.05; 'subject:skip:s 10': 0.05; 'json': 0.07; 'mode:': 0.07; 'python': 0.09; 'encode': 0.09; 'non-ascii': 0.09; 'subject:version': 0.09; 'resulting': 0.13; 'result.': 0.15; '2.6:': 0.16; '24,': 0.16; 'ascii,': 0.16; 'covered,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hmm.': 0.16; 'skip:j 30': 0.16; 'subject:simple': 0.16; 'wrote:': 0.17; 'bytes': 0.17; 'unicode': 0.17; '>>>': 0.18; 'input': 0.18; 'feb': 0.19; 'mostly': 0.20; 'fairly': 0.21; 'fine,': 0.22; 'simpler': 0.22; "i'd": 0.22; '(i.e.,': 0.23; 'paul': 0.24; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'message- id:@mail.gmail.com': 0.27; 'types.': 0.29; 'use?': 0.29; 'convert': 0.29; "i'm": 0.29; "skip:' 10": 0.30; "skip:' 20": 0.32; 'could': 0.32; 'skip:s 30': 0.33; 'skip:j 20': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'likely': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'characters': 0.36; 'option': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'subject:-': 0.40; 'skip:u 10': 0.60; "you'll": 0.62; 'between': 0.63; '2013': 0.84; 'inefficient': 0.91; 'subject:Good': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=VInMBpp3yKEWdBfoevbLrEUC8x/kjswJZRHsJacdOlQ=; b=zyYz4P1g+BhA4BXVvtMQBhH9SfbQ3pePCiAINC+AP+QlEVbFKCUM7fdFUd/lHLIekX 6byIDsSW8DLagF64NbhgBYqUrVTaUAsBi9/WANpHFQliE1EZbG48DVw2f6uNfmq5aAlS SqmsY+QWLnJDN3AfYBezRb9ZcUtUdPzMsrxCff+Trm+UJ0YtOYTRamSUO7bSRYqDFA+4 MyBChxetlXiXYxpUUCuMpfgAR08yzvrT3TRDIvWxNpzDX9jk0qbLJCn6j9cVQ4sq1/jF ALDeqUX1KKTD3zcr7J/e/vWZtgIkiC4AwSMbqNyIX3mdx74KCgATX4O/9uAwGL1n1kmy 3IKg==
MIME-Version 1.0
X-Received by 10.52.37.109 with SMTP id x13mr6829434vdj.10.1361635210098; Sat, 23 Feb 2013 08:00:10 -0800 (PST)
In-Reply-To <f1d3acb6-94e2-48f6-8ccd-042b929d0ef4@googlegroups.com>
References <f1d3acb6-94e2-48f6-8ccd-042b929d0ef4@googlegroups.com>
Date Sun, 24 Feb 2013 03:00:10 +1100
Subject Re: Good cross-version ASCII serialisation protocol for simple types
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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 <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.2353.1361635213.2939.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1361635213 news.xs4all.nl 6947 [2001:888:2000:d::a6]:60047
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:39678

Show key headers only | View raw


On Sun, Feb 24, 2013 at 2:45 AM, Paul  Moore <p.f.moore@gmail.com> wrote:
> At the moment, I'm using
>
> encoded = json.dumps([ord(c) for c in json.dumps(obj)])
> decoded = json.loads(''.join([chr(n) for n in json.loads(encoded)]))
>
> The double-encoding ensures that non-ASCII characters don't make it into the result.
>
> This works fine, but is there something simpler (i.e., less of a hack!) that I could use? (Base64 and the like don't work because they encode bytes->strings, not strings->strings).

Hmm. How likely is it that you'll have non-ASCII characters in the
input? If they're fairly uncommon, you could use UTF-7 - it's fairly
space-efficient when the input is mostly ASCII, but inefficient on
other characters.

Not sure what the problem is with bytes vs strings; you can always do
an encode("ascii") or decode("ascii") to convert 7-bit strings between
those types.

With that covered, I'd just go with a single JSON packaging, and work
with the resulting Unicode string.

Python 2.6:
>>> s=u"asdf\u1234zxcv"
>>> s.encode("utf-7").decode("ascii")
u'asdf+EjQ-zxcv'

Python 3.3:
>>> s=u"asdf\u1234zxcv"
>>> s.encode("utf-7").decode("ascii")
'asdf+EjQ-zxcv'

Another option would be to JSON-encode in pure-ASCII mode:

>>> json.dumps([s],ensure_ascii=True)
'["asdf\\u1234zxcv"]'

Would that cover it?

ChrisA

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


Thread

Good cross-version ASCII serialisation protocol for simple types Paul  Moore <p.f.moore@gmail.com> - 2013-02-23 07:45 -0800
  Re: Good cross-version ASCII serialisation protocol for simple types Chris Angelico <rosuav@gmail.com> - 2013-02-24 03:00 +1100
  Re: Good cross-version ASCII serialisation protocol for simple types Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2013-02-23 17:05 +0100
  Re: Good cross-version ASCII serialisation protocol for simple types Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-02-23 18:06 +0200
    Re: Good cross-version ASCII serialisation protocol for simple types Paul  Moore <p.f.moore@gmail.com> - 2013-02-23 11:00 -0800

csiph-web