On Sat, Jan 23, 2016 at 07:46:42PM +0100, Bram Moolenaar wrote:
> 
> Patch 7.4.1154
> Problem:    No support for JSON.
> Solution:   Add jsonencode() and jsondecode().  Also add v:false, v:true,
>             v:null and v:none.

Why do we need both v:null and v:none?

These seem to be unfriendly to vimscript tests:

    :echo v:false == 0
    E685: Internal error: get_tv_number()
    1

    :echo v:true == v:false
    E685: Internal error: get_tv_string_buf()
    E685: Internal error: get_tv_string_buf()
    1

    :if v:true | echo "yes" | endif
    E685: Internal error: get_tv_number()

> *** ../vim-7.4.1153/src/json.c        2016-01-23 19:32:31.331223590 +0100
> --- src/json.c        2016-01-23 19:17:42.068467887 +0100
...
> + /*
> +  * json.c: Encoding and decoding JSON.
> +  *
> +  * Follows this standard: http://www.ietf.org/rfc/rfc4627.txt
> +  */
...
> + json_encode_item(garray_T *gap, typval_T *val, int copyID)
> + {
> +     char_u  numbuf[NUMBUFLEN];
> +     char_u  *res;
> +     list_T  *l;
> +     dict_T  *d;
> + 
> +     switch (val->v_type)
> +     {
> +     case VAR_SPECIAL:
> +         switch(val->vval.v_number)
> +         {
> +             case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
> +             case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
> +             case VVAL_NONE: break;

Wouldn't this produce invalid JSON?  E.g.

  :echo jsonencode({'foo': v:none})
  {"foo":}

I don't think that's RFC-4627 compliant.  Even jsondecode() complains:

  :echo jsondecode(jsonencode({'foo': v:none}))
  E474: Invalid argument

> *** ../vim-7.4.1153/src/if_py_both.h  2016-01-17 22:36:56.017417164 +0100
> --- src/if_py_both.h  2016-01-23 15:26:35.948143016 +0100
> ***************
> *** 810,815 ****
> --- 810,834 ----
>           }
>       }
>       }
> +     else if (our_tv->v_type == VAR_SPECIAL)
> +     {
> +     if (our_tv->vval.v_number == VVAL_FALSE)
> +     {
> +         ret = Py_False;
> +         Py_INCREF(ret);
> +     }
> +     else if (our_tv->vval.v_number == VVAL_TRUE)
> +     {
> +         ret = Py_True;
> +         Py_INCREF(ret);
> +     }
> +     else
> +     {
> +         Py_INCREF(Py_None);
> +         ret = Py_None;
> +     }
> +     return ret;
> +     }
>       else
>       {
>       Py_INCREF(Py_None);

Perhaps pyeval("None") should be changed to return v:none/v:null?  (I
still think these two should be merged into one, with the jsonencode()
semantics of v:null).

And maybe pyeval("True")/pyeval("False") should return v:true/v:false?

> *** ../vim-7.4.1153/runtime/doc/eval.txt      2016-01-21 23:32:14.154035915 
> +0100
> --- runtime/doc/eval.txt      2016-01-23 14:05:58.107086430 +0100
> ***************
> *** 1399,1404 ****
> --- 1409,1418 ----
>       :endtry
>   <           Output: "caught oops".
>   
> +                                     *v:false* *false-variable*
> + v:false             A Number with value zero. Used to put "false" in JSON.  
> See
> +             |jsonencode()|.
> + 
>                                       *v:fcs_reason* *fcs_reason-variable*
>   v:fcs_reason        The reason why the |FileChangedShell| event was 
> triggered.
>               Can be used in an autocommand to decide what to do and/or what
> ***************
> *** 1530,1535 ****
> --- 1546,1559 ----
>               This is the screen column number, like with |virtcol()|.  The
>               value is zero when there was no mouse button click.
>   
> +                                     *v:none* *none-variable*
> + v:none              An empty String. Used to put an empty item in JSON.  See
> +             |jsonencode()|.

There's no concept of "an empty item" in JSON.

> + 
> +                                     *v:null* *null-variable*
> + v:null              An empty String. Used to put "null" in JSON.  See
> +             |jsonencode()|.
> + 
>                                       *v:oldfiles* *oldfiles-variable*
>   v:oldfiles  List of file names that is loaded from the |viminfo| file on
>               startup.  These are the files that Vim remembers marks for.
> ***************
> *** 1695,1700 ****
> --- 1719,1728 ----
>       :endtry
>   <           Output: "Exception from test.vim, line 2"
>   
> +                                             *v:true* *true-variable*
> + v:true              A Number with value one. Used to put "true" in JSON.  
> See
> +             |jsonencode()|.
> + 
>                                               *v:val* *val-variable*
>   v:val               Value of the current item of a |List| or |Dictionary|.  
> Only
>               valid while evaluating the expression used with |map()| and
> ***************
> *** 1901,1906 ****
> --- 1929,1936 ----
>   islocked( {expr})           Number  TRUE if {expr} is locked
>   items( {dict})                      List    key-value pairs in {dict}
>   join( {list} [, {sep}])             String  join {list} items into one 
> String
> + jsondecode( {string})               any     decode JSON
> + jsonencode( {expr})         String  encode JSON
>   keys( {dict})                       List    keys in {dict}
>   len( {expr})                        Number  the length of {expr}
>   libcall( {lib}, {func}, {arg})      String  call {func} in library {lib} 
> with {arg}
> ***************
> *** 4145,4150 ****
> --- 4233,4259 ----
>               converted into a string like with |string()|.
>               The opposite function is |split()|.
>   
> + jsondecode({string})                                        *jsondecode()*
> +             TODO
> + 

This seems incomplete.

> + jsonencode({expr})                                  *jsonencode()*
> +             Encodode {expr} as JSON and return this as a string.

Typo: Encode

> +             The encoding is specified in:
> +             http://www.ietf.org/rfc/rfc4627.txt
> +             Vim values are converted as follows:
> +                Number               decimal number
> +                Float                floating point number
> +                String               in double quotes (possibly null)
> +                Funcref              nothing
> +                List                 as an array (possibly null); when
> +                                     used recursively: []
> +                Dict                 as an object (possibly null); when
> +                                     used recursively: {}
> +                v:false              "false"
> +                v:true               "true"
> +                v:none               nothing
> +                v:null               "null"
> + 
>   keys({dict})                                                *keys()*
>               Return a |List| with all the keys of {dict}.  The |List| is in
>               arbitrary order.

Marius Gedminas
-- 
Did you know that 7/5 people don't know how to use fractions?

-- 
-- 
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.

Attachment: signature.asc
Description: Digital signature

Raspunde prin e-mail lui