38
CONTENTS
local value,is_empty_array=util.json.decode('{"a":{},"b":[]}',is_empty_array)
log.notice("a: %s%s",type(value.a),is_empty_array[value.a] and ":array" or "")
log.notice("b: %s%s",type(value.b),is_empty_array[value.b] and ":array" or "")
Thus JSON round-trips, i.e.
util.json.encode(util.json.decode(JSON))
produces a JSON string
equivalent to the input
JSON
even in the presence of empty arrays/objects.
All transcoding functions may throw errors on invalid input; using
pcall
/
xpcall
is advised.
util.null
is the JSON
null
constant, distinct from, but sometimes interchangeable with,
nil
. It may be useful
when dealing with some APIs.
The
util.copy(val[,deep_keys])
function returns a deep plain copy of its argument (which can be or
contain an API structure). This may be helpful when working with
util.json.encode
as it doesn't by itself
support encoding API structures. An optional argument,
deep_keys
, can make
util.copy
perform deep
copying of table keys (in case they're tables themselves), otherwise table keys are left intact.
The
util.equal(val1,val2)
function performs a deep equality comparison of its two arguments (each of
which can be or contain an API structure) and returns true if they are equal and false otherwise. Tables are compared
structurally, i.e. they are considered equal if they have equal keys with equal values. Unlike
util.copy
,
util.
←
-
equal
doesn't support comparisons with copied table keys as those are in general undecidable without additional
information (consider
{[{}]=1,[{}]=2}
and
{[{}]=2,[{}]=1}
).
The
util.argpack
function compresses its arguments into a table with an extra
n
member indicating the number
of arguments. It could be implemented as:
util.argpack = function(...)
return {n=select("#",...),...}
end
The
util.argunpack
function expands a table into a value list with the number of elements taken from the
table's
n
member. It could be implemented as:
util.argunpack=function(tbl)
return unpack(tbl,1,tbl.n)
end
In combination,
util.argpack
and
util.argunpack
allow perfect function argument forwarding in presence
of
nil
arguments.
Unlike most APIs, utility functions are available in the global context.
7.6.2.15
Debugging
•
dump
— useful debugging function which outputs the argument to the system log, can be used to inspect
state and even study the modern API itself (try
dump(_G)
!). Note that this function may delay execution
while dumping sufficiently large objects. Other threads may run while execution of the dumping thread is
suspended. If they modify the object being dumped, the resulting dump output may be inconsistent.
•
log
— contains methods
debug
,
info
,
notice
, etc. which accept a format string argument and any extra
arguments it requires, format the string using
string.format
internally and log it at the corresponding
severity level, e.g.
local i=5 local name="Fridge" log.notice("Switching on %d (%s)",i,name)
DLI LPC9 User’s Guide: 1.7.24.0
Summary of Contents for LPC9
Page 1: ...DLI LPC9 User s Guide 1 7 24 0 ...
Page 81: ......