diff options
author | Thibault Charbonnier <thibaultcha@me.com> | 2017-07-08 21:54:18 -0700 |
---|---|---|
committer | Yichun Zhang (agentzh) <agentzh@gmail.com> | 2017-11-15 20:41:57 -0800 |
commit | 5f9efa4829a72935ddcd40c7da6b1a9e10939b65 (patch) | |
tree | 319cdf13f99c753f85820c3fabb89bfa50496dae /README.md | |
parent | a9af7965219710ac1ec1e2bbf1a63eb77be622b7 (diff) | |
download | lua-cjson-5f9efa4829a72935ddcd40c7da6b1a9e10939b65.tar.gz lua-cjson-5f9efa4829a72935ddcd40c7da6b1a9e10939b65.tar.bz2 lua-cjson-5f9efa4829a72935ddcd40c7da6b1a9e10939b65.zip |
feature: added new cjson.array_mt metatable to allow enforcing JSON array encoding.
Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 34 |
1 files changed, 34 insertions, 0 deletions
@@ -11,6 +11,7 @@ Table of Contents | |||
11 | * [Additions to mpx/lua](#additions) | 11 | * [Additions to mpx/lua](#additions) |
12 | * [encode_empty_table_as_object](#encode_empty_table_as_object) | 12 | * [encode_empty_table_as_object](#encode_empty_table_as_object) |
13 | * [empty_array](#empty_array) | 13 | * [empty_array](#empty_array) |
14 | * [array_mt](#array_mt) | ||
14 | * [empty_array_mt](#empty_array_mt) | 15 | * [empty_array_mt](#empty_array_mt) |
15 | * [encode_number_precision](#encode_number_precision) | 16 | * [encode_number_precision](#encode_number_precision) |
16 | 17 | ||
@@ -76,6 +77,39 @@ This will generate: | |||
76 | 77 | ||
77 | [Back to TOC](#table-of-contents) | 78 | [Back to TOC](#table-of-contents) |
78 | 79 | ||
80 | array_mt | ||
81 | -------- | ||
82 | **syntax:** `setmetatable({}, cjson.array_mt)` | ||
83 | |||
84 | When lua-cjson encodes a table with this metatable, it will systematically | ||
85 | encode it as a JSON Array. The resulting, encoded Array will contain the array | ||
86 | part of the table, and will be of the same length as the `#` operator on that | ||
87 | table. Holes in the table will be encoded with the `null` JSON value. | ||
88 | |||
89 | Example: | ||
90 | |||
91 | ```lua | ||
92 | local t = { "hello", "world" } | ||
93 | setmetatable(t, cjson.array_mt) | ||
94 | cjson.encode(t) -- ["hello","world"] | ||
95 | ``` | ||
96 | |||
97 | Or: | ||
98 | |||
99 | ```lua | ||
100 | local t = {} | ||
101 | t[1] = "one" | ||
102 | t[2] = "two" | ||
103 | t[4] = "three" | ||
104 | t.foo = "bar" | ||
105 | setmetatable(t, cjson.array_mt) | ||
106 | cjson.encode(t) -- ["one","two",null,"three"] | ||
107 | ``` | ||
108 | |||
109 | This value was introduced in the `2.1.0.5` release of this module. | ||
110 | |||
111 | [Back to TOC](#table-of-contents) | ||
112 | |||
79 | empty_array_mt | 113 | empty_array_mt |
80 | -------------- | 114 | -------------- |
81 | **syntax:** `setmetatable({}, cjson.empty_array_mt)` | 115 | **syntax:** `setmetatable({}, cjson.empty_array_mt)` |