| 586 | } |
| 587 | |
| 588 | static void json_append_number(lua_State *l, json_config_t *cfg, |
| 589 | strbuf_t *json, int lindex) |
| 590 | { |
| 591 | double num = lua_tonumber(l, lindex); |
| 592 | int len; |
| 593 | |
| 594 | if (cfg->encode_invalid_numbers == 0) { |
| 595 | /* Prevent encoding invalid numbers */ |
| 596 | if (isinf(num) || isnan(num)) |
| 597 | json_encode_exception(l, cfg, json, lindex, "must not be NaN or Inf"); |
| 598 | } else if (cfg->encode_invalid_numbers == 1) { |
| 599 | /* Encode invalid numbers, but handle "nan" separately |
| 600 | * since some platforms may encode as "-nan". */ |
| 601 | if (isnan(num)) { |
| 602 | strbuf_append_mem(json, "nan", 3); |
| 603 | return; |
| 604 | } |
| 605 | } else { |
| 606 | /* Encode invalid numbers as "null" */ |
| 607 | if (isinf(num) || isnan(num)) { |
| 608 | strbuf_append_mem(json, "null", 4); |
| 609 | return; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | strbuf_ensure_empty_length(json, FPCONV_G_FMT_BUFSIZE); |
| 614 | len = fpconv_g_fmt(strbuf_empty_ptr(json), num, cfg->encode_number_precision); |
| 615 | strbuf_extend_length(json, len); |
| 616 | } |
| 617 | |
| 618 | static void json_append_object(lua_State *l, json_config_t *cfg, |
| 619 | int current_depth, strbuf_t *json) |
no test coverage detected