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
|
|
51752
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
75
|
|
6
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
71
|
|
7
|
2
|
|
|
2
|
|
20
|
use JSON qw(to_json from_json); |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
13
|
|
8
|
2
|
|
|
2
|
|
2434
|
use JSON::Streaming::Reader; |
|
2
|
|
|
|
|
52536
|
|
|
2
|
|
|
|
|
74
|
|
9
|
2
|
|
|
2
|
|
2110
|
use MARC::Record::Generic; |
|
2
|
|
|
|
|
22574
|
|
|
2
|
|
|
|
|
69
|
|
10
|
2
|
|
|
2
|
|
23
|
use MARC::Record; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
72
|
|
11
|
2
|
|
|
2
|
|
2025
|
use MARC::File; |
|
2
|
|
|
|
|
1876
|
|
|
2
|
|
|
|
|
66
|
|
12
|
|
|
|
|
|
|
|
13
|
2
|
|
|
2
|
|
10
|
use vars qw( @ISA $VERSION ); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
6563
|
|
14
|
|
|
|
|
|
|
$VERSION = '0.003'; |
15
|
|
|
|
|
|
|
push @ISA, 'MARC::File'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# MARC::Record -> JSON |
18
|
|
|
|
|
|
|
sub encode { |
19
|
1
|
|
|
1
|
0
|
3
|
my ($record, $args) = @_; |
20
|
1
|
|
|
|
|
6
|
return to_json( $record->as_generic, $args ); |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# JSON -> MARC::Record |
24
|
|
|
|
|
|
|
sub decode { |
25
|
12
|
|
|
12
|
1
|
594677
|
my ($self, $data, $args) = @_; |
26
|
|
|
|
|
|
|
|
27
|
12
|
50
|
|
|
|
63
|
if ( !ref($data) ) { |
28
|
0
|
|
|
|
|
0
|
$data = from_json( $data, $args ); |
29
|
|
|
|
|
|
|
} |
30
|
12
|
|
|
|
|
96
|
return MARC::Record->new_from_generic( $data ); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub _next { |
34
|
12
|
|
|
12
|
|
22564
|
my $self = shift; |
35
|
12
|
|
66
|
|
|
72
|
my $jsonr |
36
|
|
|
|
|
|
|
= $self->{jsonr} //= JSON::Streaming::Reader->for_stream($self->{fh}); |
37
|
12
|
|
|
|
|
52
|
my $token = $jsonr->get_token; |
38
|
12
|
100
|
|
|
|
1615
|
if ($token->[0] eq 'start_array') { |
39
|
1
|
|
|
|
|
4
|
$token = $jsonr->get_token; |
40
|
|
|
|
|
|
|
} |
41
|
12
|
100
|
|
|
|
144
|
return ($token->[0] eq 'end_array') ? undef : $jsonr->slurp; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
### Methods injected into MARC::Record |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub MARC::Record::new_from_json { |
47
|
1
|
|
|
1
|
0
|
14
|
my ($class, $json, $args) = @_; |
48
|
1
|
|
|
|
|
5
|
return __PACKAGE__->decode( from_json($json, $args) ); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub MARC::Record::as_json { |
52
|
1
|
|
|
1
|
0
|
2525
|
my ($self, $args) = @_; |
53
|
1
|
|
|
|
|
6
|
return encode( $self, $args ); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
1; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
__END__ |