| 1 | use utf8; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | package DR::Tnt::Msgpack::Types::Bool; | ||
| 6 | use Carp; | ||
| 7 | $Carp::Internal{ (__PACKAGE__) }++; | ||
| 8 | use Scalar::Util (); | ||
| 9 | use feature 'state'; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my ($class, $value) = @_; | ||
| 13 | |||
| 14 | $value = $value ? 1 : 0; | ||
| 15 | bless \$value => ref($class) || $class; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub TO_MSGPACK { | ||
| 19 | my ($self) = @_; | ||
| 20 | return pack 'C', 0xC3 if $$self; | ||
| 21 | return pack 'C', 0xC2; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub TO_JSON { | ||
| 25 | my ($self) = @_; | ||
| 26 | |||
| 27 | state ($true, $false); | ||
| 28 | |||
| 29 | unless (defined $true) { | ||
| 30 | if (eval "require JSON::XS; 1") { | ||
| 31 | $true = JSON::XS::true(); | ||
| 32 | $false = JSON::XS::false(); | ||
| 33 | } elsif (eval "require JSON; 1") { | ||
| 34 | $true = JSON::true(); | ||
| 35 | $false = JSON::false(); | ||
| 36 | } else { | ||
| 37 | croak "JSON or JSON::XS must be installed"; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | return $true if $$self; | ||
| 42 | return $false; | ||
| 43 | } | ||
| 44 | |||
| 45 | =head1 NAME | ||
| 46 | |||
| 47 | DR::Tnt::Msgpack::Types::Bool - container for bool. | ||
| 48 | |||
| 49 | =head1 SYNOPSIS | ||
| 50 | |||
| 51 | use DR::Tnt::Msgpack::Types::Bool; | ||
| 52 | |||
| 53 | my $o = DR::Tnt::Msgpack::Types::Bool->new(1); | ||
| 54 | my $blob = msgpack($o); | ||
| 55 | |||
| 56 | =head1 DESCRIPTION | ||
| 57 | |||
| 58 | See L<DR::Tnt::Msgpack::Types>. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | 1; |