blib/lib/MARC/File/JSON.pm

filter: Statementstmt 96.0% 48 / 50 Branchbran 60.0% 6 / 10 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 14 / 14 Podpod 25.0% 1 / 4 totaltotal 85.5% 71 / 83 SCAR 19.6
CC
7
Cov
89%
decode
4.1
_next
4.0
encode
2.0
CRAP 7.1
↑ Summary
1 package MARC::File::JSON;
2  
3 # ABSTRACT: read/write MARC data into JSON format
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JSON qw(to_json from_json encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use JSON::Streaming::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use MARC::Record::Generic;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use MARC::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use MARC::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use vars qw( @ISA $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 $VERSION = '0.004';
16 2 push @ISA, 'MARC::File';
17  
18 # MARC::Record -> JSON
19 sub encode {
20 1 ▶ my ($record, $args) = @_;
Subroutine encode: called CC=2 SCAR=7.1
Pod undocumented
21 1 my $json;
22 1 ▶ if ( defined $args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args) { } 0 1 1
23 0 $json = to_json( $record->as_generic, $args );
24 } else {
25 1 $json = encode_json( $record->as_generic);
26 1 utf8::upgrade($json);
27 }
28 1 return $json;
29 }
30  
31 # JSON -> MARC::Record
32 sub decode {
33 12 ▶ my ($self, $data, $args) = @_;
Subroutine decode: called CC=3 SCAR=14.2
Pod documented
34  
35 12 ▶ if ( !ref($data) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $data) 0 12 12
36 0 ▶ $data = defined $args
Branch: 1 branch
BranchTrueFalseTotal
defined $args ? : 0 0 0
37 ? from_json( $data, $args )
38 : decode_json( $data );
39 }
40 12 return MARC::Record->new_from_generic( $data );
41 }
42  
43 sub _next {
44 12 ▶ my $self = shift;
Subroutine _next: called CC=4 SCAR=13.9
45 my $jsonr
46 12 ▶ = $self->{jsonr} //= JSON::Streaming::Reader->for_stream($self->{fh});
Condition66%2/3
$self->{"jsonr"} //= "JSON::Streaming::Reader"->for_stream($self->{"fh"})
l!l&&r!l&&!r
1110
MC/DC0%0/2
$self->{"jsonr"} //= "JSON::Streaming::Reader"->for_stream($self->{"fh"})
$self->{"jsonr"} "JSON::Streaming::Reader"->for_stream($self->{"fh"})
Truth table66%2/3
A //= B
A= $self->{"jsonr"}B= "JSON::Streaming::Reader"->for_stream($self->{"fh"})
ABresult
1X1
011
000
47 12 my $token = $jsonr->get_token;
48 12 ▶ if ($token->[0] eq 'start_array') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[0] eq "start_array") 1 11 12
49 1 $token = $jsonr->get_token;
50 }
51 12 ▶ return ($token->[0] eq 'end_array') ? undef : $jsonr->slurp;
Branch: 1 branch
BranchTrueFalseTotal
$token->[0] eq 'end_array' ? : 1 11 12
52 }
53  
54 ### Methods injected into MARC::Record
55  
56 sub MARC::Record::new_from_json {
57 1 ▶ my ($class, $json, $args) = @_;
Subroutine new_from_json: called CC=1 SCAR=0.0
Pod undocumented
58 1 return __PACKAGE__->decode( from_json($json, $args) );
59 }
60  
61 sub MARC::Record::as_json {
62 1 ▶ my ($self, $args) = @_;
Subroutine as_json: called CC=1 SCAR=0.0
Pod undocumented
63 1 return encode( $self, $args );
64 }
65  
66 2 1;
67  
68 __END__
↑ Summary