line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MARC::File::JSON; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: read/write MARC data into JSON format |
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
81538
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
60
|
|
6
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
104
|
|
7
|
2
|
|
|
2
|
|
37
|
use 5.010; |
|
2
|
|
|
|
|
7
|
|
8
|
2
|
|
|
2
|
|
619
|
use JSON qw(to_json from_json); |
|
2
|
|
|
|
|
7909
|
|
|
2
|
|
|
|
|
11
|
|
9
|
2
|
|
|
2
|
|
1329
|
use JSON::Streaming::Reader; |
|
2
|
|
|
|
|
36937
|
|
|
2
|
|
|
|
|
83
|
|
10
|
2
|
|
|
2
|
|
1076
|
use MARC::Record::Generic; |
|
2
|
|
|
|
|
16790
|
|
|
2
|
|
|
|
|
60
|
|
11
|
2
|
|
|
2
|
|
15
|
use MARC::Record; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
72
|
|
12
|
2
|
|
|
2
|
|
882
|
use MARC::File; |
|
2
|
|
|
|
|
1662
|
|
|
2
|
|
|
|
|
63
|
|
13
|
|
|
|
|
|
|
|
14
|
2
|
|
|
2
|
|
13
|
use vars qw( @ISA $VERSION ); |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
636
|
|
15
|
|
|
|
|
|
|
$VERSION = '0.005'; |
16
|
|
|
|
|
|
|
push @ISA, 'MARC::File'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# MARC::Record -> JSON |
19
|
|
|
|
|
|
|
sub encode { |
20
|
1
|
|
|
1
|
0
|
2
|
my ($record, $args) = @_; |
21
|
1
|
|
|
|
|
4
|
return to_json( $record->as_generic, $args ); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# JSON -> MARC::Record |
25
|
|
|
|
|
|
|
sub decode { |
26
|
12
|
|
|
12
|
1
|
700014
|
my ($self, $data, $args) = @_; |
27
|
|
|
|
|
|
|
|
28
|
12
|
50
|
|
|
|
88
|
if ( !ref($data) ) { |
29
|
0
|
|
|
|
|
0
|
$data = from_json( $data, $args ); |
30
|
|
|
|
|
|
|
} |
31
|
12
|
|
|
|
|
97
|
return MARC::Record->new_from_generic( $data ); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub _next { |
35
|
12
|
|
|
12
|
|
24200
|
my $self = shift; |
36
|
|
|
|
|
|
|
my $jsonr |
37
|
12
|
|
66
|
|
|
57
|
= $self->{jsonr} //= JSON::Streaming::Reader->for_stream($self->{fh}); |
38
|
12
|
|
|
|
|
60
|
my $token = $jsonr->get_token; |
39
|
12
|
100
|
|
|
|
2097
|
if ($token->[0] eq 'start_array') { |
40
|
1
|
|
|
|
|
4
|
$token = $jsonr->get_token; |
41
|
|
|
|
|
|
|
} |
42
|
12
|
100
|
|
|
|
231
|
return ($token->[0] eq 'end_array') ? undef : $jsonr->slurp; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
### Methods injected into MARC::Record |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub MARC::Record::new_from_json { |
48
|
1
|
|
|
1
|
0
|
87
|
my ($class, $json, $args) = @_; |
49
|
1
|
|
|
|
|
5
|
return __PACKAGE__->decode( from_json($json, $args) ); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub MARC::Record::as_json { |
53
|
1
|
|
|
1
|
0
|
2124
|
my ($self, $args) = @_; |
54
|
1
|
|
|
|
|
7
|
return encode( $self, $args ); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
1; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
__END__ |