aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2011-12-15 21:39:49 +1030
committerMark Pulford <mark@kyne.com.au>2011-12-15 21:39:49 +1030
commit6b2578c5433d8afa46051bb57e3a3224603b264a (patch)
treea05d3802d75d65e33964786c39c9a346a92b91ba
parentd3796d8a20339925a1108074f1da973a9fe59663 (diff)
downloadlua-cjson-6b2578c5433d8afa46051bb57e3a3224603b264a.tar.gz
lua-cjson-6b2578c5433d8afa46051bb57e3a3224603b264a.tar.bz2
lua-cjson-6b2578c5433d8afa46051bb57e3a3224603b264a.zip
Rewrite cjson.encode_sparse_array documentation
-rw-r--r--manual.txt54
1 files changed, 25 insertions, 29 deletions
diff --git a/manual.txt b/manual.txt
index 76188d6..37d8229 100644
--- a/manual.txt
+++ b/manual.txt
@@ -328,43 +328,39 @@ convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]])
328-- "safe" must be a positive integer (>0). Default: 10 328-- "safe" must be a positive integer (>0). Default: 10
329------------ 329------------
330 330
331A Lua array is sparse if it is missing a value for at least 1 index. 331Lua CJSON classifies Lua tables into 3 types when encoding as a JSON
332Lua CJSON encodes missing values as JSON +null+. Eg, Lua array: 332array. This is determined by the number of values missing for keys up
333to the maximum index:
333 334
334.Lua input 335Normal::
335[source,lua] 336 All values are available.
336------------ 337Sparse::
337{ [3] = "sparse" } 338 At least 1 value is missing.
338------------ 339Excessively sparse::
340 The number of missing values exceed a configured number.
339 341
340.JSON output 342Lua CJSON encodes sparse Lua arrays by as JSON arrays using JSON +null+.
341[source,javascript] 343
342------------------- 344.Example: Encoding a sparse array
343[null,null,"sparse"] 345[source,lua]
344------------------- 346cjson.encode({ [3] = "sparse" })
347-- Returns: '[null,null,"sparse"]'
345 348
346Lua CJSON detects excessively sparse arrays by comparing the number of 349Lua CJSON checks for excessively sparse arrays when the maximum index
347items in a Lua array with the maximum index. In particular: 350is greater an the +safe+ limit and +ratio+ is greater than +0+. An
351array is _excessively sparse_ when:
348 352
349----- 353_maximum_index_ > _item_count_ * +ratio+
350maximum index > safe AND maximum index > array_items * ratio
351-----
352 354
353By default, attempting to encode excessively sparse arrays will 355By default, attempting to encode excessively sparse arrays will
354generate an error. 356generate an error. If +convert+ is set to +true+, excessively sparse
357arrays will be converted to a JSON object:
355 358
356If _convert_ is set to +true+, excessively sparse arrays will be 359.Example: Enabling conversion to a JSON object
357encoded as a JSON object:
358
359.Lua input
360[source,lua] 360[source,lua]
361{ [1000] = "excessively sparse" } 361cjson.encode_sparse_array(true)
362 362cjson.encode({ [1000] = "excessively sparse" })
363.JSON output 363-- Returns: '{"1000":"excessively sparse"}'
364[source,javascript]
365{"1000":"excessively sparse"}
366
367Setting +ratio+ to +0+ disables checking for excessively sparse arrays.
368 364
369 365
370cjson.keep_encode_buffer 366cjson.keep_encode_buffer