CBOR


Reference

encode( data )

Serialize a data.

decode( str )

Deserialize a string.

decoder( src )

Accept a string or a ltn12.source and returns a iterator.

The iterator gives a couple of values, the interesting value is the second.

set_string( str )

Configures the behaviour of encode. The valid options are 'byte_string', 'text_string' and 'check_utf8' (only if Lua 5.3). The default is 'text_string'.

set_array( str )

Configures the behaviour of encode. The valid options are 'without_hole', 'with_hole' and 'always_as_map'. The default is 'without_hole'.

set_nil( str )

Configures the behaviour of encode. The valid options are 'null' and 'undef'. The default is 'undef'.

set_float( str )

Configures the behaviour of encode. The valid options are 'half', 'single' and 'double'. The default is usually 'double'.

register_tag( tag, builder )

Configures the behaviour of decode. Registers a function builder which will be called for a tagged value.

Data Conversion

Examples

Basic usage

local c = require 'CBOR'

c.set_float'single'
c.set_nil'null'
c.set_array'with_hole'
c.set_string'text_string'

cbor = c.encode(data)
data = c.decode(cbor)

See other usages in Stream and Tag.

Advanced usage

The following Lua hack allows to have several instances of the module CBOR, each one with its own settings.

local c1 = require 'CBOR'
package.loaded['CBOR'] = nil    -- the hack is here
local c2 = require 'CBOR'

c1.set_array'without_hole'
c2.set_array'always_as_map'