Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.021 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'transform': 0.07; 'yeah,': 0.09; 'def': 0.12; 'from:addr:r.koebler': 0.16; 'from:name:roland koebler': 0.16; 'received:78.47': 0.16; 'skip:j 30': 0.16; 'header :In-Reply-To:1': 0.27; 'class': 0.32; 'probably': 0.32; 'there': 0.35; 'charset:us-ascii': 0.36; 'hi,': 0.36; 'easiest': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'content- disposition:inline': 0.62 Date: Thu, 18 Apr 2013 00:40:30 +0200 From: Roland Koebler To: python-list@python.org Subject: Re: Encoding NaN in JSON References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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: 14 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366238887 news.xs4all.nl 2300 [2001:888:2000:d::a6]:38336 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43785 Hi, > > Easiest way is probably to transform your object before you try to write > Yeah, that's what I ended up doing. Wondered if there's a better way ... yes, there is: subclass+extend the JSON-encoder, see pydoc json. e.g.: class JsonNanEncoder(json.JSONEncoder): def default(self, obj): if some-check-if-obj-is-NaN: return 'NaN' return json.JSONEncoder.default(self, obj) Roland