Fix JSON NaN
authorJo-Philipp Wich <jow@openwrt.org>
Mon, 16 Apr 2012 16:48:59 +0000 (16:48 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Mon, 16 Apr 2012 16:48:59 +0000 (16:48 +0000)
Hi,

The attached patch fixes the JSON generation when dealing with NaN (not
a number), this makes the JSON parsing in the web browser succeed
(before it would get a "nan" which is not a valid JS value)

Chris

libs/web/luasrc/http.lua

index 85854054840f5078ed71199f21e930e94f5be893..60a3e0722808dd146c8bbe9306f659dc366925a0 100644 (file)
@@ -333,7 +333,12 @@ function write_json(x)
                        write(" }")
                end
        elseif type(x) == "number" or type(x) == "boolean" then
-               write(tostring(x))
+               if (x ~= x) then
+                       -- NaN is the only value that doesn't equal to itself.   
+                       write("Number.NaN")
+               else
+                       write(tostring(x))
+               end
        elseif type(x) == "string" then
                write("%q" % tostring(x))
        end