Encoding NaN in JSON

Wayne Werner wayne at waynewerner.com
Mon Apr 22 14:53:13 EDT 2013


On Sat, 20 Apr 2013, Chris “Kwpolska” Warrick wrote:

> On Fri, Apr 19, 2013 at 9:42 PM, Grant Edwards <invalid at 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


More information about the Python-list mailing list