Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'programmer': 0.03; 'else:': 0.03; 'elif': 0.05; 'json': 0.07; 'transform': 0.07; 'stack.': 0.09; 'runs': 0.10; 'def': 0.12; 'emit': 0.16; 'list)': 0.16; 'nan': 0.16; 'received:74.208.4.195': 0.16; 'wrote:': 0.18; 'trying': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'header :In-Reply-To:1': 0.27; 'function': 0.29; "i'm": 0.30; 'that.': 0.31; '>>>>': 0.31; 'writes:': 0.31; 'probably': 0.32; 'run': 0.32; 'but': 0.35; 'there': 0.35; 'object,': 0.36; 'skip:j 20': 0.36; 'represent': 0.38; 'e.g.': 0.38; 'easiest': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'no.': 0.61; 'simply': 0.61; 'received:74.208': 0.68 Date: Wed, 17 Apr 2013 17:37:40 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Encoding NaN in JSON References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:RLYT1XAvq+x/0ZDv5/j66t0MYeqKXuU0vdwybCvYHUn aUDDYZ/eMP0jH/4glOnYjwvGjphwhyZXqMku+ihxVs1mvKIeza RSJeZ1A8vCNjYtv7TO9OR6mFBdLmwPieMHGcM6Uevcv3TpVEpZ rjtdQ22U5gyOix1Ueqv6qc3AdFBG1s+5ArYqDS80v0E6E8NLPS LSVPcnyNoKDELztS84VFf20yOpgPlILdkYf4A98SvBDvGZn1x6 OpAr76Z9FpWxqiEFy/gdv5n8iv1VzafdC6DaH9ZBycoh3osJcQ 3t3+DIm7cnhYmLg3ZIEU/Atq1ENuYn3oDuwvIwXiDA58AcSCw= = X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366234687 news.xs4all.nl 2255 [2001:888:2000:d::a6]:42601 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43781 On 04/17/2013 03:05 PM, Johann Hibschman wrote: > Miki Tebeka writes: > >>>> I'm trying to find a way to have json emit float('NaN') as 'N/A'. >>> No. There is no way to represent NaN in JSON. It's simply not part of the >>> specification. >> I know that. I'm trying to emit the *string* 'N/A' for every NaN. > > Easiest way is probably to transform your object before you try to write > it, e.g. > > def transform(x): > if isinstance(x, dict): > return dict((k, transform(v)) for k, v in x.items()) > elif isinstance(x, list) or isinstance(x, tuple): > return [transform(v) for v in x] > elif isinstance(x, float) and x != x: > return 'N/A' > else: > return x > Note that for a self-referencing object, this function might run "forever," or until it runs out of stack. The programmer is likely to know about the possibility, but just in case ... > Then just use > > json.dumps(transform(x)) > > rather than just > > json.dumps(x) > -- DaveA