Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #43721 > unrolled thread
| Started by | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| First post | 2013-04-16 17:21 -0700 |
| Last post | 2013-04-19 12:52 -0700 |
| Articles | 20 on this page of 22 — 11 participants |
Back to article view | Back to comp.lang.python
Encoding NaN in JSON Miki Tebeka <miki.tebeka@gmail.com> - 2013-04-16 17:21 -0700
Re: Encoding NaN in JSON Tim Roberts <timr@probo.com> - 2013-04-16 22:38 -0700
Re: Encoding NaN in JSON Miki Tebeka <miki.tebeka@gmail.com> - 2013-04-17 07:33 -0700
Re: Encoding NaN in JSON John Gordon <gordon@panix.com> - 2013-04-17 14:47 +0000
Re: Encoding NaN in JSON Johann Hibschman <jhibschman@gmail.com> - 2013-04-17 14:05 -0500
Re: Encoding NaN in JSON Miki Tebeka <miki.tebeka@gmail.com> - 2013-04-17 14:21 -0700
Re: Encoding NaN in JSON Roland Koebler <r.koebler@yahoo.de> - 2013-04-18 00:40 +0200
Re: Encoding NaN in JSON Miki Tebeka <miki.tebeka@gmail.com> - 2013-04-17 18:01 -0700
Re: Encoding NaN in JSON Roland Koebler <r.koebler@yahoo.de> - 2013-04-18 03:39 +0200
Re: Encoding NaN in JSON Chris Angelico <rosuav@gmail.com> - 2013-04-18 11:41 +1000
Re: Encoding NaN in JSON Chris Angelico <rosuav@gmail.com> - 2013-04-18 11:46 +1000
Re: Encoding NaN in JSON Roland Koebler <r.koebler@yahoo.de> - 2013-04-18 10:11 +0200
Re: Encoding NaN in JSON Dave Angel <d@davea.name> - 2013-04-17 17:37 -0400
Re: Encoding NaN in JSON Wayne Werner <wayne@waynewerner.com> - 2013-04-18 08:53 -0500
Re: Encoding NaN in JSON Grant Edwards <invalid@invalid.invalid> - 2013-04-19 14:54 +0000
Re: Encoding NaN in JSON Chris “Kwpolska” Warrick <kwpolska@gmail.com> - 2013-04-19 20:21 +0200
Re: Encoding NaN in JSON Grant Edwards <invalid@invalid.invalid> - 2013-04-19 19:42 +0000
Re: Encoding NaN in JSON Chris “Kwpolska” Warrick <kwpolska@gmail.com> - 2013-04-20 10:00 +0200
Re: Encoding NaN in JSON Wayne Werner <wayne@waynewerner.com> - 2013-04-22 13:53 -0500
Re: Encoding NaN in JSON Tim Roberts <timr@probo.com> - 2013-04-18 22:04 -0700
Re: Encoding NaN in JSON Robert Kern <robert.kern@gmail.com> - 2013-04-19 11:43 +0530
Re: Encoding NaN in JSON Miki Tebeka <miki.tebeka@gmail.com> - 2013-04-19 12:52 -0700
Page 1 of 2 [1] 2 Next page →
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2013-04-16 17:21 -0700 |
| Subject | Encoding NaN in JSON |
| Message-ID | <bf3bf709-9cca-4488-a3c7-97b066bb2adf@googlegroups.com> |
Greetings,
I'm trying to find a way to have json emit float('NaN') as 'N/A'.
I can't seem to find a way since NaN is a float, which means overriding "default" won't help.
Any simple way to do this?
Thanks,
--
Miki
[toc] | [next] | [standalone]
| From | Tim Roberts <timr@probo.com> |
|---|---|
| Date | 2013-04-16 22:38 -0700 |
| Message-ID | <k4dsm8dea0gi3gf689p94el7gc6b1vk4bh@4ax.com> |
| In reply to | #43721 |
Miki Tebeka <miki.tebeka@gmail.com> wrote:
>
>I'm trying to find a way to have json emit float('NaN') as 'N/A'.
>I can't seem to find a way since NaN is a float, which means overriding "default" won't help.
>
>Any simple way to do this?
No. There is no way to represent NaN in JSON. It's simply not part of the
specification. From RFC 4627 section 2.4:
Numeric values that cannot be represented as sequences of digits
(such as Infinity and NaN) are not permitted.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
[toc] | [prev] | [next] | [standalone]
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2013-04-17 07:33 -0700 |
| Message-ID | <c37a3b9c-6fe8-48aa-b703-9b4f922c3969@googlegroups.com> |
| In reply to | #43730 |
>> 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.
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-04-17 14:47 +0000 |
| Message-ID | <kkmclv$sbo$1@reader1.panix.com> |
| In reply to | #43762 |
In <c37a3b9c-6fe8-48aa-b703-9b4f922c3969@googlegroups.com> Miki Tebeka <miki.tebeka@gmail.com> 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.
import math
x = possibly_NaN()
if math.isnan(x):
x = 'N/A'
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Johann Hibschman <jhibschman@gmail.com> |
|---|---|
| Date | 2013-04-17 14:05 -0500 |
| Message-ID | <xjrkgehe9yo3p.fsf@gmail.com> |
| In reply to | #43762 |
Miki Tebeka <miki.tebeka@gmail.com> 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
Then just use
json.dumps(transform(x))
rather than just
json.dumps(x)
[toc] | [prev] | [next] | [standalone]
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2013-04-17 14:21 -0700 |
| Message-ID | <efca95c6-156e-4959-9d90-a67421494547@googlegroups.com> |
| In reply to | #43776 |
> >>> I'm trying to find a way to have json emit float('NaN') as 'N/A'.
> 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 ...
Thanks,
--
Miki
[toc] | [prev] | [next] | [standalone]
| From | Roland Koebler <r.koebler@yahoo.de> |
|---|---|
| Date | 2013-04-18 00:40 +0200 |
| Message-ID | <mailman.738.1366238887.3114.python-list@python.org> |
| In reply to | #43779 |
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
[toc] | [prev] | [next] | [standalone]
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2013-04-17 18:01 -0700 |
| Message-ID | <1040891b-d350-4304-8112-de4a383562dc@googlegroups.com> |
| In reply to | #43785 |
[Roland] > yes, there is: subclass+extend the JSON-encoder, see pydoc json. Please read the original post before answering. What you suggested does not work since NaN is of float type.
[toc] | [prev] | [next] | [standalone]
| From | Roland Koebler <r.koebler@yahoo.de> |
|---|---|
| Date | 2013-04-18 03:39 +0200 |
| Message-ID | <mailman.746.1366249181.3114.python-list@python.org> |
| In reply to | #43791 |
Hi,
> > yes, there is: subclass+extend the JSON-encoder, see pydoc json.
> Please read the original post before answering. What you suggested does not work since NaN is of float type.
ok, right, default does not work this way.
But I would still suggest to extend the JSON-encoder, since that is
quite simple (see sourcecode of JSON module); as a quickhack, you
could even monkey patch json.encoder.floatstr with a wrapper which
returns "N/A" for NaN. (I've tested it: It works.)
But: If you only need NaN and inf, and are ok with 'NaN' instead of 'N/A',
you can simply use the json module. See pydoc json:
If allow_nan is True, then NaN, Infinity, and -Infinity will be
encoded as such. This behavior is not JSON specification compliant,
but is consistent with most JavaScript based encoders and decoders.
Otherwise, it will be a ValueError to encode such floats.
>>> import json
>>> json.dumps(float('NaN'))
'NaN'
>>> json.dumps(float('inf'))
'Infinity'
Roland
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-04-18 11:41 +1000 |
| Message-ID | <mailman.747.1366249316.3114.python-list@python.org> |
| In reply to | #43791 |
On Thu, Apr 18, 2013 at 11:01 AM, Miki Tebeka <miki.tebeka@gmail.com> wrote: > [Roland] >> yes, there is: subclass+extend the JSON-encoder, see pydoc json. > Please read the original post before answering. What you suggested does not work since NaN is of float type. You may be able to override a bit more of the code, though. Check out Lib/json/encoder.py for the implementation, and have a look at the floatstr() internal function; unfortunately you can't simply subclass and override that, but perhaps overriding iterencode (which is where floatstr is defined) would do the job. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-04-18 11:46 +1000 |
| Message-ID | <mailman.748.1366249600.3114.python-list@python.org> |
| In reply to | #43791 |
On Thu, Apr 18, 2013 at 11:39 AM, Roland Koebler <r.koebler@yahoo.de> wrote: > as a quickhack, you > could even monkey patch json.encoder.floatstr with a wrapper which > returns "N/A" for NaN. (I've tested it: It works.) Wait... you can do that? It's internal to iterencode, at least in Python 3.3 and 2.7 that I'm looking at here. Can you share your code please? I'd like to try that! When I first looked at the docstring, I was thinking "Ah, can I override the bit that emits NaN to return \"N/A\" instead?", but the code made me think that's not possible. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Roland Koebler <r.koebler@yahoo.de> |
|---|---|
| Date | 2013-04-18 10:11 +0200 |
| Message-ID | <mailman.756.1366272710.3114.python-list@python.org> |
| In reply to | #43791 |
On Thu, Apr 18, 2013 at 11:46:37AM +1000, Chris Angelico wrote: > Wait... you can do that? It's internal to iterencode, at least in > Python 3.3 and 2.7 that I'm looking at here. In Python 2.6 it wasn't internal to iterencode; in Python 2.7 and 3.x you probably would have to monkey-patch iterencode. (In addition, patching floatstr alone wouldn't be enough in 3.x and probably 2.7, since you also have to make sure that the C-extension is not used here.) BUT: Keep in mind that monkey-patches are problematic, and should be avoided (or used very carefully) in production code. So, better replace the complete encoder.py or use your own patched version of the complete json-module. Roland
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2013-04-17 17:37 -0400 |
| Message-ID | <mailman.734.1366234687.3114.python-list@python.org> |
| In reply to | #43776 |
On 04/17/2013 03:05 PM, Johann Hibschman wrote:
> Miki Tebeka <miki.tebeka@gmail.com> 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
[toc] | [prev] | [next] | [standalone]
| From | Wayne Werner <wayne@waynewerner.com> |
|---|---|
| Date | 2013-04-18 08:53 -0500 |
| Message-ID | <mailman.770.1366293244.3114.python-list@python.org> |
| In reply to | #43762 |
On Wed, 17 Apr 2013, Miki Tebeka wrote:
>>> 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.
Why not use `null` instead? It seems to be semantically more similar...
-W
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2013-04-19 14:54 +0000 |
| Message-ID | <kkrlr4$brs$1@reader1.panix.com> |
| In reply to | #43829 |
On 2013-04-18, Wayne Werner <wayne@waynewerner.com> wrote:
> On Wed, 17 Apr 2013, Miki Tebeka wrote:
>
>>>> 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.
>
> Why not use `null` instead? It seems to be semantically more similar...
Why not use 'NaN' instead? It seems to be even more semantically
similar...
--
Grant Edwards grant.b.edwards Yow! I want a VEGETARIAN
at BURRITO to go ... with
gmail.com EXTRA MSG!!
[toc] | [prev] | [next] | [standalone]
| From | Chris “Kwpolska” Warrick <kwpolska@gmail.com> |
|---|---|
| Date | 2013-04-19 20:21 +0200 |
| Message-ID | <mailman.836.1366395732.3114.python-list@python.org> |
| In reply to | #43903 |
On Fri, Apr 19, 2013 at 4:54 PM, Grant Edwards <invalid@invalid.invalid> wrote:
> On 2013-04-18, Wayne Werner <wayne@waynewerner.com> wrote:
>> On Wed, 17 Apr 2013, Miki Tebeka wrote:
>>
>>>>> 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.
>>
>> Why not use `null` instead? It seems to be semantically more similar...
>
> Why not use 'NaN' instead? It seems to be even more semantically
> similar...
Because there is no NaN in JSON? Unless you mean a string, which
makes no semantical sense and is human-oriented and not
machine-oriented.
--
Kwpolska <http://kwpolska.tk> | GPG KEY: 5EAAEA16
stop html mail | always bottom-post
http://asciiribbon.org | http://caliburn.nl/topposting.html
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2013-04-19 19:42 +0000 |
| Message-ID | <kks6np$mvt$1@reader1.panix.com> |
| In reply to | #43925 |
On 2013-04-19, Chris ???Kwpolska??? Warrick <kwpolska@gmail.com> wrote:
> On Fri, Apr 19, 2013 at 4:54 PM, Grant Edwards <invalid@invalid.invalid> wrote:
>> On 2013-04-18, Wayne Werner <wayne@waynewerner.com> wrote:
>>> On Wed, 17 Apr 2013, Miki Tebeka wrote:
>>>
>>>>>> 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.
>>>
>>> Why not use `null` instead? It seems to be semantically more similar...
>>
>> Why not use 'NaN' instead? It seems to be even more semantically
>> similar...
>
> Because there is no NaN in JSON? Unless you mean a string, which
> makes no semantical sense and is human-oriented and not
> machine-oriented.
The OP asked for a string, and I thought you were proposing the string
'null'. If one is to use a string, then 'NaN' makes the most sense,
since it can be converted back into a floating point NaN object.
I infer that you were proposing a JSON null value and not the string
'null'?
--
Grant Edwards grant.b.edwards Yow! I'm receiving a coded
at message from EUBIE BLAKE!!
gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Chris “Kwpolska” Warrick <kwpolska@gmail.com> |
|---|---|
| Date | 2013-04-20 10:00 +0200 |
| Message-ID | <mailman.850.1366445217.3114.python-list@python.org> |
| In reply to | #43933 |
On Fri, Apr 19, 2013 at 9:42 PM, Grant Edwards <invalid@invalid.invalid> wrote: > The OP asked for a string, and I thought you were proposing the string > 'null'. If one is to use a string, then 'NaN' makes the most sense, > since it can be converted back into a floating point NaN object. > > I infer that you were proposing a JSON null value and not the string > 'null'? Not me, Wayne Werner proposed to use the JSON null value. I parsed the backticks (`) used by him as a way to delimit it from text and not as a string. PS. > On 2013-04-19, Chris ???Kwpolska??? Warrick <kwpolska@gmail.com> wrote: Is Unicode support so hard, especially in the 21st century? -- Kwpolska <http://kwpolska.tk> | GPG KEY: 5EAAEA16 stop html mail | always bottom-post http://asciiribbon.org | http://caliburn.nl/topposting.html
[toc] | [prev] | [next] | [standalone]
| From | Wayne Werner <wayne@waynewerner.com> |
|---|---|
| Date | 2013-04-22 13:53 -0500 |
| Message-ID | <mailman.935.1366656809.3114.python-list@python.org> |
| In reply to | #43933 |
[Multipart message — attachments visible in raw view] — view raw
On Sat, 20 Apr 2013, Chris “Kwpolska” Warrick wrote:
> On Fri, Apr 19, 2013 at 9:42 PM, Grant Edwards <invalid@invalid.invalid> wrote:
>> The OP asked for a string, and I thought you were proposing the string
>> 'null'. If one is to use a string, then 'NaN' makes the most sense,
>> since it can be converted back into a floating point NaN object.
>>
>> I infer that you were proposing a JSON null value and not the string
>> 'null'?
>
> Not me, Wayne Werner proposed to use the JSON null value. I parsed
> the backticks (`) used by him as a way to delimit it from text and not
> as a string.
That was, in fact, my intention. Though it seems to me that you'll have to
suffer between some sort of ambiguity - in Chrome, at least,
`Number(null)` evaluates to `0` instead of NaN. But `Number('Whatever')`
evaluates to NaN. However, a JSON parser obviously wouldn't be able to
make the semantic distinction, so I think you'll be left with whichever
API makes the most sense to you:
NaN maps to null
or
NaN maps to "NaN" (or any other string, really)
Obviously you're not limited to these particular choices, but they're
probably the easiest to implement and communicate.
HTH,
-W
[toc] | [prev] | [next] | [standalone]
| From | Tim Roberts <timr@probo.com> |
|---|---|
| Date | 2013-04-18 22:04 -0700 |
| Message-ID | <n1k1n8t0gsuv0dvt6amllmn6u0vmng4jnf@4ax.com> |
| In reply to | #43762 |
Miki Tebeka <miki.tebeka@gmail.com> wrote:
>
>>> 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.
You understand that this will result in a chunk of text that is not JSON?
Other JSON readers won't be able to read it.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.python
csiph-web