Giter VIP home page Giter VIP logo

lua-json5's Introduction

Json5 parser for luajit

This crate provides json5 deserialization for luajit.

Inspired and adapted from json5-rs

NOTE: When compiling for macos, please add this to your $CARGO_HOME/config per this article (which also inspired this project):

[target.x86_64-apple-darwin]
rustflags = [
    "-C", "link-arg=-undefined",
    "-C", "link-arg=dynamic_lookup",
]

[target.aarch64-apple-darwin]
rustflags = [
    "-C", "link-arg=-undefined",
    "-C", "link-arg=dynamic_lookup",
]

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:

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:

use {
    'Joakker/lua-json5',
    run = './install.sh'
}

Performance

Tested on neovim using the following script:

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.

lua-json5's People

Contributors

joakker avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.