A tinier, lightweight JSON library for Lua
This is a fork of json.lua. No changes were made to this documentation, so please take the benchmarks with a grain of salt. The purpose of this is to split decoding and encoding into separate files for a smaller size at run time. This doesn't really matter if you're using encode and decode, because the total size of the combined of both files is higher than the one. This project is for you if you just need a lightweight decode or encode, but not both. If you need both, you really should go to the upstream repository as this was something I am not necessarily going to push changes to quickly, and it helps the original author. Go give them a star! :)
Also, if you read this far, thank you so much!
- 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 280sloc, 9kb
- Proper error messages, eg:
expected '}' or ',' at line 203 col 30
The json.lua file should be dropped into an existing project and required by it:
json = require "json"The library provides the following functions:
Returns a string representing value encoded in JSON.
json.encode({ 1, 2, 3, { x = 10 } }) -- Returns '[1,2,3,{"x":10}]'Returns a value representing the decoded JSON string.
json.decode('[1,2,3,{"x":10}]') -- Returns { 1, 2, 3, { x = 10 } }- 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
nullvalues contained within an array or object are converted toniland are therefore lost upon decoding- Pretty encoding is not supported,
json.encode()only encodes to a compact format
This library is free software; you can redistribute it and/or modify it under the terms of the MIT license. See LICENSE for details.
