<commit_before>local tostring = tostring

module("json.encode.number")

function encode(number, options)
	local str = tostring(number)
	if str == "nan" then return "NaN" end
	if str == "inf" then return "Infinity" end
	if str == "-inf" then return "-Infinity" end
	return str
end<commit_msg>encoder: added applicable configuration options from the decoder to the number encoder<commit_after>local tostring = tostring
local assert = assert
local util = require("json.util")

module("json.encode.number")

local defaultOptions = {
	nan = true,
	inf = true,
-- N/A - cannot tell encoder not to use fractions, exponents, or hex-form
--	frac = true,
--	exp = true,
--	hex = false
}

default = nil -- Let the buildCapture optimization take place
strict = {
	nan = false,
	inf = false
}

function encode(number, options)
	-- Load up number-only options
	options = options and options.number
	options = options and util.merge({}, defaultOptions, options) or defaultOptions
	local str = tostring(number)
	if str == "nan" then
		assert(options.nan, "Invalid number: NaN not enabled")
		return "NaN"
	end
	if str == "inf" then
		assert(options.inf, "Invalid number: Infinity not enabled")
		return "Infinity"
	end
	if str == "-inf" then
		assert(options.inf, "Invalid number: -Infinity not enabled")
		return "-Infinity"
	end
	return str
end
{'fraction_non_alphanumeric': 0.11203007518796992, 'fraction_numerical': 0.0, 'mean_word_length': 4.158914728682171, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6481488', 'n_tokens_mistral': 417, 'n_tokens_neox': 404, 'n_words': 155}