diff options
author | ShadowNinja <shadowninja@minetest.net> | 2013-12-18 18:17:26 -0500 |
---|---|---|
committer | ShadowNinja <shadowninja@minetest.net> | 2013-12-18 18:18:43 -0500 |
commit | e1f9ba435f8c0cca90f2d3df36e9aa82763e1da0 (patch) | |
tree | e4d84dc22aff5c8048083cbfab1d7c97da958728 | |
parent | ba8fa0bd42b5a4a0e3658a5e784ee6a885dcc891 (diff) | |
download | minetest-e1f9ba435f8c0cca90f2d3df36e9aa82763e1da0.tar.gz minetest-e1f9ba435f8c0cca90f2d3df36e9aa82763e1da0.tar.bz2 minetest-e1f9ba435f8c0cca90f2d3df36e9aa82763e1da0.zip |
Don't throw a error when writing JSON fails
-rw-r--r-- | doc/lua_api.txt | 2 | ||||
-rw-r--r-- | src/script/common/c_content.cpp | 12 | ||||
-rw-r--r-- | src/script/lua_api/l_util.cpp | 10 |
3 files changed, 15 insertions, 9 deletions
diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 135e2cfcd..eb6362b0b 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -1531,7 +1531,7 @@ minetest.parse_json(string[, nullvalue]) -> something ^ On success returns a table, a string, a number, a boolean or nullvalue ^ On failure outputs an error message and returns nil ^ Example: parse_json("[10, {\"a\":false}]") -> {10, {a = false}} -minetest.write_json(data[, styled]) -> string +minetest.write_json(data[, styled]) -> string or nil and error message ^ Convert a Lua table into a JSON string ^ styled: Outputs in a human-readable format if this is set, defaults to false ^ Un-serializable things like functions and userdata are saved as null. diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 8eb57ba41..cb5a92ae4 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1106,26 +1106,26 @@ void get_json_value(lua_State *L, Json::Value &root, int index) if (keytype == LUA_TNUMBER) { lua_Number key = lua_tonumber(L, -1); if (roottype != Json::nullValue && roottype != Json::arrayValue) { - throw LuaError(NULL, "Can't mix array and object values in JSON"); + throw SerializationError("Can't mix array and object values in JSON"); } else if (key < 1) { - throw LuaError(NULL, "Can't use zero-based or negative indexes in JSON"); + throw SerializationError("Can't use zero-based or negative indexes in JSON"); } else if (floor(key) != key) { - throw LuaError(NULL, "Can't use indexes with a fractional part in JSON"); + throw SerializationError("Can't use indexes with a fractional part in JSON"); } root[(Json::ArrayIndex) key - 1] = value; } else if (keytype == LUA_TSTRING) { if (roottype != Json::nullValue && roottype != Json::objectValue) { - throw LuaError(NULL, "Can't mix array and object values in JSON"); + throw SerializationError("Can't mix array and object values in JSON"); } root[lua_tostring(L, -1)] = value; } else { - throw LuaError(NULL, "Lua key to convert to JSON is not a string or number"); + throw SerializationError("Lua key to convert to JSON is not a string or number"); } } } else if (type == LUA_TNIL) { root = Json::nullValue; } else { - throw LuaError(NULL, "Can only store booleans, numbers, strings, objects, arrays, and null in JSON"); + throw SerializationError("Can only store booleans, numbers, strings, objects, arrays, and null in JSON"); } lua_pop(L, 1); // Pop value } diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index 9fa6fcb77..f9ec94db4 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -179,7 +179,7 @@ int ModApiUtil::l_parse_json(lua_State *L) return 1; } -// write_json(data[, styled]) -> string +// write_json(data[, styled]) -> string or nil and error message int ModApiUtil::l_write_json(lua_State *L) { NO_MAP_LOCK_REQUIRED; @@ -191,7 +191,13 @@ int ModApiUtil::l_write_json(lua_State *L) } Json::Value root; - get_json_value(L, root, 1); + try { + get_json_value(L, root, 1); + } catch (SerializationError &e) { + lua_pushnil(L); + lua_pushstring(L, e.what()); + return 2; + } std::string out; if (styled) { |