Did you know ... | Search Documentation: |
Pack logtalk -- logtalk-3.85.0/manuals/_sources/libraries/cbor.rst.txt |
.. _library_cbor:
cbor
The cbor
library implements predicates for importing and exporting
data in the Concise Binary Object Representation (CBOR) format:
{Pairs}
, where
each pair uses the representation Key-Value
.bytes(List)
compound terms.chars(List)
, or
codes(List)
. The default when decoding is to use atoms when using
the cbor
object. To decode text strings into lists of chars or
code, use the cbor/1 with the parameter bound to chars
or
codes
. For example:
::
| ?- cbor::parse([0x65,0x68,0x65,0x6c,0x6c,0x6f], Term)
.
Term = hello
yes
| ?- cbor(atom)
::parse([0x65,0x68,0x65,0x6c,0x6c,0x6f], Term)
.
Term = hello
yes
| ?- cbor(chars)
::parse([0x65,0x68,0x65,0x6c,0x6c,0x6f], Term)
.
Term = chars([h,e,l,l,o])
yes
| ?- cbor(codes)
::parse([0x65,0x68,0x65,0x6c,0x6c,0x6f], Term)
.
Term = codes([104,101,108,108,111])
yes
tag(Tag, Data)
compound terms.simple(Simple)
compound
terms.false
, true
, null
, and undefined
are
represented by, respectively, the @false
, @true
, @null
,
and @undefined
compound terms.@infinity
, @negative_infinity
, and
@not_a_number
are used to represent the corresponding CBOR
elements.@zero
and @negative_zero
can be used
as an alternative for encoding. The decoder, however, produces the
0.0
and -0.0
floats.Encoding is accomplished using the generate/2 predicate. For example:
::
| ?- cbor::generate([a,{b-c}], Encoding)
.
Encoding = [0x9f,0x61,0x61,0xbf,0x61,0x62,0x61,0x63,0xff,0xff]
yes
The encoding of arrays and maps uses indefinite-length encoding. All floats are currently encoded using decimal fractions. Encoding indicators and big floats are not currently supported.
Decoding is accomplished using the parse/2 predicate. For example:
::
| ?- cbor::parse([0x9f,0x61,0x61,0xbf,0x61,0x62,0x61,0x63,0xff,0xff], Term)
.
Term = [a,{b-c}]
yes
Open the `../../docs/library_index.html#cbor <../../docs/library_index.html#cbor>`__ link in a web browser.
To load all entities in this library, load the loader.lgt
file:
::
| ?- logtalk_load(cbor(loader))
.
To test this library predicates, load the tester.lgt
file:
::
| ?- logtalk_load(cbor(tester))
.