Fix JSON encoding: force empty tables to encode as arrays

Lua's json.encode produces {} for empty tables instead of [].
Use json.util.InitArray to mark tables as arrays so they encode
correctly as [] for authors, bookmarks, highlights, notes, and books.
This commit is contained in:
2026-05-30 10:24:56 -04:00
parent ac84585e50
commit 618793a075
2 changed files with 8 additions and 4 deletions
+3 -1
View File
@@ -1,4 +1,5 @@
local json = require("json") local json = require("json")
local json_util = require("json/util")
local logger = require("logger") local logger = require("logger")
local ltn12 = require("ltn12") local ltn12 = require("ltn12")
local socketutil = require("socketutil") local socketutil = require("socketutil")
@@ -118,8 +119,9 @@ function BookhoardAPI:checkRegistrationStatus(registration_id)
end end
function BookhoardAPI:syncProgress(book_data, sync_mode) function BookhoardAPI:syncProgress(book_data, sync_mode)
local books = json_util.InitArray({ book_data })
return self:_request("POST", "/api/sync/koreader/progress", { return self:_request("POST", "/api/sync/koreader/progress", {
books = { book_data }, books = books,
sync_mode = sync_mode or "immediate", sync_mode = sync_mode or "immediate",
}, PROGRESS_TIMEOUTS) }, PROGRESS_TIMEOUTS)
end end
+5 -3
View File
@@ -11,6 +11,7 @@ local SpinWidget = require("ui/widget/spinwidget")
local UIManager = require("ui/uimanager") local UIManager = require("ui/uimanager")
local WidgetContainer = require("ui/widget/container/widgetcontainer") local WidgetContainer = require("ui/widget/container/widgetcontainer")
local json = require("json") local json = require("json")
local json_util = require("json/util")
local logger = require("logger") local logger = require("logger")
local sha2 = require("ffi/sha2") local sha2 = require("ffi/sha2")
local time = require("ui/time") local time = require("ui/time")
@@ -670,6 +671,7 @@ function Bookhoard:collectBookData()
if props.authors then if props.authors then
authors = { props.authors } authors = { props.authors }
end end
json_util.InitArray(authors)
local file_sha256 = self:getFileSHA256() local file_sha256 = self:getFileSHA256()
local book_uuid = self:getBookhoardUUID() local book_uuid = self:getBookhoardUUID()
@@ -699,9 +701,9 @@ function Bookhoard:collectBookData()
end end
function Bookhoard:collectAnnotations() function Bookhoard:collectAnnotations()
local bookmarks = {} local bookmarks = json_util.InitArray({})
local highlights = {} local highlights = json_util.InitArray({})
local notes = {} local notes = json_util.InitArray({})
if not self.ui.bookmark then if not self.ui.bookmark then
return bookmarks, highlights, notes return bookmarks, highlights, notes