2016-02-23 23:27 GMT+03:00 Bram Moolenaar <[email protected]>:
> > David Barnett wrote: > > > `json_encode` accepts special "inf" and "nan" float values and renders > them literally into the encoded JSON: > > ```vim > > :echo json_encode(0.0 / 0.0) > > '-nan' > > ``` > > > > Those values aren't in the JSON spec, and it must have been an > > oversight since those values can't be subsequently decoded: > > ```vim > > :call json_decode(json_encode(0.0 / 0.0)) > > ``` > > ``` > > E474: Invalid argument > > ``` > > Thanks for reporting. json_encode() should produce null, at least > that's when happens in Javascript. > > For js_encode() we can pass on the values. I checked how this is handled in other languages: - Python and Ruby (Python standard library 3.4.3-r1 and 2.7.10-r1, Python demjson 2.2.4, Python simplejson 3.8.1, Ruby 2.0.0_p647-r1) encode infinity as `Infinity` and NaN as `NaN` and is able to parse the whole thing back. I guess they have an agreement - Perl (http://search.cpan.org/dist/JSON/) has the same problem as Vim. - Lua has way too many JSON parsers to test, none of which are standard. dkjson uses `null`. - Haskell’s `aeson` uses `null`, though it again is not the only or the standard package. There are not *that* much packages as with lua here, but I have only aeson because it is used by pandoc and yaml. If that was YAML it would be .nan and .inf (with rather big number of variations like .NaN, .Inf, .INF, .NAN, but not e.g. .InF: these are variants hardcoded in the standard and not case-insensitiviness). Thus I would prefer in order 1. Simply error out. This is better then suddenly changing value type because if there is infinity somewhere then actually it was expected to be a float and immediate error when dumping is better then error in code that tried to use `null` as a float later. 2. Use `null`. Standard in fact has no `Infinity` or `NaN`, so Ruby/Python variant can be rather unexpected. 3. Support Ruby and Python, maybe this will make it into the next standard. I also hope they are not alone. > > > > -- > When I look deep into your eyes, I see JPEG artifacts. > I can tell by the pixels that we're wrong for each other. (xkcd) > > /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net \\\ > /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ > \\\ an exciting new programming language -- http://www.Zimbu.org /// > \\\ help me help AIDS victims -- http://ICCF-Holland.org /// > > — > Reply to this email directly or view it on GitHub > <https://github.com/vim/vim/issues/654#issuecomment-187888445>. > > -- > -- > You received this message from the "vim_dev" maillist. > Do not top-post! Type your reply below the text you are replying to. > For more information, visit http://www.vim.org/maillist.php > > --- > You received this message because you are subscribed to the Google Groups > "vim_dev" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- -- You received this message from the "vim_dev" maillist. Do not top-post! Type your reply below the text you are replying to. For more information, visit http://www.vim.org/maillist.php --- You received this message because you are subscribed to the Google Groups "vim_dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
