|
1 | 1 | # hjson-lua |
2 | 2 | A lightweight H/JSON library for Lua |
3 | 3 |
|
4 | | -Ported from [hjson-py](https://github.com/hjson/hjson-py). Inspired by rxi - [json.lua](https://github.com/rxi/json.lua). |
| 4 | +Ported from [hjson-py](https://github.com/hjson/hjson-py). Inspired by rxi - [json.lua](https://github.com/rxi/json.lua). |
| 5 | + |
| 6 | +* Implemented in pure Lua: tested with latest Lua (5.3) only |
| 7 | + |
| 8 | +## Setup |
| 9 | + |
| 10 | +1. drop [hjson.lua](tree/master/hjson.lua) and [hjson folder](tree/master/hjson) folder into your project |
| 11 | +2. require hjson.lua |
| 12 | + * `hjson = require "hjson"` |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +Library exports json.lua like and JS like api. |
| 17 | + |
| 18 | +- Lua object to HJSON - returns HJSON string |
| 19 | + - `encode(obj, indent, skipkeys)` |
| 20 | + - `stringify(obj, indent, skipkeys)` |
| 21 | + - Parameters: |
| 22 | + - `obj` - Lua object - `table`, `string`, `number`, `nil`, `boolean` |
| 23 | + - `indent` - default `" "`. Accepts string of whitespace characters or a number representing number of spaces (non indented HJSON is JSON, automatically forwards to `_to_json` version) |
| 24 | + - `skipkeys` - default `true` Skips invalid keys. If false throws error on invalid key. |
| 25 | + - Valid key types: `boolean`, `nil`, `string` |
| 26 | +- Lua object to JSON - returns JSON string |
| 27 | + - `encode(obj, indent, skipkeys)` |
| 28 | + - `stringify(obj, indent, skipkeys)` |
| 29 | + - Parameters: |
| 30 | + - `obj` - Lua object - `table`, `string`, `number`, `nil`, `boolean` |
| 31 | + - `indent` - default `" "`. Accepts string of whitespace characters or a number representing number of spaces |
| 32 | + - `skipkeys` - default `true` Skips invalid keys. If false throws error on invalid key. |
| 33 | + - Valid key types: `boolean`, `nil`, `string` |
| 34 | +- H/JSON to Lua object - returns Lua object |
| 35 | + - `decode(str, strict, object_hook, object_pairs_hook)` |
| 36 | + - `parse(str, strict, object_hook, object_pairs_hook)` |
| 37 | + - Parameters: |
| 38 | + - `str` has to be valid HJSON string |
| 39 | + - `strict` default `true` . If true parse/decode fails on invalid control characters. |
| 40 | + - `object_hook` - `function(obj)` hook which allows to adjust tables generated from JSON on per JSON object basis (including nested objects). `obj` is lua `table`. |
| 41 | + - `object_pairs_hook` - `function(pairs)` hook which allows to adjust table before generation. `pairs` is table (in array form) composited from `key/value` pairs. It is called before the table for `object_hook` is generated. |
| 42 | + |
| 43 | +*`null` values contained within an array or object are converted to `nil` and are therefore lost upon decoding.* |
| 44 | + |
| 45 | +## License |
| 46 | +This library is free software; you can redistribute it and/or modify it under |
| 47 | +the terms of the MIT license. See [LICENSE](LICENSE) for details. |
0 commit comments