https://github.com/Joakker/lua-json5
A json5 parser for luajit
https://github.com/Joakker/lua-json5
json5-parser lua neovim
Last synced: 4 months ago
JSON representation
A json5 parser for luajit
- Host: GitHub
- URL: https://github.com/Joakker/lua-json5
- Owner: Joakker
- License: mit
- Created: 2021-08-24T17:44:53.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2025-07-12T23:19:31.000Z (4 months ago)
- Last Synced: 2025-07-13T01:14:51.170Z (4 months ago)
- Topics: json5-parser, lua, neovim
- Language: Rust
- Homepage:
- Size: 30.3 KB
- Stars: 38
- Watchers: 2
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Json5 parser for luajit
This crate provides json5 deserialization for luajit.
Inspired and adapted from [json5-rs](https://github.com/callum-oakley/json5-rs)
Also, if you haven't already, add ';?.dylib' to your `package.cpath` so it will
be recognized by the interpreter.
## Usage
You can simply require the module in your scripts and parse a string using the
`parse` method:
```lua
local parse = require'json5'.parse
local data = [[
{
/* This is a comment */
ecma_identifier: 'works like a charm',
"string keys": [1,2,3], // trailing comma
}
]]
local parsed_data = parse(data)
```
## Use with neovim
You must have `cargo` installed and in your `$PATH`
Using [packer.nvim](https://github.com/wbthomason/packer.nvim):
```lua
use {
'Joakker/lua-json5',
-- if you're on windows
-- run = 'powershell ./install.ps1'
run = './install.sh'
}
```
Using [lazy.nvim](https://github.com/folke/lazy.nvim.git)
```lua
{
'Joakker/lua-json5',
build = './install.sh',
}
```
## Performance
Tested on neovim using the following script:
```lua
local data = [[ {"hello":"world"} ]]
local json5 = require('json5').parse
local json_decode = vim.fn.json_decode
local time_json5, time_json_decode = 0, 0
local aux
for _ = 1, 1000 do
aux = os.clock()
json5(data)
time_json5 = time_json5 + (os.clock() - aux)
end
for _ = 1, 1000 do
aux = os.clock()
json_decode(data)
time_json_decode = time_json_decode + (os.clock() - aux)
end
print(('json5: %.3fms'):format(time_json5))
print(('json_decode: %.3fms'):format(time_json_decode))
```
On average:
```
json5: 0.023ms
json_decode: 0.010ms
```
## So, why should I use this instead of the builtin `json_decode`?
If performance is your concern, I think you're better off using the builtin
function `json_decode`. The advantage this package has over regular json,
however, is that you get json5 features, such as comments, trailing commas and
more flexible string literals.