1
0
forked from aniani/vim

patch 7.4.1279

Problem:    jsonencode() is not producing strict JSON.
Solution:   Add jsencode() and jsdecode().  Make jsonencode() and jsondecode()
            strict.
This commit is contained in:
Bram Moolenaar
2016-02-07 19:19:53 +01:00
parent 55fab439a6
commit 595e64e259
12 changed files with 379 additions and 138 deletions

View File

@@ -1,4 +1,4 @@
*channel.txt* For Vim version 7.4. Last change: 2016 Feb 06
*channel.txt* For Vim version 7.4. Last change: 2016 Feb 07
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -16,7 +16,7 @@ The Netbeans interface also uses a channel. |netbeans|
1. Demo |channel-demo|
2. Opening a channel |channel-open|
3. Using a JSON channel |channel-use|
3. Using a JSON or JS channel |channel-use|
4. Vim commands |channel-commands|
5. Using a raw channel |channel-use|
6. Job control |job-control|
@@ -77,6 +77,7 @@ To open a channel: >
"mode" can be: *channel-mode*
"json" - Use JSON, see below; most convenient way. Default.
"js" - Use JavaScript encoding, more efficient than JSON.
"raw" - Use raw messages
*channel-callback*
@@ -86,7 +87,7 @@ message. Example: >
func Handle(handle, msg)
echo 'Received: ' . a:msg
endfunc
let handle = ch_open("localhost:8765", 'json', "Handle")
let handle = ch_open("localhost:8765", {"callback": "Handle"})
"waittime" is the time to wait for the connection to be made in milliseconds.
The default is zero, don't wait, which is useful if the server is supposed to
@@ -95,12 +96,12 @@ be running already. A negative number waits forever.
"timeout" is the time to wait for a request when blocking, using
ch_sendexpr(). Again in milliseconds. The default is 2000 (2 seconds).
When "mode" is "json" the "msg" argument is the body of the received message,
converted to Vim types.
When "mode" is "json" or "js" the "msg" argument is the body of the received
message, converted to Vim types.
When "mode" is "raw" the "msg" argument is the whole message as a string.
When "mode" is "json" the "callback" is optional. When omitted it is only
possible to receive a message after sending one.
When "mode" is "json" or "js" the "callback" is optional. When omitted it is
only possible to receive a message after sending one.
The handler can be added or changed later: >
call ch_setcallback(handle, {callback})
@@ -123,12 +124,15 @@ If there is an error reading or writing a channel it will be closed.
*E896* *E630* *E631*
==============================================================================
3. Using a JSON channel *channel-use*
3. Using a JSON or JS channel *channel-use*
If {mode} is "json" then a message can be sent synchronously like this: >
let response = ch_sendexpr(handle, {expr})
This awaits a response from the other side.
When {mode} is "js" this works the same, except that the messages use
JavaScript encoding. See |jsencode()| for the difference.
To send a message, without handling a response: >
call ch_sendexpr(handle, {expr}, 0)
@@ -231,7 +235,8 @@ Here {number} is the same as what was in the request. Use a negative number
to avoid confusion with message that Vim sends.
{result} is the result of the evaluation and is JSON encoded. If the
evaluation fails it is the string "ERROR".
evaluation fails or the result can't be encoded in JSON it is the string
"ERROR".
Command "expr" ~

View File

@@ -1956,6 +1956,8 @@ job_start({command} [, {options}]) Job start a job
job_status({job}) String get the status of a job
job_stop({job} [, {how}]) Number stop a job
join( {list} [, {sep}]) String join {list} items into one String
jsdecode( {string}) any decode JS style JSON
jsencode( {expr}) String encode JS style JSON
jsondecode( {string}) any decode JSON
jsonencode( {expr}) String encode JSON
keys( {dict}) List keys in {dict}
@@ -2439,7 +2441,6 @@ bufwinnr({expr}) *bufwinnr()*
|:wincmd|.
Only deals with the current tab page.
byte2line({byte}) *byte2line()*
Return the line number that contains the character at byte
count {byte} in the current buffer. This includes the
@@ -2688,7 +2689,7 @@ ch_open({address} [, {argdict}]) *ch_open()*
If {argdict} is given it must be a |Dictionary|. The optional
items are:
mode "raw" or "json".
mode "raw", "js" or "json".
Default "json".
callback function to call for requests with a zero
sequence number. See |channel-callback|.
@@ -4381,17 +4382,33 @@ join({list} [, {sep}]) *join()*
converted into a string like with |string()|.
The opposite function is |split()|.
jsdecode({string}) *jsdecode()*
This is similar to |jsondecode()| with these differences:
- Object key names do not have to be in quotes.
- Empty items in an array (between two commas) are allowed and
result in v:none items.
jsencode({expr}) *jsencode()*
This is similar to |jsonencode()| with these differences:
- Object key names are not in quotes.
- v:none items in an array result in an empty item between
commas.
For example, the Vim object:
[1,v:none,{"one":1}],v:none ~
Will be encoded as:
[1,,{one:1},,] ~
While jsonencode() would produce:
[1,null,{"one":1},null] ~
This encoding is valid for JavaScript. It is more efficient
than JSON, especially when using an array with optional items.
jsondecode({string}) *jsondecode()*
This parses a JSON formatted string and returns the equivalent
in Vim values. See |jsonencode()| for the relation between
JSON and Vim values.
The decoding is permissive:
- A trailing comma in an array and object is ignored.
- An empty item in an array, two commas with nothing or white
space in between, results in v:none.
- When an object member name is not a string it is converted
to a string. E.g. the number 123 is used as the string
"123".
- More floating point numbers are recognized, e.g. "1." for
"1.0".
The result must be a valid Vim type:
@@ -4413,7 +4430,7 @@ jsonencode({expr}) *jsonencode()*
used recursively: {}
v:false "false"
v:true "true"
v:none nothing
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