Skip to content

Commit 6cbf315

Browse files
committed
added encode forward to to_json if indent is set to 0
added Usage
1 parent 98356da commit 6cbf315

4 files changed

Lines changed: 62 additions & 15 deletions

File tree

README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,47 @@
11
# hjson-lua
22
A lightweight H/JSON library for Lua
33

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.

hjson.lua

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,28 @@ local _decoder
88
local _encoder
99
local _encoderH
1010

11-
function decode(str)
12-
if not _decoder then
13-
_decoder = decoder:new()
14-
end
11+
local function decode(str, strict, object_hook, object_pairs_hook)
12+
_decoder = decoder:new(strict, object_hook, object_pairs_hoo)
1513
return _decoder:decode(str)
1614
end
1715

18-
function encode_json(str)
19-
if not _encoder then
20-
_encoder = encoder:new()
21-
end
22-
return _encoder:encode(str)
16+
local function encode_json(obj, indent, skipkeys)
17+
_encoder = encoder:new(indent, skipkeys)
18+
return _encoder:encode(obj)
2319
end
2420

25-
function encode(str)
21+
local function encode(obj, indent, skipkeys)
22+
forward = false
23+
if indent == "" or indent == false or indent == 0 then
24+
return encode_json(obj, indent, skipkeys)
25+
end
2626
if not _encoderH then
2727
_encoderH = encoderH:new()
2828
end
29-
return _encoderH:encode(str)
29+
return _encoderH:encode(obj)
3030
end
3131

32-
hjson = {
32+
local hjson = {
3333
encode = encode,
3434
stringify = encode,
3535
encode_to_json = encode_json,

hjson/encoder.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ function JsonEncoder:new(indent, skipkeys, sort_keys, item_sort_key)
5757
indent = string.rep(" ", indent)
5858
end
5959

60+
if type(indent) == "boolean" and indent then
61+
indent = " "
62+
end
63+
6064
if indent and not indent:match("%s*") then
6165
error("Indent has to contain only whitespace characters or be a number")
6266
end

hjson/encoderH.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function HjsonEncoder:new(indent, skipkeys, sort_keys, item_sort_key)
117117
if indent == nil then
118118
indent = " "
119119
end
120-
if (type(indent) ~= "number" or indent < 2) and (type(indent) ~= "string" or not indent:find("%s%s+")) then
120+
if (type(indent) ~= "number" or indent < 2) and (type(indent) ~= "string" or not indent:find("%s%s*")) then
121121
error("indent (#1 parameter) has to be of type string with at least 2 spaces or integer greater than 1")
122122
end
123123
if type(indent) == "number" then
@@ -289,7 +289,7 @@ function HjsonEncoder:new(indent, skipkeys, sort_keys, item_sort_key)
289289
end
290290
local func = encodeFunctionMap[_type]
291291
if type(func) == "function" then
292-
return func(o, encode)
292+
return func(o, _encode)
293293
end
294294
error("Unexpected type '" .. _type .. "'")
295295
end

0 commit comments

Comments
 (0)