Skip to content

Pandoc.List type in Lua filter causes incorrect JSON encoding #9834

@jeffvalk

Description

@jeffvalk

Description

Using the (very handy!) pandoc.List type in a Lua filter causes the value to be serialized by pandoc.json as a JSON object rather than an array.

To demonstrate, if you create the following filter as test.lua

function Pandoc(doc)
  local t1 = {"one", "two", "three"} -- two identical tables, indexed by integer
  local t2 = {"one", "two", "three"}

  pandoc.List(t2) -- make the second a List

  print(pandoc.json.encode(t1)) -- this serializes as a JSON array
  print(pandoc.json.encode(t2)) -- but this does not
  os.exit()
end

and run it

echo | pandoc -L test.lua

it outputs the following:

["one","two","three"]
{"1":"one","2":"two","3":"three"}

These two lines should be the same.

Possible cause

The pandoc.List type adds an "__index" function to the metatable. Is this the culprit? (Perhaps using string indexes rather than numbers?)

Version info

Tested on pandoc 3.1.13

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions