CC: Tweaked

CC: Tweaked

42M Downloads

Wrong JSON serialization

Zelaux opened this issue · 3 comments

commented

Minecraft Version

1.19.2

Version

v1.102.2

Details

изображение
Code to reproduce

local value = 0
local pack1 = table.pack(value)
local pack2 = table.pack(pack1, nil)
print("serialise", textutils.serialise(pack2))
print("serialiseJson", textutils.serialiseJSON(pack2))

print("Click to exit")
os.pullEvent("mouse_click")
commented

Oh, this is a bit of a nasty one. As the docs state, this function attempts to guess whether a table is a JSON array or object — if all keys are numeric, then this is treated as an array, otherwise it's assumed to be an object.

In this case, because there's an n key, we assume it's an object. I think this is probably the right behaviour (otherwise you get all sorts of ambiguities with { n = 0 }), but definitely a little annoying.

I don't think we can really change the implementation of the function here without breaking existing users, so best thing is probably to clarify the docs to explain the rules here and mention the caveats.

commented

https://tweaked.cc/module/textutils.html#v:serializeJSON

  • Throws
    If the object contains a value which cannot be serialised. This includes functions and tables which appear multiple times.
commented

You can add an error, to indicate that's object cannot be fully serialized