|
89 | 89 | #define DEFAULT_ENCODE_NUMBER_PRECISION 14 |
90 | 90 | #define DEFAULT_ENCODE_EMPTY_TABLE_AS_OBJECT 1 |
91 | 91 | #define DEFAULT_DECODE_ARRAY_WITH_ARRAY_MT 0 |
| 92 | +#define DEFAULT_DECODE_ALLOW_COMMENTS 0 |
92 | 93 | #define DEFAULT_ENCODE_ESCAPE_FORWARD_SLASH 1 |
93 | 94 | #define DEFAULT_ENCODE_SKIP_UNSUPPORTED_VALUE_TYPES 0 |
94 | 95 | #define DEFAULT_ENCODE_INDENT NULL |
@@ -178,6 +179,7 @@ typedef struct { |
178 | 179 | int decode_invalid_numbers; |
179 | 180 | int decode_max_depth; |
180 | 181 | int decode_array_with_array_mt; |
| 182 | + int decode_allow_comments; |
181 | 183 | int encode_skip_unsupported_value_types; |
182 | 184 | } json_config_t; |
183 | 185 |
|
@@ -383,6 +385,16 @@ static int json_cfg_decode_array_with_array_mt(lua_State *l) |
383 | 385 | return 1; |
384 | 386 | } |
385 | 387 |
|
| 388 | +/* Configures whether decoder allows comments */ |
| 389 | +static int json_cfg_decode_allow_comments(lua_State *l) |
| 390 | +{ |
| 391 | + json_config_t *cfg = json_arg_init(l, 1); |
| 392 | + |
| 393 | + json_enum_option(l, 1, &cfg->decode_allow_comments, NULL, 1); |
| 394 | + |
| 395 | + return 1; |
| 396 | +} |
| 397 | + |
386 | 398 | /* Configure how to treat invalid types */ |
387 | 399 | static int json_cfg_encode_skip_unsupported_value_types(lua_State *l) |
388 | 400 | { |
@@ -515,6 +527,7 @@ static void json_create_config(lua_State *l) |
515 | 527 | cfg->encode_number_precision = DEFAULT_ENCODE_NUMBER_PRECISION; |
516 | 528 | cfg->encode_empty_table_as_object = DEFAULT_ENCODE_EMPTY_TABLE_AS_OBJECT; |
517 | 529 | cfg->decode_array_with_array_mt = DEFAULT_DECODE_ARRAY_WITH_ARRAY_MT; |
| 530 | + cfg->decode_allow_comments = DEFAULT_DECODE_ALLOW_COMMENTS; |
518 | 531 | cfg->encode_escape_forward_slash = DEFAULT_ENCODE_ESCAPE_FORWARD_SLASH; |
519 | 532 | cfg->encode_skip_unsupported_value_types = DEFAULT_ENCODE_SKIP_UNSUPPORTED_VALUE_TYPES; |
520 | 533 | cfg->encode_indent = DEFAULT_ENCODE_INDENT; |
@@ -1279,13 +1292,46 @@ static void json_next_token(json_parse_t *json, json_token_t *token) |
1279 | 1292 | const json_token_type_t *ch2token = json->cfg->ch2token; |
1280 | 1293 | int ch; |
1281 | 1294 |
|
1282 | | - /* Eat whitespace. */ |
1283 | 1295 | while (1) { |
1284 | | - ch = (unsigned char)*(json->ptr); |
1285 | | - token->type = ch2token[ch]; |
1286 | | - if (token->type != T_WHITESPACE) |
| 1296 | + /* Eat whitespace. */ |
| 1297 | + while (1) { |
| 1298 | + ch = (unsigned char)*(json->ptr); |
| 1299 | + token->type = ch2token[ch]; |
| 1300 | + if (token->type != T_WHITESPACE) |
| 1301 | + break; |
| 1302 | + json->ptr++; |
| 1303 | + } |
| 1304 | + |
| 1305 | + if (!json->cfg->decode_allow_comments) |
1287 | 1306 | break; |
1288 | | - json->ptr++; |
| 1307 | + |
| 1308 | + /* Eat comments. */ |
| 1309 | + if ((unsigned char)json->ptr[0] != '/' || |
| 1310 | + ((unsigned char)json->ptr[1] != '/' && |
| 1311 | + (unsigned char)json->ptr[1] != '*')) { |
| 1312 | + break; |
| 1313 | + } |
| 1314 | + |
| 1315 | + if (json->ptr[1] == '/') { |
| 1316 | + /* Handle single-line comment */ |
| 1317 | + json->ptr += 2; |
| 1318 | + while (*json->ptr != '\0' && *json->ptr != '\n') |
| 1319 | + json->ptr++; |
| 1320 | + } else { |
| 1321 | + /* Handle multi-line comment */ |
| 1322 | + json->ptr += 2; |
| 1323 | + while (1) { |
| 1324 | + if (*json->ptr == '\0') { |
| 1325 | + json_set_token_error(token, json, "unclosed multi-line comment"); |
| 1326 | + return; |
| 1327 | + } |
| 1328 | + if (json->ptr[0] == '*' && json->ptr[1] == '/') { |
| 1329 | + json->ptr += 2; |
| 1330 | + break; |
| 1331 | + } |
| 1332 | + json->ptr++; |
| 1333 | + } |
| 1334 | + } |
1289 | 1335 | } |
1290 | 1336 |
|
1291 | 1337 | /* Store location of new token. Required when throwing errors |
@@ -1625,6 +1671,7 @@ static int lua_cjson_new(lua_State *l) |
1625 | 1671 | { "decode", json_decode }, |
1626 | 1672 | { "encode_empty_table_as_object", json_cfg_encode_empty_table_as_object }, |
1627 | 1673 | { "decode_array_with_array_mt", json_cfg_decode_array_with_array_mt }, |
| 1674 | + { "decode_allow_comments", json_cfg_decode_allow_comments }, |
1628 | 1675 | { "encode_sparse_array", json_cfg_encode_sparse_array }, |
1629 | 1676 | { "encode_max_depth", json_cfg_encode_max_depth }, |
1630 | 1677 | { "decode_max_depth", json_cfg_decode_max_depth }, |
|
0 commit comments