| 1 | package JSON::YY; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 27 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 27 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 27 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 27 | our $VERSION = '0.07'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 27 | require XSLoader; | |||||||||||||||||||||||||||
| 10 | 27 | XSLoader::load('JSON::YY', $VERSION); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 27 | our @EXPORT_OK = qw(encode_json decode_json decode_json_ro); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 27 | my @DOC_KEYWORDS = qw(jdoc jget jgetp jset jdel jhas jclone jencode | |||||||||||||||||||||||||||
| 15 | jstr jnum jbool jnull jarr jobj jtype jlen jkeys jdecode | ||||||||||||||||||||||||||||
| 16 | jiter jnext jkey jpatch jmerge jfrom jvals jeq | ||||||||||||||||||||||||||||
| 17 | jpp jraw jread jwrite jpaths jfind | ||||||||||||||||||||||||||||
| 18 | jis_obj jis_arr jis_str jis_num jis_int jis_real jis_bool jis_null); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # functional API — fast XS path, always utf8 | ||||||||||||||||||||||||||||
| 21 | 27 | *encode_json = \&_xs_encode_json; | |||||||||||||||||||||||||||
| 22 | 27 | *decode_json = \&_xs_decode_json; | |||||||||||||||||||||||||||
| 23 | 27 | *decode_json_ro = \&_xs_decode_json_ro; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # max_depth reaches XS as a U32: -1 would wrap to 4294967295, which is not | ||||||||||||||||||||||||||||
| 26 | # "unlimited" but "deep enough to blow the C stack". | ||||||||||||||||||||||||||||
| 27 | sub _checked_max_depth { | ||||||||||||||||||||||||||||
| 28 | 21 | ▶ | my ($self, $val) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_checked_max_depth: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 29 | 21 | ▶ | $val = 512 unless defined $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 21 | ▶ | Carp::croak("max_depth must be a non-negative integer (got '$val')") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$val =~ /\A[0-9]+\z/ and $val <= 4294967295
MC/DC100%2/2
$val =~ /\A[0-9]+\z/ and $val <= 4294967295
$val =~ /\A[0-9]+\z/
$val <= 4294967295
Truth table100%3/3
A and B
A= $val =~ /\A[0-9]+\z/B= $val <= 4294967295
| |||||||||||||||||||||||||||||
| 31 | unless $val =~ /\A[0-9]+\z/ && $val <= 0xFFFFFFFF; | ||||||||||||||||||||||||||||
| 32 | 11 | $self->_set_max_depth($val); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 27 | my %SETTERS = ( | |||||||||||||||||||||||||||
| 36 | utf8 => \&_set_utf8, | ||||||||||||||||||||||||||||
| 37 | pretty => \&_set_pretty, | ||||||||||||||||||||||||||||
| 38 | canonical => \&_set_canonical, | ||||||||||||||||||||||||||||
| 39 | allow_nonref => \&_set_allow_nonref, | ||||||||||||||||||||||||||||
| 40 | allow_unknown => \&_set_allow_unknown, | ||||||||||||||||||||||||||||
| 41 | allow_blessed => \&_set_allow_blessed, | ||||||||||||||||||||||||||||
| 42 | convert_blessed => \&_set_convert_blessed, | ||||||||||||||||||||||||||||
| 43 | max_depth => \&_checked_max_depth, | ||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub import { | ||||||||||||||||||||||||||||
| 47 | 43 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||
| 48 | 43 | my @exports; | |||||||||||||||||||||||||||
| 49 | my @flags; | ||||||||||||||||||||||||||||
| 50 | 43 | my $want_doc = 0; | |||||||||||||||||||||||||||
| 51 | 43 | for my $arg (@_) { | |||||||||||||||||||||||||||
| 52 | 62 | ▶ | if ($arg eq ':doc') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 53 | 18 | $want_doc = 1; | |||||||||||||||||||||||||||
| 54 | } elsif ($arg =~ /^-(.+)/) { | ||||||||||||||||||||||||||||
| 55 | 4 | push @flags, $1; | |||||||||||||||||||||||||||
| 56 | } else { | ||||||||||||||||||||||||||||
| 57 | 40 | push @exports, $arg; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 43 | my $caller = caller; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # activate keywords via XS::Parse::Keyword hint keys | ||||||||||||||||||||||||||||
| 63 | 43 | ▶ | if ($want_doc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 18 | $^H{"JSON::YY/$_"} = 1 for @DOC_KEYWORDS; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 43 | ▶ | if (@flags) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 3 | my $coder = $class->new; | |||||||||||||||||||||||||||
| 68 | 3 | for my $f (@flags) { | |||||||||||||||||||||||||||
| 69 | 4 | ▶ | my $setter = $SETTERS{$f} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | or Carp::croak("unknown flag: -$f"); | ||||||||||||||||||||||||||||
| 71 | 4 | $coder->$setter(1); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 27 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | 27 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | # a qw() import in the same scope leaves the keyword hint set, and the | ||||||||||||||||||||||||||||
| 76 | # compiler would then bypass these closures entirely -- silently | ||||||||||||||||||||||||||||
| 77 | # dropping the flags the caller just asked for | ||||||||||||||||||||||||||||
| 78 | 3 | delete $^H{"JSON::YY/$_"} for qw(encode_json decode_json); | |||||||||||||||||||||||||||
| 79 | 3 | ▶ | *{"${caller}::encode_json"} = sub { $coder->encode($_[0]) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 80 | 3 | ▶ | *{"${caller}::decode_json"} = sub { $coder->decode($_[0]) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 43 | ▶ | if (@exports) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 27 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 84 | 22 | for my $e (@exports) { | |||||||||||||||||||||||||||
| 85 | Carp::croak("'$e' is not exported by JSON::YY") | ||||||||||||||||||||||||||||
| 86 | 40 | ▶ | unless grep { $_ eq $e } @EXPORT_OK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 40 | *{"${caller}::$e"} = \&{$e}; | |||||||||||||||||||||||||||
| 88 | # also enable keyword for exported functions | ||||||||||||||||||||||||||||
| 89 | 40 | $^H{"JSON::YY/$e"} = 1; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # chaining setters | ||||||||||||||||||||||||||||
| 95 | 10 | ▶ | sub utf8 { $_[0]->_set_utf8($_[1] // 1); $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
utf8: called CC=2 SCAR=7.2
Pod
documented
Condition50%1/2
$_[1] // 1
MC/DC0%0/1
$_[1] // 1
$_[1]
Truth table50%1/2
A
A= $_[1]
| |||||||||||||||||||||||||||||
| 96 | 3 | ▶ | sub pretty { $_[0]->_set_pretty($_[1] // 1); $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
pretty: called CC=2 SCAR=7.2
Pod
documented
Condition50%1/2
$_[1] // 1
MC/DC0%0/1
$_[1] // 1
$_[1]
Truth table50%1/2
A
A= $_[1]
| |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | sub canonical { $_[0]->_set_canonical($_[1] // 1); $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
canonical: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/2
$_[1] // 1
MC/DC0%0/1
$_[1] // 1
$_[1]
Truth table0%0/2
A
A= $_[1]
| |||||||||||||||||||||||||||||
| 98 | 3 | ▶ | sub allow_nonref { $_[0]->_set_allow_nonref($_[1] // 1); $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
allow_nonref: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
$_[1] // 1
MC/DC100%1/1
$_[1] // 1
$_[1]
Truth table100%2/2
A
A= $_[1]
| |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub allow_unknown { $_[0]->_set_allow_unknown($_[1] // 1); $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
allow_unknown: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/2
$_[1] // 1
MC/DC0%0/1
$_[1] // 1
$_[1]
Truth table0%0/2
A
A= $_[1]
| |||||||||||||||||||||||||||||
| 100 | 1 | ▶ | sub allow_blessed { $_[0]->_set_allow_blessed($_[1] // 1); $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
allow_blessed: called CC=2 SCAR=7.2
Pod
documented
Condition50%1/2
$_[1] // 1
MC/DC0%0/1
$_[1] // 1
$_[1]
Truth table50%1/2
A
A= $_[1]
| |||||||||||||||||||||||||||||
| 101 | 2 | ▶ | sub convert_blessed { $_[0]->_set_convert_blessed($_[1] // 1); $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
convert_blessed: called CC=2 SCAR=7.2
Pod
documented
Condition50%1/2
$_[1] // 1
MC/DC0%0/1
$_[1] // 1
$_[1]
Truth table50%1/2
A
A= $_[1]
| |||||||||||||||||||||||||||||
| 102 | 4 | ▶ | sub max_depth { $_[0]->_checked_max_depth($_[1]); $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
max_depth: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # wrap XS new to accept keyword args | ||||||||||||||||||||||||||||
| 105 | { | ||||||||||||||||||||||||||||
| 106 | 27 | my $orig_new = JSON::YY->can('new'); | |||||||||||||||||||||||||||
| 107 | 27 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 108 | *new = sub { | ||||||||||||||||||||||||||||
| 109 | 68 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 110 | 68 | my $self = $orig_new->($class); | |||||||||||||||||||||||||||
| 111 | 68 | for my $k (keys %args) { | |||||||||||||||||||||||||||
| 112 | 78 | ▶ | my $setter = $SETTERS{$k} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | or Carp::croak("unknown option: $k"); | ||||||||||||||||||||||||||||
| 114 | 78 | $self->$setter($args{$k}); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 59 | $self; | |||||||||||||||||||||||||||
| 117 | 27 | }; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # Doc overloading: stringify to JSON, boolean always true, eq/ne deep compare | ||||||||||||||||||||||||||||
| 121 | package JSON::YY::Doc; | ||||||||||||||||||||||||||||
| 122 | 27 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # _doc_eq only understands Docs, so compare the JSON text when the other side | ||||||||||||||||||||||||||||
| 125 | # is not one -- otherwise $doc eq '{"a":1}' is false while "$doc" eq it is true. | ||||||||||||||||||||||||||||
| 126 | sub _eq { | ||||||||||||||||||||||||||||
| 127 | 17 | ▶ | my ($self, $other) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_eq: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 128 | 17 | ▶ | return JSON::YY::_doc_eq($self, $other) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($other) and $other->isa("JSON::YY::Doc")
MC/DC50%1/2
builtin::blessed($other) and $other->isa("JSON::YY::Doc")
builtin::blessed($other)
$other->isa("JSON::YY::Doc")
Truth table66%2/3
A and B
A= builtin::blessed($other)B= $other->isa("JSON::YY::Doc")
| |||||||||||||||||||||||||||||
| 129 | if Scalar::Util::blessed($other) && $other->isa('JSON::YY::Doc'); | ||||||||||||||||||||||||||||
| 130 | # _doc_stringify returns UTF-8 bytes. The UTF8 flag cannot tell a character | ||||||||||||||||||||||||||||
| 131 | # string from a byte string (chars < 256 may be stored either way), so | ||||||||||||||||||||||||||||
| 132 | # accept the operand under either reading. | ||||||||||||||||||||||||||||
| 133 | 12 | ▶ | my $str = defined $other ? "$other" : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 12 | my $json = JSON::YY::_doc_stringify($self); | |||||||||||||||||||||||||||
| 135 | 12 | ▶ | return 1 if $json eq $str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 7 | my $enc = $str; | |||||||||||||||||||||||||||
| 137 | 7 | utf8::encode($enc); | |||||||||||||||||||||||||||
| 138 | 7 | return $json eq $enc; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | use overload | ||||||||||||||||||||||||||||
| 142 | 39 | ▶ | '""' => sub { JSON::YY::_doc_stringify($_[0]) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 143 | 2 | ▶ | 'bool' => sub { 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 144 | # without this `fallback` numifies via the string and every pair of Docs | ||||||||||||||||||||||||||||
| 145 | # compares equal; identity is the only sensible number for a handle | ||||||||||||||||||||||||||||
| 146 | 6 | ▶ | '0+' => sub { Scalar::Util::refaddr($_[0]) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 147 | 'eq' => \&_eq, | ||||||||||||||||||||||||||||
| 148 | 6 | ▶ | 'ne' => sub { !_eq(@_) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 149 | 27 | ▶ | fallback => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | package JSON::YY; | ||||||||||||||||||||||||||||
| 152 | 27 | 1; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | __END__ | ||||||||||||||||||||||||||||