lua.wetgenes.json
local wjson=require("wetgenes.json")
-- or export the main functions like so
local json_encode,json_decode=require("wetgenes.json"):export("encode","decode")
other json encode/decode using pure lua library seemed too slow, here is a fast and loose one lets see if it goes any faster :) should be a direct replacement for JSON4Lua which is what I was using before I profiled where all the time was getting spent...
I needed it to be pure json as I was running it on googles appengine so the lua was actually running in java, no C available.
Anyhow I hope it is useful, in order to get it running faster I cut across some corners so you may find some obscure problems.
lua.wetgenes.json.decode
json_table = wjson.decode(json_string)
json_table = wjson.decode(json_string,opts)
Convert a json string into a lua table.
Set opts.null to wetgenes.json.null (or indeed any other value) if you would like to have this as nulls in your results. By default nulls are replaced with nil and therefore invisible.
Any object key string that looks like a number will be converted to a number. This will probably reverse any numbers we converted to strings when encoding. Set opts.keystring=true to turn off this behaviour.
lua.wetgenes.json.encode
json_string = wjson.encode(json_table)
json_string = wjson.encode(json_table,opts)
Convert a lua table into a json string. Note it must be valid json, primarily make sure that the table is either an array or a dictionary but never both. Note that we can not tell the difference between an empty array and an empty object and will assume it is an object.
An array must have a length>0 and contain an element in the first slot, eg array[1] and only contain numerical integer keys between 1 and the length. This allows for the possibility of some nil holes depending on the length lua returns but holes are not a good idea in arrays in lua. Best to use false or the special wjson.null value and avoid holes.
Also some of the internal lua types will cause errors, eg functions as these can not be converted into json.
include nulls in the output by using wetgenes.json.null
opts is an optional table that can set the following options.
ops.pretty=true
ops.pretty=" "
Enable pretty printing, line feeds and indents and set each
indent level to multiples of the given string or " ".
ops.white=true
ops.white=" "
Enable white space but not lines or indents, just a single space
between value assignment to make line wrapping easier.
ops.sort=true
Sort the keys, so we can create stable output for better diffing.