diff options
author | Thibault Charbonnier <thibaultcha@me.com> | 2017-08-05 15:19:43 -0700 |
---|---|---|
committer | Yichun Zhang (agentzh) <agentzh@gmail.com> | 2017-11-17 11:46:46 -0800 |
commit | b5e364c7c60167995944ed3a3b9c54d9a377fc1d (patch) | |
tree | b2554317191a34b155aca68be0a54ef364296b58 /README.md | |
parent | 5f9efa4829a72935ddcd40c7da6b1a9e10939b65 (diff) | |
download | lua-cjson-2.1.0.6rc1.tar.gz lua-cjson-2.1.0.6rc1.tar.bz2 lua-cjson-2.1.0.6rc1.zip |
feature: set cjson.array_mt on decoded JSON arrays.2.1.0.6rc1
this can be turned on via cjson.decode_array_with_array_mt(true). off by
default.
Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 37 |
1 files changed, 37 insertions, 0 deletions
@@ -14,6 +14,7 @@ Table of Contents | |||
14 | * [array_mt](#array_mt) | 14 | * [array_mt](#array_mt) |
15 | * [empty_array_mt](#empty_array_mt) | 15 | * [empty_array_mt](#empty_array_mt) |
16 | * [encode_number_precision](#encode_number_precision) | 16 | * [encode_number_precision](#encode_number_precision) |
17 | * [decode_array_with_array_mt](#decode_array_with_array_mt) | ||
17 | 18 | ||
18 | Description | 19 | Description |
19 | =========== | 20 | =========== |
@@ -156,3 +157,39 @@ encode_number_precision | |||
156 | This fork allows encoding of numbers with a `precision` up to 16 decimals (vs. 14 in mpx/lua-cjson). | 157 | This fork allows encoding of numbers with a `precision` up to 16 decimals (vs. 14 in mpx/lua-cjson). |
157 | 158 | ||
158 | [Back to TOC](#table-of-contents) | 159 | [Back to TOC](#table-of-contents) |
160 | |||
161 | decode_array_with_array_mt | ||
162 | -------------------------- | ||
163 | **syntax:** `cjson.decode_array_with_array_mt(enabled)` | ||
164 | |||
165 | **default:** false | ||
166 | |||
167 | If enabled, JSON Arrays decoded by `cjson.decode` will result in Lua | ||
168 | tables with the [`array_mt`](#array_mt) metatable. This can ensure a 1-to-1 | ||
169 | relationship between arrays upon multiple encoding/decoding of your | ||
170 | JSON data with this module. | ||
171 | |||
172 | If disabled, JSON Arrays will be decoded to plain Lua tables, without | ||
173 | the `array_mt` metatable. | ||
174 | |||
175 | The `enabled` argument is a boolean. | ||
176 | |||
177 | Example: | ||
178 | |||
179 | ```lua | ||
180 | local cjson = require "cjson" | ||
181 | |||
182 | -- default behavior | ||
183 | local my_json = [[{"my_array":[]}]] | ||
184 | local t = cjson.decode(my_json) | ||
185 | cjson.encode(t) -- {"my_array":{}} back to an object | ||
186 | |||
187 | -- now, if this behavior is enabled | ||
188 | cjson.decode_array_with_array_mt(true) | ||
189 | |||
190 | local my_json = [[{"my_array":[]}]] | ||
191 | local t = cjson.decode(my_json) | ||
192 | cjson.encode(t) -- {"my_array":[]} properly re-encoded as an array | ||
193 | ``` | ||
194 | |||
195 | [Back to TOC](#table-of-contents) | ||