Mercurial > vim
comparison src/eval.c @ 7967:45ea5ebf3a98 v7.4.1279
commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Feb 7 19:19:53 2016 +0100
patch 7.4.1279
Problem: jsonencode() is not producing strict JSON.
Solution: Add jsencode() and jsdecode(). Make jsonencode() and jsondecode()
strict.
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sun, 07 Feb 2016 19:30:05 +0100 |
parents | d96e38f35e2d |
children | 7224f5e9c36a |
comparison
equal
deleted
inserted
replaced
7966:79c5a86fcdfe | 7967:45ea5ebf3a98 |
---|---|
626 static void f_job_start(typval_T *argvars, typval_T *rettv); | 626 static void f_job_start(typval_T *argvars, typval_T *rettv); |
627 static void f_job_stop(typval_T *argvars, typval_T *rettv); | 627 static void f_job_stop(typval_T *argvars, typval_T *rettv); |
628 static void f_job_status(typval_T *argvars, typval_T *rettv); | 628 static void f_job_status(typval_T *argvars, typval_T *rettv); |
629 #endif | 629 #endif |
630 static void f_join(typval_T *argvars, typval_T *rettv); | 630 static void f_join(typval_T *argvars, typval_T *rettv); |
631 static void f_jsdecode(typval_T *argvars, typval_T *rettv); | |
632 static void f_jsencode(typval_T *argvars, typval_T *rettv); | |
631 static void f_jsondecode(typval_T *argvars, typval_T *rettv); | 633 static void f_jsondecode(typval_T *argvars, typval_T *rettv); |
632 static void f_jsonencode(typval_T *argvars, typval_T *rettv); | 634 static void f_jsonencode(typval_T *argvars, typval_T *rettv); |
633 static void f_keys(typval_T *argvars, typval_T *rettv); | 635 static void f_keys(typval_T *argvars, typval_T *rettv); |
634 static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv); | 636 static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv); |
635 static void f_len(typval_T *argvars, typval_T *rettv); | 637 static void f_len(typval_T *argvars, typval_T *rettv); |
8204 {"job_start", 1, 2, f_job_start}, | 8206 {"job_start", 1, 2, f_job_start}, |
8205 {"job_status", 1, 1, f_job_status}, | 8207 {"job_status", 1, 1, f_job_status}, |
8206 {"job_stop", 1, 1, f_job_stop}, | 8208 {"job_stop", 1, 1, f_job_stop}, |
8207 #endif | 8209 #endif |
8208 {"join", 1, 2, f_join}, | 8210 {"join", 1, 2, f_join}, |
8211 {"jsdecode", 1, 1, f_jsdecode}, | |
8212 {"jsencode", 1, 1, f_jsencode}, | |
8209 {"jsondecode", 1, 1, f_jsondecode}, | 8213 {"jsondecode", 1, 1, f_jsondecode}, |
8210 {"jsonencode", 1, 1, f_jsonencode}, | 8214 {"jsonencode", 1, 1, f_jsonencode}, |
8211 {"keys", 1, 1, f_keys}, | 8215 {"keys", 1, 1, f_keys}, |
8212 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */ | 8216 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */ |
8213 {"len", 1, 1, f_len}, | 8217 {"len", 1, 1, f_len}, |
9827 char_u *p; | 9831 char_u *p; |
9828 char *rest; | 9832 char *rest; |
9829 int port; | 9833 int port; |
9830 int waittime = 0; | 9834 int waittime = 0; |
9831 int timeout = 2000; | 9835 int timeout = 2000; |
9832 int json_mode = TRUE; | 9836 ch_mode_T ch_mode = MODE_JSON; |
9833 int ch_idx; | 9837 int ch_idx; |
9834 | 9838 |
9835 /* default: fail */ | 9839 /* default: fail */ |
9836 rettv->vval.v_number = -1; | 9840 rettv->vval.v_number = -1; |
9837 | 9841 |
9866 | 9870 |
9867 if (dict_find(dict, (char_u *)"mode", -1) != NULL) | 9871 if (dict_find(dict, (char_u *)"mode", -1) != NULL) |
9868 { | 9872 { |
9869 mode = get_dict_string(dict, (char_u *)"mode", FALSE); | 9873 mode = get_dict_string(dict, (char_u *)"mode", FALSE); |
9870 if (STRCMP(mode, "raw") == 0) | 9874 if (STRCMP(mode, "raw") == 0) |
9871 json_mode = FALSE; | 9875 ch_mode = MODE_RAW; |
9872 else if (STRCMP(mode, "json") != 0) | 9876 else if (STRCMP(mode, "js") == 0) |
9877 ch_mode = MODE_JS; | |
9878 else if (STRCMP(mode, "json") == 0) | |
9879 ch_mode = MODE_JSON; | |
9880 else | |
9873 { | 9881 { |
9874 EMSG2(_(e_invarg2), mode); | 9882 EMSG2(_(e_invarg2), mode); |
9875 return; | 9883 return; |
9876 } | 9884 } |
9877 } | 9885 } |
9889 } | 9897 } |
9890 | 9898 |
9891 ch_idx = channel_open((char *)address, port, waittime, NULL); | 9899 ch_idx = channel_open((char *)address, port, waittime, NULL); |
9892 if (ch_idx >= 0) | 9900 if (ch_idx >= 0) |
9893 { | 9901 { |
9894 channel_set_json_mode(ch_idx, json_mode); | 9902 channel_set_json_mode(ch_idx, ch_mode); |
9895 channel_set_timeout(ch_idx, timeout); | 9903 channel_set_timeout(ch_idx, timeout); |
9896 if (callback != NULL && *callback != NUL) | 9904 if (callback != NULL && *callback != NUL) |
9897 channel_set_callback(ch_idx, callback); | 9905 channel_set_callback(ch_idx, callback); |
9898 } | 9906 } |
9899 rettv->vval.v_number = ch_idx; | 9907 rettv->vval.v_number = ch_idx; |
9944 /* return an empty string by default */ | 9952 /* return an empty string by default */ |
9945 rettv->v_type = VAR_STRING; | 9953 rettv->v_type = VAR_STRING; |
9946 rettv->vval.v_string = NULL; | 9954 rettv->vval.v_string = NULL; |
9947 | 9955 |
9948 id = channel_get_id(); | 9956 id = channel_get_id(); |
9949 text = json_encode_nr_expr(id, &argvars[1]); | 9957 text = json_encode_nr_expr(id, &argvars[1], 0); |
9950 if (text == NULL) | 9958 if (text == NULL) |
9951 return; | 9959 return; |
9952 | 9960 |
9953 ch_idx = send_common(argvars, text, id, "sendexpr"); | 9961 ch_idx = send_common(argvars, text, id, "sendexpr"); |
9954 vim_free(text); | 9962 vim_free(text); |
14441 else | 14449 else |
14442 rettv->vval.v_string = NULL; | 14450 rettv->vval.v_string = NULL; |
14443 } | 14451 } |
14444 | 14452 |
14445 /* | 14453 /* |
14446 * "jsondecode()" function | 14454 * "jsdecode()" function |
14447 */ | 14455 */ |
14448 static void | 14456 static void |
14449 f_jsondecode(typval_T *argvars, typval_T *rettv) | 14457 f_jsdecode(typval_T *argvars, typval_T *rettv) |
14450 { | 14458 { |
14451 js_read_T reader; | 14459 js_read_T reader; |
14452 | 14460 |
14453 reader.js_buf = get_tv_string(&argvars[0]); | 14461 reader.js_buf = get_tv_string(&argvars[0]); |
14454 reader.js_fill = NULL; | 14462 reader.js_fill = NULL; |
14455 reader.js_used = 0; | 14463 reader.js_used = 0; |
14456 if (json_decode_all(&reader, rettv) != OK) | 14464 if (json_decode_all(&reader, rettv, JSON_JS) != OK) |
14457 EMSG(_(e_invarg)); | 14465 EMSG(_(e_invarg)); |
14458 } | 14466 } |
14459 | 14467 |
14460 /* | 14468 /* |
14469 * "jsencode()" function | |
14470 */ | |
14471 static void | |
14472 f_jsencode(typval_T *argvars, typval_T *rettv) | |
14473 { | |
14474 rettv->v_type = VAR_STRING; | |
14475 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS); | |
14476 } | |
14477 | |
14478 /* | |
14479 * "jsondecode()" function | |
14480 */ | |
14481 static void | |
14482 f_jsondecode(typval_T *argvars, typval_T *rettv) | |
14483 { | |
14484 js_read_T reader; | |
14485 | |
14486 reader.js_buf = get_tv_string(&argvars[0]); | |
14487 reader.js_fill = NULL; | |
14488 reader.js_used = 0; | |
14489 if (json_decode_all(&reader, rettv, 0) != OK) | |
14490 EMSG(_(e_invarg)); | |
14491 } | |
14492 | |
14493 /* | |
14461 * "jsonencode()" function | 14494 * "jsonencode()" function |
14462 */ | 14495 */ |
14463 static void | 14496 static void |
14464 f_jsonencode(typval_T *argvars, typval_T *rettv) | 14497 f_jsonencode(typval_T *argvars, typval_T *rettv) |
14465 { | 14498 { |
14466 rettv->v_type = VAR_STRING; | 14499 rettv->v_type = VAR_STRING; |
14467 rettv->vval.v_string = json_encode(&argvars[0]); | 14500 rettv->vval.v_string = json_encode(&argvars[0], 0); |
14468 } | 14501 } |
14469 | 14502 |
14470 /* | 14503 /* |
14471 * "keys()" function | 14504 * "keys()" function |
14472 */ | 14505 */ |