Skip to content

Commit

Permalink
Merge pull request #508 from neumond/mc-1.15.x
Browse files Browse the repository at this point in the history
Fix JSON serialization of strings
  • Loading branch information
SquidDev committed Jul 25, 2020
2 parents ac7979f + 053cb1b commit cd879b0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/main/resources/data/computercraft/lua/rom/apis/textutils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,31 @@ empty_json_array = mk_tbl("[]", "empty_json_array")
-- @see textutils.unserialiseJSON
json_null = mk_tbl("null", "json_null")

local serializeJSONString
do
local function hexify(c)
return ("\\u00%02X"):format(c:byte())
end

local map = {
["\""] = "\\\"",
["\\"] = "\\\\",
["\b"] = "\\b",
["\f"] = "\\f",
["\n"] = "\\n",
["\r"] = "\\r",
["\t"] = "\\t",
}
for i = 0, 0x1f do
local c = string.char(i)
if map[c] == nil then map[c] = hexify(c) end
end

serializeJSONString = function(s)
return ('"%s"'):format(s:gsub("[\0-\x1f\"\\]", map):gsub("[\x7f-\xff]", hexify))
end
end

local function serializeJSONImpl(t, tTracking, bNBTStyle)
local sType = type(t)
if t == empty_json_array then return "[]"
Expand All @@ -361,7 +386,7 @@ local function serializeJSONImpl(t, tTracking, bNBTStyle)
if bNBTStyle then
sEntry = tostring(k) .. ":" .. serializeJSONImpl(v, tTracking, bNBTStyle)
else
sEntry = string.format("%q", k) .. ":" .. serializeJSONImpl(v, tTracking, bNBTStyle)
sEntry = serializeJSONString(k) .. ":" .. serializeJSONImpl(v, tTracking, bNBTStyle)
end
if nObjectSize == 0 then
sObjectResult = sObjectResult .. sEntry
Expand Down Expand Up @@ -390,7 +415,7 @@ local function serializeJSONImpl(t, tTracking, bNBTStyle)
end

elseif sType == "string" then
return string.format("%q", t)
return serializeJSONString(t)

elseif sType == "number" or sType == "boolean" then
return tostring(t)
Expand Down
14 changes: 14 additions & 0 deletions src/test/resources/test-rom/spec/apis/textutils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ describe("The textutils library", function()
it("serializes null", function()
expect(textutils.serializeJSON(textutils.json_null)):eq("null")
end)

it("serializes strings", function()
expect(textutils.serializeJSON('a')):eq('"a"')
expect(textutils.serializeJSON('"')):eq('"\\""')
expect(textutils.serializeJSON('\\')):eq('"\\\\"')
expect(textutils.serializeJSON('/')):eq('"/"')
expect(textutils.serializeJSON('\b')):eq('"\\b"')
expect(textutils.serializeJSON('\n')):eq('"\\n"')
expect(textutils.serializeJSON(string.char(0))):eq('"\\u0000"')
expect(textutils.serializeJSON(string.char(0x0A))):eq('"\\n"')
expect(textutils.serializeJSON(string.char(0x1D))):eq('"\\u001D"')
expect(textutils.serializeJSON(string.char(0x81))):eq('"\\u0081"')
expect(textutils.serializeJSON(string.char(0xFF))):eq('"\\u00FF"')
end)
end)

describe("textutils.unserializeJSON", function()
Expand Down

0 comments on commit cd879b0

Please sign in to comment.