CBOR-PP

 view release on metacpan or  search on metacpan

lib/CBOR/PP/Decode.pm  view on Meta::CPAN


This implements a basic CBOR decoder in pure Perl.

=head1 MAPPING CBOR TO PERL

=over

=item * All tags are ignored. (This could be iterated on later.)

=item * Indefinite-length objects are supported, but streamed parsing
is not; the data structure must be complete to be decoded.

=item * CBOR text strings are decoded to UTF8-flagged strings, while
binary strings are decoded to non-UTF8-flagged strings. In practical
terms, this means that a decoded CBOR binary string will have no code point
above 255, while a decoded CBOR text string can contain any valid Unicode
code point.

=item * null, undefined, true, and false become undef, undef,
Types::Serialiser::true(), and Types::Serialiser::false(), respectively.
(NB: undefined is deserialized as an error object in L<CBOR::XS>,
which doesn’t seem to make sense.)

=back

=head1 TODO

t/against_cbor_xs.t  view on Meta::CPAN


            { map { ($_ => undef) } 1 .. 1 },
            { map { ($_ => undef) } 1 .. 23},
            { map { ($_ => undef) } 1 .. 24},
            { map { ($_ => undef) } 1 .. 255 },
            { map { ($_ => undef) } 1 .. 256 },
        ],
    );

    for my $item ( @tests ) {
        my ($cbor, $decoded);

        $cbor = CBOR::XS::encode_cbor($item);
        $decoded = CBOR::PP::decode($cbor);

        is_deeply(
            $decoded,
            $item,
            'we decode what CBOR::XS encoded',
        ) or diag explain $decoded;

        $cbor = CBOR::PP::encode($item) or die "failed to encode()?";
        $decoded = CBOR::XS::decode_cbor($cbor);

        is_deeply(
            $decoded,
            $item,
            sprintf( "CBOR::XS decodes what we encoded (%d bytes)", length $cbor),
        ) or diag sprintf('%v.02x', $cbor);
    }
}

done_testing;

t/examples.t  view on Meta::CPAN

    [ [1, [2, 3], [4, 5]] => '83018202039f0405ff' ],
    [ [1, [2, 3], [4, 5]] => '83019f0203ff820405' ],
    [ [ 1 .. 25 ] => '9f0102030405060708090a0b0c0d0e0f101112131415161718181819ff' ],
    [ { a => 1, b => [2,3] } => 'bf61610161629f0203ffff' ],
    [ ['a', { b => 'c' }] => '826161bf61626163ff' ],
    [ { Fun => Types::Serialiser::true(), Amt => -2 } => 'bf6346756ef563416d7421ff' ],
);


for my $t (@decode) {
    my $decoded = CBOR::PP::decode( pack( 'H*', $t->[1] ) );
    is_deeply(
        $decoded,
        $t->[0],
        sprintf('Decode %s', $t->[1])
    ) or diag explain $decoded;

    is_deeply(
        scalar( CBOR::PP::decode( CBOR::PP::encode( $t->[0] ) ) ),
        $t->[0],
        sprintf("Round-trip: $t->[1]"),
    );
}

done_testing;

t/utf8.pl  view on Meta::CPAN

use warnings;

use Test::More;

use CBOR::PP;

my $str = CBOR::PP::decode("\x64éé");

ok(
    utf8::is_utf8($str),
    'UTF-8 string is decoded as such',
);

$str = CBOR::PP::decode("\x44éé");

ok(
    !utf8::is_utf8($str),
    'binary string is decoded as such',
);

$str = pack 'U', 0xfc;
is(
    sprintf('%v.02x', CBOR::PP::encode($str)),
    '62.c3.bc',
    'Encode UTF-8 U+00fc (ü)',
);

$str = "\xc3\xbc";



( run in 0.327 second using v1.01-cache-2.11-cpan-26ccb49234f )