comparison src/json.c @ 10563:bac9cec298ed v8.0.0171

patch 8.0.0171: JS style JSON does not support single quotes commit https://github.com/vim/vim/commit/ee142add229cbcd58bc76d59f23e02517df14379 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Jan 11 21:50:08 2017 +0100 patch 8.0.0171: JS style JSON does not support single quotes Problem: JS style JSON does not support single quotes. Solution: Allow for single quotes. (Yasuhiro Matsumoto, closes https://github.com/vim/vim/issues/1371)
author Christian Brabandt <cb@256bit.org>
date Wed, 11 Jan 2017 22:00:04 +0100
parents e025378406d1
children dffda1f9b501
comparison
equal deleted inserted replaced
10562:9b1ca542ca91 10563:bac9cec298ed
376 } 376 }
377 fill_numbuflen(reader); 377 fill_numbuflen(reader);
378 } 378 }
379 379
380 static int 380 static int
381 json_decode_string(js_read_T *reader, typval_T *res) 381 json_decode_string(js_read_T *reader, typval_T *res, int quote)
382 { 382 {
383 garray_T ga; 383 garray_T ga;
384 int len; 384 int len;
385 char_u *p; 385 char_u *p;
386 int c; 386 int c;
387 varnumber_T nr; 387 varnumber_T nr;
388 388
389 if (res != NULL) 389 if (res != NULL)
390 ga_init2(&ga, 1, 200); 390 ga_init2(&ga, 1, 200);
391 391
392 p = reader->js_buf + reader->js_used + 1; /* skip over " */ 392 p = reader->js_buf + reader->js_used + 1; /* skip over " or ' */
393 while (*p != '"') 393 while (*p != quote)
394 { 394 {
395 /* The JSON is always expected to be utf-8, thus use utf functions 395 /* The JSON is always expected to be utf-8, thus use utf functions
396 * here. The string is converted below if needed. */ 396 * here. The string is converted below if needed. */
397 if (*p == NUL || p[1] == NUL 397 if (*p == NUL || p[1] == NUL
398 #ifdef FEAT_MBYTE 398 #ifdef FEAT_MBYTE
502 p += len; 502 p += len;
503 } 503 }
504 } 504 }
505 505
506 reader->js_used = (int)(p - reader->js_buf); 506 reader->js_used = (int)(p - reader->js_buf);
507 if (*p == '"') 507 if (*p == quote)
508 { 508 {
509 ++reader->js_used; 509 ++reader->js_used;
510 if (res != NULL) 510 if (res != NULL)
511 { 511 {
512 ga_append(&ga, NUL); 512 ga_append(&ga, NUL);
618 } 618 }
619 } 619 }
620 620
621 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY 621 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
622 && (options & JSON_JS) 622 && (options & JSON_JS)
623 && reader->js_buf[reader->js_used] != '"') 623 && reader->js_buf[reader->js_used] != '"'
624 && reader->js_buf[reader->js_used] != '\'')
624 { 625 {
625 char_u *key; 626 char_u *key;
626 627
627 /* accept an object key that is not in quotes */ 628 /* accept an object key that is not in quotes */
628 key = p = reader->js_buf + reader->js_used; 629 key = p = reader->js_buf + reader->js_used;
688 cur_item = &top_item->jd_key_tv; 689 cur_item = &top_item->jd_key_tv;
689 } 690 }
690 continue; 691 continue;
691 692
692 case '"': /* string */ 693 case '"': /* string */
693 retval = json_decode_string(reader, cur_item); 694 retval = json_decode_string(reader, cur_item, *p);
695 break;
696
697 case '\'':
698 if (options & JSON_JS)
699 retval = json_decode_string(reader, cur_item, *p);
700 else
701 {
702 EMSG(_(e_invarg));
703 retval = FAIL;
704 }
694 break; 705 break;
695 706
696 case ',': /* comma: empty item */ 707 case ',': /* comma: empty item */
697 if ((options & JSON_JS) == 0) 708 if ((options & JSON_JS) == 0)
698 { 709 {