diff --git a/BookhoardAPI.lua b/BookhoardAPI.lua index 8fc05d7..f818acf 100644 --- a/BookhoardAPI.lua +++ b/BookhoardAPI.lua @@ -1,5 +1,18 @@ local json = require("json") local json_util = require("json.util") + +-- Recursively replace LuaJSON's truthy null-sentinel functions with nil +-- (JSON null), so server payloads behave like normal Lua tables. +local function strip_json_nulls(t) + for k, v in pairs(t) do + if type(v) == "function" then + t[k] = nil + elseif type(v) == "table" then + strip_json_nulls(v) + end + end + return t +end local logger = require("logger") local ltn12 = require("ltn12") local socketutil = require("socketutil") @@ -86,6 +99,11 @@ function BookhoardAPI:_request(method, path, body, timeouts) if response_body and #response_body > 0 then local decode_ok, data = pcall(json.decode, response_body) if decode_ok and type(data) == "table" then + -- LuaJSON decodes JSON null into a truthy sentinel FUNCTION, + -- which defeats every `if not x.field` nil-check downstream + -- (crash: "attempt to index local 'progress' (a function + -- value)"). Normalize sentinels to nil at the boundary. + strip_json_nulls(data) return true, data end return true, response_body