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
|
|
93154
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
57
|
|
6
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
43
|
|
7
|
2
|
|
|
2
|
|
39
|
use 5.010; |
|
2
|
|
|
|
|
7
|
|
8
|
2
|
|
|
2
|
|
639
|
use JSON qw(to_json from_json encode_json decode_json); |
|
2
|
|
|
|
|
7841
|
|
|
2
|
|
|
|
|
14
|
|
9
|
2
|
|
|
2
|
|
1513
|
use JSON::Streaming::Reader; |
|
2
|
|
|
|
|
32680
|
|
|
2
|
|
|
|
|
66
|
|
10
|
2
|
|
|
2
|
|
931
|
use MARC::Record::Generic; |
|
2
|
|
|
|
|
16034
|
|
|
2
|
|
|
|
|
60
|
|
11
|
2
|
|
|
2
|
|
13
|
use MARC::Record; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
69
|
|
12
|
2
|
|
|
2
|
|
900
|
use MARC::File; |
|
2
|
|
|
|
|
1590
|
|
|
2
|
|
|
|
|
65
|
|
13
|
|
|
|
|
|
|
|
14
|
2
|
|
|
2
|
|
14
|
use vars qw( @ISA $VERSION ); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
729
|
|
15
|
|
|
|
|
|
|
$VERSION = '0.004'; |
16
|
|
|
|
|
|
|
push @ISA, 'MARC::File'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# MARC::Record -> JSON |
19
|
|
|
|
|
|
|
sub encode { |
20
|
1
|
|
|
1
|
0
|
5
|
my ($record, $args) = @_; |
21
|
1
|
|
|
|
|
2
|
my $json; |
22
|
1
|
50
|
|
|
|
5
|
if ( defined $args ) { |
23
|
0
|
|
|
|
|
0
|
$json = to_json( $record->as_generic, $args ); |
24
|
|
|
|
|
|
|
} else { |
25
|
1
|
|
|
|
|
6
|
$json = encode_json( $record->as_generic); |
26
|
1
|
|
|
|
|
1119
|
utf8::upgrade($json); |
27
|
|
|
|
|
|
|
} |
28
|
1
|
|
|
|
|
5
|
return $json; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# JSON -> MARC::Record |
32
|
|
|
|
|
|
|
sub decode { |
33
|
12
|
|
|
12
|
1
|
705599
|
my ($self, $data, $args) = @_; |
34
|
|
|
|
|
|
|
|
35
|
12
|
50
|
|
|
|
80
|
if ( !ref($data) ) { |
36
|
0
|
0
|
|
|
|
0
|
$data = defined $args |
37
|
|
|
|
|
|
|
? from_json( $data, $args ) |
38
|
|
|
|
|
|
|
: decode_json( $data ); |
39
|
|
|
|
|
|
|
} |
40
|
12
|
|
|
|
|
79
|
return MARC::Record->new_from_generic( $data ); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub _next { |
44
|
12
|
|
|
12
|
|
26014
|
my $self = shift; |
45
|
|
|
|
|
|
|
my $jsonr |
46
|
12
|
|
66
|
|
|
50
|
= $self->{jsonr} //= JSON::Streaming::Reader->for_stream($self->{fh}); |
47
|
12
|
|
|
|
|
61
|
my $token = $jsonr->get_token; |
48
|
12
|
100
|
|
|
|
2059
|
if ($token->[0] eq 'start_array') { |
49
|
1
|
|
|
|
|
3
|
$token = $jsonr->get_token; |
50
|
|
|
|
|
|
|
} |
51
|
12
|
100
|
|
|
|
172
|
return ($token->[0] eq 'end_array') ? undef : $jsonr->slurp; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
### Methods injected into MARC::Record |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub MARC::Record::new_from_json { |
57
|
1
|
|
|
1
|
0
|
83
|
my ($class, $json, $args) = @_; |
58
|
1
|
|
|
|
|
6
|
return __PACKAGE__->decode( from_json($json, $args) ); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub MARC::Record::as_json { |
62
|
1
|
|
|
1
|
0
|
2173
|
my ($self, $args) = @_; |
63
|
1
|
|
|
|
|
16
|
return encode( $self, $args ); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
1; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
__END__ |