lune
lune copied to clipboard
Add JSON comment support to Lune APIs
Lune's APIs for decoding JSON (net.jsonDecode
and serde.decode("json", ...)
) error when given a JSON string with comments.
This is a problem for me as I don't control the JSON files I'm reading, so I can't enforce which JSON schema is used.
Repro steps
Create two json files:
pass.json
{
"foo": true,
"bar": "str"
}
fail.json
// Comment at the top
{
"foo": true,
"bar": "str", // <-- trailing comma
}
Create a script with the following contents:
local fs = require("@lune/fs")
local net = require("@lune/net")
local serde = require("@lune/serde")
local pass = fs.readFile("pass.json")
local fail = fs.readFile("fail.json")
local success, result = pcall(net.jsonDecode, pass)
print(success, result)
success, result = pcall(net.jsonDecode, fail)
print(success, result)
success, result = pcall(serde.decode, "json", pass)
print(success, result)
success, result = pcall(serde.decode, "json", fail)
print(success, result)
Run the script. Then from the output you can see where both functions fail:
true {
bar = "str",
foo = true,
}
false expected value at line 1 column 1
[Stack Begin]
Script '[C]' - function pcall
Script 'server/server', Line 29
[Stack End]
true {
bar = "str",
foo = true,
}
false expected value at line 1 column 1
[Stack Begin]
Script '[C]' - function pcall
Script 'server/server', Line 33
[Stack End]
Considerations
If Lune doesn't want to support other JSON formats, would there be a good alternative? I'm considering spawning a separate process for JSON parsing to handle these edge cases, though I would much prefer to do everything from within Lune.