Json Handler Script

Type : Module
Recommended place for it : Replicated Storage
What for / Name : SafeJson

Script content :

-- Json handler

local SafeJson = {}

function SafeJson.encode(value)
	local t = typeof(value)
	if t == "string" then
		return SafeJson._str(value)
	elseif t == "number" then
		if value ~= value or math.abs(value) == math.huge then return "null" end
		return tostring(value)
	elseif t == "boolean" then
		return value and "true" or "false"
	elseif t == "nil" then
		return "null"
	elseif t == "table" then
		local n = 0
		for _ in pairs(value) do n = n + 1 end
		if n == #value and n > 0 then
			local p = {}
			for i = 1, #value do p[i] = SafeJson.encode(value[i]) end
			return "[" .. table.concat(p, ",") .. "]"
		else
			local p = {}
			for k, v in pairs(value) do
				if typeof(k) == "string" then
					table.insert(p, SafeJson._str(k) .. ":" .. SafeJson.encode(v))
				end
			end
			return "{" .. table.concat(p, ",") .. "}"
		end
	end
	return "null"
end

function SafeJson._str(s)
	local p = {}
	local i = 1
	while i <= #s do
		local b = string.byte(s, i)
		if b == 34 then table.insert(p, '\\"')
		elseif b == 92 then table.insert(p, "\\\\")
		elseif b == 8 then table.insert(p, "\\b")
		elseif b == 12 then table.insert(p, "\\f")
		elseif b == 10 then table.insert(p, "\\n")
		elseif b == 13 then table.insert(p, "\\r")
		elseif b == 9 then table.insert(p, "\\t")
		elseif b < 32 then table.insert(p, string.format("\\u%04x", b))
		else table.insert(p, string.char(b))
		end
		i = i + 1
	end
	return '"' .. table.concat(p) .. '"'
end

function SafeJson.decode(str)
	return game:GetService("HttpService"):JSONDecode(str)
end

print("[SafeJson] Loaded json support table .")

return SafeJson

Enjoy , Hope it is useful ! :grinning_face:

for some reason some stuff cut out when I was pasting the script lol imma fix it in a sec ( it is fine now )

what problem does this module solve that HttpService:JSONEncode() doesnt already solve?

3 Likes

So if u have smth like lol it might cut to lo instead of lol or like ;) would also bug out and say like <font…> the main goal for the script is when you are using smth that handles text for it to not bug out characters . I use it when I am making like global or server announce commands on cmdr , so if I want to say “Aleks : Hi guys :)” it would say that or sometimes without it it might look like “Aleks : Hi guys <font…304793smth>”

but how does this module fix that though+? from what I can see it just handles JSON escaping it doesnt do anything with RichText or prevent characters from being cut off from what I could see.

wouldnt HttpService:JSONEncode() already handle the same thing?

3 Likes

The module doesn’t fix RichText rendering or it being cut off. It only serializes values into JSON: JSON escaping protects things like quotes, backslashes, and control characters so JSON remains valid. It doesn’t escape RichText tags such as or , and it can’t address a separate text-lenght or truncation limit .

1 Like

but wouldnt HttpService:JSONEncode() already handle all of that ? Im still not really seeing what benefit this module has over just using the builtin methods in roblox,.

4 Likes

not always most of the time when I didn’t have that script it would end up looking like “Hi guys <font…3949fjojd>” instead of “Hi guys ;)”