| 765 | } |
| 766 | |
| 767 | static void json_append_number(lua_State *l, json_config_t *cfg, |
| 768 | strbuf_t *json, int lindex) |
| 769 | { |
| 770 | double num = lua_tonumber(l, lindex); |
| 771 | int len; |
| 772 | |
| 773 | if (cfg->encode_invalid_numbers == 0) { |
| 774 | /* Prevent encoding invalid numbers */ |
| 775 | if (isinf(num) || isnan(num)) |
| 776 | json_encode_exception(l, cfg, json, lindex, |
| 777 | "must not be NaN or Infinity"); |
| 778 | } else if (cfg->encode_invalid_numbers == 1) { |
| 779 | /* Encode NaN/Infinity separately to ensure Javascript compatible |
| 780 | * values are used. */ |
| 781 | if (isnan(num)) { |
| 782 | strbuf_append_mem(json, "NaN", 3); |
| 783 | return; |
| 784 | } |
| 785 | if (isinf(num)) { |
| 786 | if (num < 0) |
| 787 | strbuf_append_mem(json, "-Infinity", 9); |
| 788 | else |
| 789 | strbuf_append_mem(json, "Infinity", 8); |
| 790 | return; |
| 791 | } |
| 792 | } else { |
| 793 | /* Encode invalid numbers as "null" */ |
| 794 | if (isinf(num) || isnan(num)) { |
| 795 | strbuf_append_mem(json, "null", 4); |
| 796 | return; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | strbuf_ensure_empty_length(json, FPCONV_G_FMT_BUFSIZE); |
| 801 | len = fpconv_g_fmt(strbuf_empty_ptr(json), num, cfg->encode_number_precision); |
| 802 | strbuf_extend_length(json, len); |
| 803 | } |
| 804 | |
| 805 | static void json_append_object(lua_State *l, json_config_t *cfg, |
| 806 | int current_depth, strbuf_t *json) |
no test coverage detected