Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rxi/json.lua
A lightweight JSON library for Lua
https://github.com/rxi/json.lua
Last synced: about 2 hours ago
JSON representation
A lightweight JSON library for Lua
- Host: GitHub
- URL: https://github.com/rxi/json.lua
- Owner: rxi
- License: mit
- Created: 2015-08-12T19:18:12.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-11-28T04:38:13.000Z (12 months ago)
- Last Synced: 2024-10-31T11:36:54.060Z (13 days ago)
- Language: Lua
- Homepage:
- Size: 28.3 KB
- Stars: 1,856
- Watchers: 45
- Forks: 367
- Open Issues: 26
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-playdate - rxi/json.lua - A lightweight JSON library for Lua. (Game Development / Programming Frameworks & Languages)
README
# ![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 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](LICENSE) for details.