diff options
| author | Mark Pulford <mark@kyne.com.au> | 2011-12-15 21:39:49 +1030 |
|---|---|---|
| committer | Mark Pulford <mark@kyne.com.au> | 2011-12-15 21:39:49 +1030 |
| commit | 6b2578c5433d8afa46051bb57e3a3224603b264a (patch) | |
| tree | a05d3802d75d65e33964786c39c9a346a92b91ba | |
| parent | d3796d8a20339925a1108074f1da973a9fe59663 (diff) | |
| download | lua-cjson-6b2578c5433d8afa46051bb57e3a3224603b264a.tar.gz lua-cjson-6b2578c5433d8afa46051bb57e3a3224603b264a.tar.bz2 lua-cjson-6b2578c5433d8afa46051bb57e3a3224603b264a.zip | |
Rewrite cjson.encode_sparse_array documentation
Diffstat (limited to '')
| -rw-r--r-- | manual.txt | 54 |
1 files changed, 25 insertions, 29 deletions
| @@ -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 | ||
| 331 | A Lua array is sparse if it is missing a value for at least 1 index. | 331 | Lua CJSON classifies Lua tables into 3 types when encoding as a JSON |
| 332 | Lua CJSON encodes missing values as JSON +null+. Eg, Lua array: | 332 | array. This is determined by the number of values missing for keys up |
| 333 | to the maximum index: | ||
| 333 | 334 | ||
| 334 | .Lua input | 335 | Normal:: |
| 335 | [source,lua] | 336 | All values are available. |
| 336 | ------------ | 337 | Sparse:: |
| 337 | { [3] = "sparse" } | 338 | At least 1 value is missing. |
| 338 | ------------ | 339 | Excessively sparse:: |
| 340 | The number of missing values exceed a configured number. | ||
| 339 | 341 | ||
| 340 | .JSON output | 342 | Lua 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 | ------------------- | 346 | cjson.encode({ [3] = "sparse" }) |
| 347 | -- Returns: '[null,null,"sparse"]' | ||
| 345 | 348 | ||
| 346 | Lua CJSON detects excessively sparse arrays by comparing the number of | 349 | Lua CJSON checks for excessively sparse arrays when the maximum index |
| 347 | items in a Lua array with the maximum index. In particular: | 350 | is greater an the +safe+ limit and +ratio+ is greater than +0+. An |
| 351 | array is _excessively sparse_ when: | ||
| 348 | 352 | ||
| 349 | ----- | 353 | _maximum_index_ > _item_count_ * +ratio+ |
| 350 | maximum index > safe AND maximum index > array_items * ratio | ||
| 351 | ----- | ||
| 352 | 354 | ||
| 353 | By default, attempting to encode excessively sparse arrays will | 355 | By default, attempting to encode excessively sparse arrays will |
| 354 | generate an error. | 356 | generate an error. If +convert+ is set to +true+, excessively sparse |
| 357 | arrays will be converted to a JSON object: | ||
| 355 | 358 | ||
| 356 | If _convert_ is set to +true+, excessively sparse arrays will be | 359 | .Example: Enabling conversion to a JSON object |
| 357 | encoded as a JSON object: | ||
| 358 | |||
| 359 | .Lua input | ||
| 360 | [source,lua] | 360 | [source,lua] |
| 361 | { [1000] = "excessively sparse" } | 361 | cjson.encode_sparse_array(true) |
| 362 | 362 | cjson.encode({ [1000] = "excessively sparse" }) | |
| 363 | .JSON output | 363 | -- Returns: '{"1000":"excessively sparse"}' |
| 364 | [source,javascript] | ||
| 365 | {"1000":"excessively sparse"} | ||
| 366 | |||
| 367 | Setting +ratio+ to +0+ disables checking for excessively sparse arrays. | ||
| 368 | 364 | ||
| 369 | 365 | ||
| 370 | cjson.keep_encode_buffer | 366 | cjson.keep_encode_buffer |
