Move json dependency to fennel-ls/json, to represent that it's a modified version

This commit is contained in:
XeroOl 2024-02-19 21:31:50 -06:00
parent 130f96a4ca
commit 6152bed8a6
14 changed files with 6 additions and 528 deletions

View File

@ -81,7 +81,9 @@ You can't load external lint rules with fennel-ls, but I would love to receive p
fennel-ls is licensed under the MIT license. See LICENSE for more info.
This project also contains files from other projects:
* test/pl/* comes from [Penlight](https://github.com/lunarmodules/Penlight) [MIT license]
* src/json/* is modified, but is originally from [json.lua](https://github.com/rxi/json.lua) [MIT license]
* (LICENSE)[test/pl/LICENSE.md]
* src/fennel-ls/json/* is modified, but is originally from [json.lua](https://github.com/rxi/json.lua) [MIT license]
* (LICENSE)[src/fennel-ls/json/LICENSE]
* src/fennel-ls/docs/* contains information from the [lua](https://lua.org) reference [MIT license]
* test/lust.lua is modified, but originially comes from from [lust](https://github.com/bjornbytes/lust) [MIT license]
* fennel and src/fennel.lua are compiled from [fennel](https://git.sr.ht/~technomancy/fennel) [MIT license]

View File

@ -8,7 +8,7 @@ There are only two functions exposed here:
It's probably not compliant yet, because serialization of [] and {} is the same.
Luckily, I'm testing with Neovim, so I can pretend these problems don't exist for now."
(local {: encode : decode} (require :json.json))
(local {: encode : decode} (require :fennel-ls.json.json))
(λ read-header [in ?header]
"Reads the header of a JSON-RPC message"

View File

@ -7,7 +7,7 @@ LSP json objects."
(local fennel (require :fennel))
(local utils (require :fennel-ls.utils))
(local json (require :json.json))
(local json (require :fennel-ls.json.json))
(λ nullify [?value]
(case ?value

View File

@ -1,45 +0,0 @@
# ![json.lua](https://cloud.githubusercontent.com/assets/3920290/9281532/99e5e0cc-42bd-11e5-8fce-eaff2f7fc681.png)
A lightweight JSON library for Lua
## Features
* Implemented in pure Lua: works with 5.1, 5.2, 5.3 and JIT
* Fast: generally outperforms other pure Lua JSON implementations
([benchmark scripts](bench/))
* Tiny: around 280sloc, 9kb
* Proper error messages, *eg:* `expected '}' or ',' at line 203 col 30`
## Usage
The [json.lua](json.lua?raw=1) file should be dropped into an existing project
and required by it:
```lua
json = require "json"
```
The library provides the following functions:
#### json.encode(value)
Returns a string representing `value` encoded in JSON.
```lua
json.encode({ 1, 2, 3, { x = 10 } }) -- Returns '[1,2,3,{"x":10}]'
```
#### json.decode(str)
Returns a value representing the decoded JSON string.
```lua
json.decode('[1,2,3,{"x":10}]') -- Returns { 1, 2, 3, { x = 10 } }
```
## Notes
* Trying to encode values which are unrepresentable in JSON will never result
in type conversion or other magic: sparse arrays, tables with mixed key types
or invalid numbers (NaN, -inf, inf) will raise an error
* `null` values are converted to the special value `json.null`
* *Pretty* encoding is not supported, `json.encode()` only encodes to a compact
format
## License
This library is free software; you can redistribute it and/or modify it under
the terms of the MIT license. See [LICENSE](LICENSE) for details.

View File

@ -1,6 +0,0 @@
print("[decode]")
loadfile("bench_decode.lua")()
print()
print("[encode]")
loadfile("bench_encode.lua")()

View File

@ -1,75 +0,0 @@
local bench = require "util.bench"
local libs = {
"../json.lua", -- https://github.com/rxi/json.lua
"dkjson.lua", -- https://github.com/LuaDist/dkjson
"jfjson.lua", -- http://regex.info/blog/lua/json
--"json4lua.lua", -- https://github.com/craigmj/json4lua
}
-- JSON string: wikipedia example stored 1000 times in an array
local text = "[" .. string.rep([[{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [],
"spouse": null
}, ]], 1000):sub(1, -3) .. "]"
-- As this is meant to be a pure Lua benchmark, we remove the ability to
-- require 'lpeg' so dkjson doesn't use it for parsing. (Incidentally json.lua
-- seems to outperform libraries which use lpeg when both are using LuaJIT)
local _require = require
require = function(modname)
if modname == "lpeg" then error() end
return _require(modname)
end
-- Run benchmarks, store results
local results = {}
for i, name in ipairs(libs) do
local f = loadfile(name)
if not f then
error( "failed to load '" .. name .. "'; run './get_json_libs.sh'" )
end
local json = f()
-- Remap functions to work for jfjson.lua
if name == "jfjson.lua" then
local _encode, _decode = json.encode, json.decode
json.encode = function(...) return _encode(json, ...) end
json.decode = function(...) return _decode(json, ...) end
end
-- Warmup (for LuaJIT)
bench.run(name, 1, function() json.decode(text) end)
-- Run and push results
local res = bench.run(name, 10, function() json.decode(text) end)
table.insert(results, res)
end
bench.print_system_info()
bench.print_results(results)

View File

@ -1,63 +0,0 @@
local bench = require "util.bench"
local libs = {
"../json.lua", -- https://github.com/rxi/json.lua
"dkjson.lua", -- https://github.com/LuaDist/dkjson
"jfjson.lua", -- http://regex.info/blog/lua/json
"json4lua.lua", -- https://github.com/craigmj/json4lua
}
-- Build table which will be encoded: wikipedia example stored 1000 times
local data = {}
for i = 1, 1000 do
table.insert(data, {
firstName = "John",
lastName = "Smith",
isAlive = true,
age = 25,
address = {
streetAddress = "21 2nd Street",
city = "New York",
state = "NY",
postalCode = "10021-3100"
},
phoneNumbers = {
{ type = "home", number = "212 555-1234" },
{ type = "office", number = "646 555-4567" }
},
children = {},
spouse = nil
})
end
-- Run benchmarks
local results = {}
for i, name in ipairs(libs) do
local f = loadfile(name)
if not f then
error( "failed to load '" .. name .. "'; run './get_json_libs.sh'" )
end
local json = f()
-- Handle special cases
if name == "jfjson.lua" then
local _encode, _decode = json.encode, json.decode
json.encode = function(...) return _encode(json, ...) end
json.decode = function(...) return _decode(json, ...) end
end
-- Warmup (for LuaJIT)
bench.run(name, 1, function() json.encode(data) end)
-- Run and push results
local res = bench.run(name, 10, function() json.encode(data) end)
table.insert(results, res)
end
bench.print_system_info()
bench.print_results(results)

View File

@ -1,15 +0,0 @@
#!/bin/bash
# Downloads other JSON libraries for use in the benchmark scripts
# Remove libraries
rm dkjson.lua 2>/dev/null
rm jfjson.lua 2>/dev/null
rm json4lua.lua 2>/dev/null
# Get libraries
echo "Downloading json libs..."
curl -sS -o dkjson.lua "https://raw.githubusercontent.com/LuaDist/dkjson/master/dkjson.lua"
curl -sS -o json4lua.lua "https://raw.githubusercontent.com/craigmj/json4lua/master/json/json.lua"
curl -sS -o jfjson.lua "http://regex.info/code/JSON.lua"
echo "Done"

View File

@ -1,62 +0,0 @@
local bench = {}
local unpack = unpack or table.unpack
local fmt = string.format
function bench.run(name, count, func)
-- Run bench
local res = {}
for i = 1, count do
local start_time = os.clock()
func()
table.insert(res, (os.clock() - start_time))
end
-- Calculate average
local avg = 0
for i, v in ipairs(res) do
avg = avg + v
end
avg = avg / #res
-- Build and return result table
return {
name = name,
avg = avg,
min = math.min(unpack(res)),
max = math.max(unpack(res)),
all = res,
}
end
function bench.get_cpu_name()
local fp = io.open("/proc/cpuinfo", "rb")
if not fp then
return "unknown"
end
local text = fp:read("*a")
return text:match("model name%s*:%s*(.-)\n")
end
function bench.print_system_info()
print( fmt("Lua version : %s", jit and jit.version or _VERSION) )
print( fmt("CPU name : %s", bench.get_cpu_name()) )
end
function bench.print_results(results)
-- Find best average
local best = math.huge
for i, v in ipairs(results) do
best = math.min(best, v.avg)
end
-- Print results
for i, v in ipairs(results) do
print( fmt("%-13s : %.03gs [x%1.3g] (min: %.03gs, max %.03gs)",
v.name, v.avg, v.avg / best, v.min, v.max) )
end
end
return bench

View File

@ -1,258 +0,0 @@
local json = loadfile("../json.lua")()
local fmt = string.format
local function test(name, func)
xpcall(function()
func()
print( fmt("[pass] %s", name) )
end, function(err)
print( fmt("[fail] %s : %s", name, err) )
end)
end
local function equal(a, b)
-- Handle table
if type(a) == "table" and type(b) == "table" then
for k in pairs(a) do
if not equal(a[k], b[k]) then
return false
end
end
for k in pairs(b) do
if not equal(b[k], a[k]) then
return false
end
end
return true
end
-- Handle scalar
return a == b
end
test("numbers", function()
local t = {
[ "123.456" ] = 123.456,
[ "-123" ] = -123,
[ "-567.765" ] = -567.765,
[ "12.3" ] = 12.3,
[ "0" ] = 0,
[ "0.10000000012" ] = 0.10000000012,
}
for k, v in pairs(t) do
local res = json.decode(k)
assert( res == v, fmt("expected '%s', got '%s'", k, res) )
local res = json.encode(v)
assert( res == k, fmt("expected '%s', got '%s'", v, res) )
end
assert( json.decode("13e2") == 13e2 )
assert( json.decode("13E+2") == 13e2 )
assert( json.decode("13e-2") == 13e-2 )
end)
test("literals", function()
assert( json.decode("true") == true )
assert( json.encode(true) == "true" )
assert( json.decode("false") == false )
assert( json.encode(false) == "false" )
assert( json.decode("null") == nil )
assert( json.encode(nil) == "null")
end)
test("strings", function()
local s = ""
assert( s == json.decode( json.encode(s) ) )
local s = "\\"
assert( s == json.decode( json.encode(s) ) )
local s = "Hello world"
assert( s == json.decode( json.encode(s) ) )
local s = "\0 \13 \27"
assert( s == json.decode( json.encode(s) ) )
local s = "\0\r\n\8"
assert( s == json.decode( json.encode(s) ) )
end)
test("unicode", function()
local s = "こんにちは世界"
assert( s == json.decode( json.encode(s) ) )
end)
test("arrays", function()
local t = { "cat", "dog", "owl" }
assert( equal( t, json.decode( json.encode(t) ) ) )
end)
test("objects", function()
local t = { x = 10, y = 20, z = 30 }
assert( equal( t, json.decode( json.encode(t) ) ) )
end)
--test("strict decode", function()
-- local t = {
-- '{x : 1}',
-- '{x : hello}',
-- "{'x' : 1}",
-- '{"x" : nil}',
-- '{"x" : 0x10}',
-- '{"x" : 001}',
-- '{"x" : .1}',
-- '{"x" : 1, }',
-- '[1, 2, 3, ]',
-- }
-- for i, v in ipairs(t) do
-- local status = pcall(json.decode, v)
-- assert( not status, fmt("'%s' was parsed without error", v) )
-- end
--end)
test("decode invalid", function()
local t = {
'',
' ',
'{',
'[',
'{"x" : ',
'{"x" : 1',
'{"x" : z }',
'{"x" : 123z }',
'{x : 123 }',
'{10 : 123 }',
'{]',
'[}',
'"a',
'10 xx',
'{}123'
}
for i, v in ipairs(t) do
local status = pcall(json.decode, v)
assert( not status, fmt("'%s' was parsed without error", v) )
end
end)
test("decode invalid string", function()
local t = {
[["\z"]],
[["\1"]],
[["\u000z"]],
[["\ud83d\ude0q"]],
'"x\ny"',
'"x\0y"',
}
for i, v in ipairs(t) do
local status, err = pcall(json.decode, v)
assert( not status, fmt("'%s' was parsed without error", v) )
end
end)
test("decode escape", function()
local t = {
[ [["\u263a"]] ] = '',
[ [["\ud83d\ude02"]] ] = '😂',
[ [["\r\n\t\\\""]] ] = '\r\n\t\\"',
[ [["\\"]] ] = '\\',
[ [["\\\\"]] ] = '\\\\',
[ [["\/"]] ] = '/',
[ [["\\u \u263a"]] ] = [[\u ☺]],
}
for k, v in pairs(t) do
local res = json.decode(k)
assert( res == v, fmt("expected '%s', got '%s'", v, res) )
end
end)
test("decode empty", function()
local t = {
[ '[]' ] = {},
[ '{}' ] = {},
[ '""' ] = "",
}
for k, v in pairs(t) do
local res = json.decode(k)
assert( equal(res, v), fmt("'%s' did not equal expected", k) )
end
end)
test("decode collection", function()
local t = {
[ '[1, 2, 3, 4, 5, 6]' ] = {1, 2, 3, 4, 5, 6},
[ '[1, 2, 3, "hello"]' ] = {1, 2, 3, "hello"},
[ '{ "name": "test", "id": 231 }' ] = {name = "test", id = 231},
[ '{"x":1,"y":2,"z":[1,2,3]}' ] = {x = 1, y = 2, z = {1, 2, 3}},
}
for k, v in pairs(t) do
local res = json.decode(k)
assert( equal(res, v), fmt("'%s' did not equal expected", k) )
end
end)
test("encode invalid", function()
local t = {
{ [1000] = "b" },
{ [ function() end ] = 12 },
{ nil, 2, 3, 4 },
{ x = 10, [1] = 2 },
{ [1] = "a", [3] = "b" },
{ x = 10, [4] = 5 },
}
for i, v in ipairs(t) do
local status, res = pcall(json.encode, v)
assert( not status, fmt("encoding idx %d did not result in an error", i) )
end
end)
test("encode invalid number", function()
local t = {
math.huge, -- inf
-math.huge, -- -inf
math.huge * 0, -- NaN
}
for i, v in ipairs(t) do
local status, res = pcall(json.encode, v)
assert( not status, fmt("encoding '%s' did not result in an error", v) )
end
end)
test("encode escape", function()
local t = {
[ '"x"' ] = [["\"x\""]],
[ 'x\ny' ] = [["x\ny"]],
[ 'x\0y' ] = [["x\u0000y"]],
[ 'x\27y' ] = [["x\u001by"]],
[ '\r\n\t\\"' ] = [["\r\n\t\\\""]],
}
for k, v in pairs(t) do
local res = json.encode(k)
assert( res == v, fmt("'%s' was not escaped properly", k) )
end
end)
test("encode null", function()
local t = {
a = "foo",
b = json.null,
c = 42,
}
local res = json.decode( json.encode(t) )
for k, v in pairs(t) do
assert( equal( v, res[k] ) )
end
assert( equal( json.null, res.b ) )
end)

View File

@ -1,6 +1,6 @@
(import-macros {: is-matching : describe : it : before-each} :test)
(local is (require :test.is))
(local {: null} (require :json.json))
(local {: null} (require :fennel-ls.json.json))
(local {: view} (require :fennel))