--- CBOR-XS/XS.pm 2013/10/26 23:02:55 1.5 +++ CBOR-XS/XS.pm 2013/10/27 20:40:25 1.6 @@ -14,7 +14,17 @@ # OO-interface $coder = CBOR::XS->new; - #TODO + $binary_cbor_data = $coder->encode ($perl_value); + $perl_value = $coder->decode ($binary_cbor_data); + + # prefix decoding + + my $many_cbor_strings = ...; + while (length $many_cbor_strings) { + my ($data, $length) = $cbor->decode_prefix ($many_cbor_strings); + # data was decoded + substr $many_cbor_strings, 0, $length, ""; # remove decoded cbor string + } =head1 DESCRIPTION @@ -28,7 +38,8 @@ can represent something in JSON, you should be able to represent it in CBOR. -This makes it a faster and more compact binary alternative to JSON. +This makes it a faster and more compact binary alternative to JSON, with +the added ability of supporting serialising of perl objects. The primary goal of this module is to be I and the secondary goal is to be I. To reach the latter goal it was written in C. @@ -50,6 +61,8 @@ use Exporter; use XSLoader; +use Types::Serialiser; + our $MAGIC = "\xd9\xd9\xf7"; =head1 FUNCTIONAL INTERFACE @@ -196,26 +209,43 @@ array or hash, respectively. The keys of the map will be stringified during this process. -=item true, false +=item null + +CBOR null becomes C in Perl. + +=item true, false, undefined -These CBOR values become C and C, +These CBOR values become C, +C and C, respectively. They are overloaded to act almost exactly like the numbers -C<1> and C<0>. You can check whether a scalar is a CBOR boolean by using -the C function. +C<1> and C<0> (for true and false) or to throw an exception on access (for +error). See the L manpage for details. + +=item CBOR tag 256 (perl object) + +The tag value C<256> (TODO: pending iana registration) will be used to +deserialise a Perl object. + +TODO For this to work, the class must be loaded and must have a +C method. The decoder will then call the C method +with the constructor arguments provided by the C method (see +below). + +The C method must return a single value that will then be used +as the deserialised value. -=item null, undefined +=item CBOR tag 55799 (magic header) -CBOR null and undefined values becomes C in Perl (in the future, -Undefined may raise an exception or something else). +The tag 55799 is ignored (this tag implements the magic header). -=item tags +=item other CBOR tags -Tagged items consists of a numeric tag and another CBOR value. The tag -55799 is ignored (this tag implements the magic header). +Tagged items consists of a numeric tag and another CBOR value. Tags not +handled internally are currently converted into a L +object, which is simply a blessed array reference consisting of the +numeric tag value followed by the (decoded) CBOR value. -All other tags are currently converted into a L object, -which is simply a blessed array reference consistsing of the numeric tag -value followed by the (decoded) BOR value. +In the future, support for user-supplied conversions might get added. =item anything else @@ -258,10 +288,11 @@ pair. The (numerical) tag will be encoded as a CBOR tag, the value will be encoded as appropriate for the value. -=item CBOR::XS::true, CBOR::XS::false +=item Types::Serialiser::true, Types::Serialiser::false, Types::Serialiser::error -These special values become CBOR true and CBOR false values, -respectively. You can also use C<\1> and C<\0> directly if you want. +These special values become CBOR true, CBOR false and CBOR undefined +values, respectively. You can also use C<\1>, C<\0> and C<\undef> directly +if you want. =item blessed objects @@ -419,34 +450,16 @@ =cut -our $true = do { bless \(my $dummy = 1), "CBOR::XS::Boolean" }; -our $false = do { bless \(my $dummy = 0), "CBOR::XS::Boolean" }; - -sub true() { $true } -sub false() { $false } - -sub is_bool($) { - UNIVERSAL::isa $_[0], "CBOR::XS::Boolean" -# or UNIVERSAL::isa $_[0], "CBOR::Literal" -} - XSLoader::load "CBOR::XS", $VERSION; -package CBOR::XS::Boolean; - -use overload - "0+" => sub { ${$_[0]} }, - "++" => sub { $_[0] = ${$_[0]} + 1 }, - "--" => sub { $_[0] = ${$_[0]} - 1 }, - fallback => 1; - -1; - =head1 SEE ALSO The L and L modules that do similar, but human-readable, serialisation. +The L module provides the data model for true, false +and error values. + =head1 AUTHOR Marc Lehmann @@ -454,3 +467,5 @@ =cut +1 +