aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md124
1 files changed, 124 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..7282a4d
--- /dev/null
+++ b/README.md
@@ -0,0 +1,124 @@
1Name
2====
3
4lua-cjson - Fast JSON encoding/parsing
5
6Table of Contents
7=================
8
9* [Name](#name)
10* [Description](#description)
11* [Additions to mpx/lua](#additions)
12 * [encode_empty_table_as_object](#encode_empty_table_as_object)
13 * [empty_array](#empty_array)
14 * [empty_array_mt](#empty_array_mt)
15 * [encode_number_precision](#encode_number_precision)
16
17Description
18===========
19
20This fork of [mpx/lua-cjson](https://github.com/mpx/lua-cjson) is included in
21the [OpenResty](https://openresty.org/) bundle and includes a few bugfixes and
22improvements, especially to facilitate the encoding of empty tables as JSON Arrays.
23
24Please refer to the [lua-cjson documentation](http://www.kyne.com.au/~mark/software/lua-cjson.php)
25for standard usage, this README only provides informations regarding this fork's additions.
26
27See [`mpx/master..openresty/master`](https://github.com/mpx/lua-cjson/compare/master...openresty:master)
28for the complete history of changes.
29
30[Back to TOC](#table-of-contents)
31
32Additions
33=========
34
35encode_empty_table_as_object
36----------------------------
37**syntax:** `cjson.encode_empty_table_as_object(true|false|"on"|"off")`
38
39Change the default behavior when encoding an empty Lua table.
40
41By default, empty Lua tables are encoded as empty JSON Objects (`{}`). If this is set to false,
42empty Lua tables will be encoded as empty JSON Arrays instead (`[]`).
43
44This method either accepts a boolean or a string (`"on"`, `"off"`).
45
46[Back to TOC](#table-of-contents)
47
48empty_array
49-----------
50**syntax:** `cjson.empty_array`
51
52A lightuserdata, similar to `cjson.null`, which will be encoded as an empty JSON Array by
53`cjson.encode()`.
54
55For example, since `encode_empty_table_as_object` is `true` by default:
56
57```lua
58local cjson = require "cjson"
59
60local json = cjson.encode({
61 foo = "bar",
62 some_object = {},
63 some_array = cjson.empty_array
64})
65```
66
67This will generate:
68
69```json
70{
71 "foo": "bar",
72 "some_object": {},
73 "some_array": []
74}
75```
76
77[Back to TOC](#table-of-contents)
78
79empty_array_mt
80--------------
81**syntax:** `setmetatable({}, cjson.empty_array_mt)`
82
83A metatable which can "tag" a table as a JSON Array in case it is empty (that is, if the
84table has no elements, `cjson.encode()` will encode it as an empty JSON Array).
85
86Instead of:
87
88```lua
89local function serialize(arr)
90 if #arr < 1 then
91 arr = cjson.empty_array
92 end
93
94 return cjson.encode({some_array = arr})
95end
96```
97
98This is more concise:
99
100```lua
101local function serialize(arr)
102 setmetatable(arr, cjson.empty_array_mt)
103
104 return cjson.encode({some_array = arr})
105end
106```
107
108Both will generate:
109
110```json
111{
112 "some_array": []
113}
114```
115
116[Back to TOC](#table-of-contents)
117
118encode_number_precision
119-----------------------
120**syntax:** `cjson.encode_number_precision(precision)`
121
122This fork allows encoding of numbers with a `precision` up to 16 decimals (vs. 14 in mpx/lua-cjson).
123
124[Back to TOC](#table-of-contents)