]> granicus.if.org Git - vim/commitdiff
patch 7.4.1430 v7.4.1430
authorBram Moolenaar <Bram@vim.org>
Sat, 27 Feb 2016 15:33:22 +0000 (16:33 +0100)
committerBram Moolenaar <Bram@vim.org>
Sat, 27 Feb 2016 15:33:22 +0000 (16:33 +0100)
Problem:    When encoding JSON, turning NaN and Infinity into null without
            giving an error is not useful.
Solution:   Pass NaN and Infinity on.  If the receiver can't handle them it
            will generate the error.

runtime/doc/eval.txt
src/json.c
src/testdir/test_json.vim
src/version.c

index e8462446774e2f66426ca4324eb926c0aa2f5f30..1a1da6e09304b6ac9d99608a4741eabac68a24a8 100644 (file)
@@ -1,4 +1,4 @@
-*eval.txt*     For Vim version 7.4.  Last change: 2016 Feb 23
+*eval.txt*     For Vim version 7.4.  Last change: 2016 Feb 27
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -4561,6 +4561,8 @@ json_encode({expr})                                       *json_encode()*
                Vim values are converted as follows:
                   Number               decimal number
                   Float                floating point number
+                  Float nan            "NaN"
+                  Float inf            "Infinity"
                   String               in double quotes (possibly null)
                   Funcref              not possible, error
                   List                 as an array (possibly null); when
@@ -4571,13 +4573,9 @@ json_encode({expr})                                      *json_encode()*
                   v:true               "true"
                   v:none               "null"
                   v:null               "null"
-               Note that using v:none is permitted, although the JSON
-               standard does not allow empty items.  This can be useful for
-               omitting items in an array:
-                       [0,,,,,5] ~
-               This is much more efficient than:
-                       [0,null,null,null,null,5] ~
-               But a strict JSON parser will not accept it.
+               Note that NaN and Infinity are passed on as values.  This is
+               missing in the JSON standard, but several implementations do
+               allow it.  If not then you will get an error.
 
 keys({dict})                                           *keys()*
                Return a |List| with all the keys of {dict}.  The |List| is in
index 1a3dea1e5bbf330a75d3391d4528ce1d410736fa..a42faec0dba5793a762dc23590845cc961cff53c 100644 (file)
 #  define isnan(x) _isnan(x)
 #  define isinf(x) (!_finite(x) && !_isnan(x))
 # endif
-# if defined(_MSC_VER) && !defined(INFINITY)
+# if !defined(INFINITY) && defined(DBL_MAX)
 #  define INFINITY (DBL_MAX+DBL_MAX)
+# endif
+# if !defined(NAN) && defined(INFINITY)
 #  define NAN (INFINITY-INFINITY)
 # endif
 #endif
@@ -285,12 +287,10 @@ json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
        case VAR_FLOAT:
 #ifdef FEAT_FLOAT
 # if defined(HAVE_MATH_H)
-           if ((options & JSON_JS) && isnan(val->vval.v_float))
+           if (isnan(val->vval.v_float))
                ga_concat(gap, (char_u *)"NaN");
-           else if ((options & JSON_JS) && isinf(val->vval.v_float))
+           else if (isinf(val->vval.v_float))
                ga_concat(gap, (char_u *)"Infinity");
-           else if (isnan(val->vval.v_float) || isinf(val->vval.v_float))
-               ga_concat(gap, (char_u *)"null");
            else
 # endif
            {
index bde7321739078e71b9948852078f3994487b6c6b..6c19b30fd2828c1fc792f31c5d1eef33ab605db5 100644 (file)
@@ -19,11 +19,9 @@ let s:varnr = 1234
 if has('float')
   let s:jsonfl = '12.34'
   let s:varfl = 12.34
-  let s:jsoninf = 'null'
-  let s:jsinf = 'Infinity'
+  let s:jsoninf = 'Infinity'
   let s:varinf = 1.0 / 0.0
-  let s:jsonnan = 'null'
-  let s:jsnan = 'NaN'
+  let s:jsonnan = 'NaN'
   let s:varnan = 0.0 / 0.0
 endif
 
@@ -175,8 +173,8 @@ func Test_js_encode()
   call assert_equal(s:jsonnr, js_encode(s:varnr))
   if has('float')
     call assert_equal(s:jsonfl, js_encode(s:varfl))
-    call assert_equal(s:jsinf, js_encode(s:varinf))
-    call assert_equal(s:jsnan, js_encode(s:varnan))
+    call assert_equal(s:jsoninf, js_encode(s:varinf))
+    call assert_equal(s:jsonnan, js_encode(s:varnan))
   endif
 
   call assert_equal(s:jsonl1, js_encode(s:varl1))
@@ -213,8 +211,8 @@ func Test_js_decode()
   call assert_equal(s:varnr, js_decode(s:jsonnr))
   if has('float')
     call assert_equal(s:varfl, js_decode(s:jsonfl))
-    call assert_equal(s:varinf, js_decode(s:jsinf))
-    call assert_true(isnan(js_decode(s:jsnan)))
+    call assert_equal(s:varinf, js_decode(s:jsoninf))
+    call assert_true(isnan(js_decode(s:jsonnan)))
   endif
 
   call assert_equal(s:varl1, js_decode(s:jsonl1))
index 4fde2fe853014827d9f44af029a08aeefb529172..fc8ae7e91d198c799ebada2ee5164af15e402b2e 100644 (file)
@@ -748,6 +748,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1430,
 /**/
     1429,
 /**/