/* vi:set ts=8 sts=4 sw=4:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* json.c: Encoding and decoding JSON.
*
* Follows this standard: https://tools.ietf.org/html/rfc7159.html
*/
#include "vim.h"
#if defined(FEAT_EVAL) || defined(PROTO)
#if defined(FEAT_FLOAT)
# include <float.h>
# if defined(HAVE_MATH_H)
/* for isnan() and isinf() */
# include <math.h>
# endif
# if defined(WIN32) && !defined(isnan)
# define isnan(x) _isnan(x)
# define isinf(x) (!_finite(x) && !_isnan(x))
# endif
# if !defined(INFINITY) && defined(DBL_MAX)
# define INFINITY (DBL_MAX+DBL_MAX)
# endif
# if !defined(NAN) && defined(INFINITY)
# define NAN (INFINITY-INFINITY)
# endif
#endif
static int json_encode_item(garray_T *gap, typval_T *val, int copyID, int options);
static int json_decode_item(js_read_T *reader, typval_T *res, int options);
/*
* Encode "val" into a JSON format string.
* The result is in allocated memory.
* The result is empty when encoding fails.
* "options" can be JSON_JS or zero;
*/
char_u *
json_encode(typval_T *val, int options)
{
garray_T ga;
/* Store bytes in the growarray. */
ga_init2(&ga, 1, 4000);
if (json_encode_item(&ga, val, get_copyID(), options) == FAIL)
{
vim_free(ga.ga_data);
return vim_strsave((char_u *)"");
}
return ga.ga_data;
}
/*
* Encode ["nr", "val"] into a JSON format string in allocated memory.
* "options" can be JSON_JS or zero;
* Returns NULL when out of memory.
*/
char_u *
json_encode_nr_expr(int nr, typval_T *val, int options)
{
typval_T listtv;
typval_T nrtv;
char_u *text;
nrtv.v_type = VAR_NUMBER;
nrtv.vval.v_number = nr;
if (rettv_list_alloc(&listtv) == FAIL)
return NULL;
if (list_append_tv(listtv.vval.v_list, &nrtv) == FAIL
|| list_append_tv(listtv.vval.v_list, val) == FAIL)
{
list_unref(listtv.vval.v_list);
return NULL;
}
text = json_encode(&listtv, options);
list_unref(listtv.vval.v_list);
return text;
}
static void
write_string(garray_T *gap, char_u *str)
{
char_u *res = str;
char_u numbuf[NUMBUFLEN];
if (res == NULL)
ga_concat(gap, (char_u *)"null");
else
{
#if defined(FEAT_MBYTE) && defined(USE_ICONV)
vimconv_T conv;
char_u *converted = NULL;
convert_setup(&conv, p_enc, (char_u*)"utf-8");
if (conv.vc_type != CONV_NONE)
converted = res = string_convert(&conv, res, NULL);
convert_setup(&conv, NULL, NULL);
#endif
ga_append(gap, '"');
while (*res != NUL)
{
int c;
#ifdef FEAT_MBYTE
/* always use utf-8 encoding, ignore 'encoding' */
c = utf_ptr2char(res);
#else
c = (int)*(p);
#endif
switch (c)
{
case 0x08:
ga_append(gap, '\\'); ga_append(gap, 'b'); break;
case 0x09:
ga_append(gap, '\\'); ga_append(gap, 't'); break;
case 0x0a:
ga_append(gap, '\\'); ga_append(gap, 'n'); break;
case 0x0c:
ga_append(gap, '\\'); ga_append(gap, 'f'); break;
case 0x0d:
ga_append(gap, '\\'); ga_append(gap, 'r'); break;
case 0x22: /* " */
case 0x5c: /* \ */
ga_append(gap, '\\');
ga_append(gap, c);
break;
default:
if (c >= 0x20)
{
#ifdef FEAT_MBYTE
numbuf[utf_char2bytes(c, numbuf)] = NUL;
#else
numbuf[0] = c;
numbuf[1] = NUL;
#endif
ga_concat(gap, numbuf);
}
else
{
vim_snprintf((char *)numbuf, NUMBUFLEN,
"\\u%04lx", (long)c);
ga_concat(gap, numbuf);
}
}
#ifdef FEAT_MBYTE
res += utf_ptr2len(res);
#else
++p;
#endif
}
ga_append(gap, '"');
#if defined(FEAT_MBYTE) && defined(USE_ICONV)
vim_free(converted);
#endif
}
}
/*
* Return TRUE if "key" can be used without quotes.
* That is when it starts with a letter and only contains letters, digits and
* underscore.
*/
static int
is_simple_key(char_u *key)
{
char_u *p;
if (!ASCII_ISALPHA(*key))
return FALSE;
for (p = key + 1; *p != NUL; ++p)
if (!ASCII_ISALPHA(*p) && *p != '_' &&