1
0
Fork 0
A lightweight JSON library for Lua
 
 
Go to file
rxi f049daf06c
Merge pull request #15 from nikeinikei/master
Fix serializing of tables with overloaded indexing / using __index metamethod
2019-04-09 20:10:27 +01:00
bench Fixed indentation in benchmark util script 2015-08-14 20:00:48 +01:00
test Added checking and tests for trailing garbage when decoding 2018-03-10 14:28:50 +00:00
LICENSE Updated copyright year (2015 -> 2018), moved full license to json.lua 2018-03-10 14:16:05 +00:00
README.md Fixed mistake in README 2015-08-30 21:37:10 +01:00
json.lua fix array detection when using overloaded indexing 2019-01-11 19:22:04 +01:00

README.md

json.lua

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)
  • Tiny: around 290sloc, 9kb
  • Proper error messages, eg: expected '}' or ',' at line 203 col 30

Usage

The json.lua file should be dropped into an existing project and required by it:

json = require "json"

The library provides the following functions:

json.encode(value)

Returns a string representing value encoded in JSON.

json.encode({ 1, 2, 3, { x = 10 } }) -- Returns '[1,2,3,{"x":10}]'

json.decode(str)

Returns a value representing the decoded JSON string.

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 contained within an array or object are converted to nil and are therefore lost upon decoding
  • 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 for details.