| 1 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | package MARC::Utils::MARC2MARC_in_JSON; | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use 5.008002; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | our (@ISA, @EXPORT_OK); | |||||||||||||||||||||||||||
| 12 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 14 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 15 | 1 | @EXPORT_OK = qw( marc2marc_in_json marc_in_json2marc each_record ); | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use MARC::Record; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use JSON; # decode_json() | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 22 | 1 | ▶ | sub marc2marc_in_json { | ||||||||||||||||||||||||||
|
Subroutine
marc2marc_in_json: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 1 | my( $marc_record ) = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | my %marc_in_json; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | for my $leader ( $marc_record->leader() ) { | |||||||||||||||||||||||||||
| 28 | 1 | $marc_in_json{'leader'} = $leader; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | for my $field ( $marc_record->fields() ) { | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 27 | my $ftag = $field->tag(); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 27 | ▶ | if( $field->is_control_field() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 4 | push @{$marc_in_json{'fields'}}, { $ftag => $field->data() }; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | else { | ||||||||||||||||||||||||||||
| 40 | 23 | my $fdata; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 23 | for my $i ( 1, 2 ) { | |||||||||||||||||||||||||||
| 43 | 46 | $fdata->{"ind$i"} = $field->indicator( $i ) | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 23 | for my $subfield ( $field->subfields ) { | |||||||||||||||||||||||||||
| 47 | 44 | push @{$fdata->{'subfields'}}, { $subfield->[0] => $subfield->[1] }; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 23 | push @{$marc_in_json{'fields'}}, { $ftag => $fdata }; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1 | \%marc_in_json; # returned | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 58 | 2 | ▶ | sub marc_in_json2marc { | ||||||||||||||||||||||||||
|
Subroutine
marc_in_json2marc: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 2 | my( $marc_in_json ) = @_; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 2 | my $marc_record = MARC::Record->new(); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 2 | for my $leader ( $marc_in_json->{'leader'} ) { | |||||||||||||||||||||||||||
| 64 | 2 | $marc_record->leader( $leader ); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 2 | for my $field ( @{$marc_in_json->{'fields'}} ) { | |||||||||||||||||||||||||||
| 68 | 54 | my( $ftag, $fdata ) = %$field; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 54 | ▶ | if( ref $fdata ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 46 | my @subfields; | |||||||||||||||||||||||||||
| 72 | 46 | for my $subfield ( @{$fdata->{'subfields'}} ) { | |||||||||||||||||||||||||||
| 73 | 88 | my( $sftag, $sfdata ) = %$subfield; | |||||||||||||||||||||||||||
| 74 | 88 | push @subfields, $sftag, $sfdata; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | $marc_record->append_fields( MARC::Field->new( | ||||||||||||||||||||||||||||
| 77 | 46 | $ftag, $fdata->{'ind1'}, $fdata->{'ind2'}, @subfields ) ); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # control field | ||||||||||||||||||||||||||||
| 81 | else { | ||||||||||||||||||||||||||||
| 82 | 8 | $marc_record->append_fields( MARC::Field->new( $ftag, $fdata ) ); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2 | $marc_record; #returned | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 90 | 10 | ▶ | sub each_record { | ||||||||||||||||||||||||||
|
Subroutine
each_record: called CC=15 SCAR=27.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 91 | 10 | my( $filename, $declared_filetype ) = @_; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 10 | ▶ | open my $fh, '<', $filename or croak "Can't open $filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # examine beginning of file to determine its type | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 10 | my $first_line = <$fh>; | |||||||||||||||||||||||||||
| 98 | 10 | my( $filetype, $recsep ); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 10 | for( $first_line ) { | |||||||||||||||||||||||||||
| 101 | 10 | ▶ | if( /^\[/ ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 102 | 4 | $filetype = 'collection'; | |||||||||||||||||||||||||||
| 103 | 4 | ▶ | if( /^\[\n$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 4 | my $second_line = <$fh>; | |||||||||||||||||||||||||||
| 105 | 4 | ▶ | if( $second_line eq "\n" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 2 | $filetype = 'collection-delimited'; | |||||||||||||||||||||||||||
| 107 | 2 | $recsep = "\n\n"; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | elsif( /^{/ ) #vi} | ||||||||||||||||||||||||||||
| 112 | { | ||||||||||||||||||||||||||||
| 113 | 2 | ▶ | if( $declared_filetype and | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$declared_filetype and $declared_filetype eq "ndj"
MC/DC50%1/2
$declared_filetype and $declared_filetype eq "ndj"
$declared_filetype
$declared_filetype eq "ndj"
Truth table66%2/3
A and B
A= $declared_filetypeB= $declared_filetype eq "ndj"
| |||||||||||||||||||||||||||||
| 114 | $declared_filetype eq 'ndj' ) { # newline delimited json | ||||||||||||||||||||||||||||
| 115 | 1 | $filetype = $declared_filetype; | |||||||||||||||||||||||||||
| 116 | 1 | $recsep = "\n"; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | else { | ||||||||||||||||||||||||||||
| 119 | 1 | $filetype = 'object'; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | else { | ||||||||||||||||||||||||||||
| 123 | 4 | $filetype = 'delimited'; | |||||||||||||||||||||||||||
| 124 | 4 | $recsep = "\n$_"; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 10 | ▶ | croak "File doesn't match file type: $filename, $declared_filetype vs. $filetype" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$declared_filetype and $declared_filetype ne $filetype
MC/DC0%0/2
$declared_filetype and $declared_filetype ne $filetype
$declared_filetype
$declared_filetype ne $filetype
Truth table66%2/3
A and B
A= $declared_filetypeB= $declared_filetype ne $filetype
| |||||||||||||||||||||||||||||
| 129 | if $declared_filetype and $declared_filetype ne $filetype; | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 10 | ▶ | if( $filetype =~ /^object|collection$/ ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 3 | seek $fh, 0, 0; # rewind to top | |||||||||||||||||||||||||||
| 134 | 3 | local $/; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 3 | my $json_items = decode_json( <$fh> ); # slurp | |||||||||||||||||||||||||||
| 137 | 3 | ▶ | $json_items = [$json_items] if $filetype eq 'object'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 3 | my $index = 0; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # "get_next" closure | ||||||||||||||||||||||||||||
| 141 | 14 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 142 | 14 | ▶ | return if $index > $#$json_items; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 11 | return $json_items->[ $index ++ ]; | |||||||||||||||||||||||||||
| 144 | 3 | }; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | elsif( $filetype =~ /delimited$/ ) { | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # "get_next" closure | ||||||||||||||||||||||||||||
| 151 | 36 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 152 | 36 | local $/ = $recsep; | |||||||||||||||||||||||||||
| 153 | 36 | my $text = <$fh>; | |||||||||||||||||||||||||||
| 154 | 36 | ▶ | return unless defined $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 32 | ▶ | return unless $text =~ /^\s*{/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 30 | chomp $text; | |||||||||||||||||||||||||||
| 157 | 30 | $text =~ s/,\s*$//; | |||||||||||||||||||||||||||
| 158 | 30 | return decode_json $text; | |||||||||||||||||||||||||||
| 159 | 6 | }; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | elsif( $filetype eq 'ndj' ) { | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 1 | seek $fh, 0, 0; # rewind to top | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | # "get_next" closure | ||||||||||||||||||||||||||||
| 168 | 6 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 169 | 6 | local $/ = $recsep; | |||||||||||||||||||||||||||
| 170 | 6 | my $text = <$fh>; | |||||||||||||||||||||||||||
| 171 | 6 | ▶ | return unless defined $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 5 | ▶ | return unless $text =~ /^\s*{/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 5 | chomp $text; | |||||||||||||||||||||||||||
| 174 | 5 | $text =~ s/,\s*$//; # just in case | |||||||||||||||||||||||||||
| 175 | 5 | return decode_json $text; | |||||||||||||||||||||||||||
| 176 | 1 | }; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | else { | ||||||||||||||||||||||||||||
| 181 | 0 | croak "Unrecognized file type: $filename"; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 1 | 1; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | __END__ | ||||||||||||||||||||||||||||