Skip to content

Commit

Permalink
Merge pull request #507 from evo-lua/499-json-encode-cdata
Browse files Browse the repository at this point in the history
Add support for encoding cdata values (and other objects with a __tostring metamethod) to json.encode
  • Loading branch information
rdw-software authored Feb 19, 2024
2 parents c6ff7ea + 3ae2c2e commit c5cf4cf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
35 changes: 22 additions & 13 deletions Runtime/Bindings/rapidjson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,23 @@ class CustomizedEncoder {
}
[[fallthrough]];
default:
luaL_error(L, "unsupported value type : %s", lua_typename(L, t));
if(!luaL_callmeta(L, idx, "__tostring")) { // Not a serializable object
lua_pushvalue(L, idx);
lua_pushstring(L, "tostring");
lua_gettable(L, LUA_GLOBALSINDEX);
lua_pushvalue(L, -2);
lua_call(L, 1, 1);
const char* stringifiedValue = lua_tostring(L, -1);
if(stringifiedValue == nullptr) stringifiedValue = "nil";
lua_pop(L, 2);

lua_pushfstring(L, "Cannot encode value %s (only JSON-compatible primitive types are supported)", stringifiedValue);
lua_error(L);
}

// While this isn't reversible, assume __tostring was set on purpose (serializable object)
encodeString(L, writer, -1);
lua_pop(L, 1);
}
}

Expand Down Expand Up @@ -247,18 +263,11 @@ int luaopen_rapidjson(lua_State* L); // Declared here because there's no header
}

static int json_encode_custom(lua_State* L) {
try {
CustomizedEncoder encode(L, 2);
StringBuffer s;
encode.encode(L, &s, 1);
lua_pushlstring(L, s.GetString(), s.GetSize());
return 1;
} catch(const std::exception& e) {
luaL_error(L, "error while encoding: %s", e.what());
} catch(...) {
luaL_error(L, "unknown error while encoding");
}
return 0;
CustomizedEncoder encode(L, 2);
StringBuffer s;
encode.encode(L, &s, 1);
lua_pushlstring(L, s.GetString(), s.GetSize());
return 1;
}

int json_get_version_string(lua_State* L) {
Expand Down
18 changes: 17 additions & 1 deletion Tests/BDD/json-library.spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
local ffi = require("ffi")
local json = require("json")

describe("json", function()
local json = require("json")
local exportedFunctions = {
"array",
"decode",
Expand Down Expand Up @@ -62,6 +64,20 @@ describe("json", function()
it("should be an alias of json.encode", function()
assertEquals(json.stringify, json.encode)
end)

it("should propagate encoding errors to the Lua environment", function()
assertThrows(function()
json.stringify(print)
end, format(
"Cannot encode value %s (only JSON-compatible primitive types are supported)",
tostring(print)
))
end)

it("should be able to stringify cdata values", function()
local cdata = ffi.new("uint32_t", 42)
assertEquals(json.stringify({ cdata = cdata }), '{"cdata":"' .. tostring(cdata) .. '"}')
end)
end)

describe("pretty", function()
Expand Down

0 comments on commit c5cf4cf

Please sign in to comment.