Debian Codegraph @

lib/MARC/Record/MiJ.pm

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 package MARC::Record::MiJscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/;
2 use ex::monkeypatchedscip-perl cpan MARC-File-MiJ 0.04 ex/monkeypatched/;
3 use JSONscip-perl cpan MARC-File-MiJ 0.04 JSON/;
4 use 5.006scip-perl cpan MARC-File-MiJ 0.04 `5.006`/;
5 use strictscip-perl cpan MARC-File-MiJ 0.04 strict/;
6 use warningsscip-perl cpan MARC-File-MiJ 0.04 warnings/ FATAL => 'all';
7
8 =head1 NAME
9
10 MARC::Record::MiJ - Convert MARC::Record to/from marc-in-json structure
11
12 =head1 VERSION
13
14 Version 0.04
15
16 =cut
17
18 our $VERSIONscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/$VERSION. = '0.04';
19
20 =head1 SYNOPSIS
21
22 use MARC::Record;
23 use MARC::Record::MIJ
24
25 my $str = get_marc_in_json_from_somewhere;
26
27 # The most common use will be to use methods monkeypatched into MARC::Record
28 my $r = MARC::Record->new_from_mij($str);
29 my $json = $r->to_mij;
30
31 # You can also work with the underlying hash/array structure if you're dealing with
32 # json serialization/deserialization on your own
33
34 my $mij_structure = $r->to_mij_structure;
35 my $r = MARC::Record->new_from_mij_structure($mij_structure);
36
37 # You can also call things on MARC::Record::MiJ
38
39 my $r = MARC::Record::MiJ->new($str);
40 my $json = MARC::Record::MiJ->to_mij($r);
41 my $mij_structure = MARC::Record::MiJ->to_mij_structure($r);
42 my $r = MARC::Record::MiJ->new_from_mij_structure($mij_structure);
43
44 =head1 DESCRIPTION
45
46 Reads and writes MARC-in-JSON structures and strings as supported by pymarc/ruby-marc/marc4j and
47 described at http://dilettantes.code4lib.org/blog/2010/09/a-proposal-to-serialize-marc-in-json/
48
49 Don't confuse with another (incompatible) JSON encoding in the module C<MARC::File::JSON>, which
50 to the best of my knowledge isn't supported by other readers/writers.
51
52 For reading, you probably don't need to use this directly; take a look at C<MARC::File::MiJ> for reading in
53 newline-delimited marc-in-json files by itself or in conjunction with C<MARC::Batch>.
54
55 The MARC::Record distribution doesn't so much do do writing out files. You can do something like this:
56
57 # convert file from marc binary to marc-in-json
58 use MARC::Batch;
59 use MARC::Record::MiJ;
60 my $batch = MARC::Batch->new('USMARC', 'file.mrc');
61 open(my $jsonfile, '>', 'file.ndj' );
62 while (my $r = $batch->next) {
63 print $jsonfile MARC::Record::MiJ->to_mij($r), "\n";
64 }
65 close $jsonfile;
66
67 ...to produce newline-delimited marc-in-json from a binary file.
68
69
70 =head1 SUBROUTINES/METHODS
71
72 =head2 json
73
74 Get a json object to work with (memoized). We want to control it so we make sure
75 it's not doing anything pretty (like, say, putting newlines in, which woudl make it
76 harder to produce newline-delimited json file).
77
78 =cut
79
80 my $json;
81 sub jsonscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/json(). {
82 return $json if ($json);
83 my $self = shiftscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/shift().;
84 $json = JSON->newscip-perl cpan MARC-File-MiJ 0.04 JSON/new().->utf8scip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/utf8().;
85 $json->prettyscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/pretty().(0);
86 return $json;
87 }
88
89
90 =head2 new($str)
91
92 Take a JSON string and turn it into a MARC::Record object
93
94 =cut
95
96 sub newscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/new(). {
97 my $self = shiftscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/shift().;
98 my $str = shiftscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/shift().;
99 return $self->new_from_mij_structurescip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/new_from_mij_structure().($self->jsonscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/json().->decodescip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/decode().($str));
100 }
101
102 =head2 to_mij($r)
103
104 Take a record; return a JSON string
105
106 =cut
107
108 sub to_mijscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/to_mij(). {
109 my $self = shiftscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/shift().;
110 my $r = shiftscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/shift().;
111 return $self->jsonscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/json().->encodescip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/encode().($self->to_mij_structurescip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/to_mij_structure().($r));
112 }
113
114
115
116 =head2 MARC::Record::JSON->to_mij_structure($r)
117
118 Turn a record into a marc-in-json compatible hash; return the hash pointer
119
120 =cut
121
122 sub to_mij_structurescip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/to_mij_structure(). {
123 my $class = shiftscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/shift().;
124 my $r = shiftscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/shift().;
125
126 my $h = {};
127 my @fields;
128 $h->{leader} = $r->leaderscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/leader().;
129
130 foreach my $f ($r->fieldsscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/fields().) {
131 if ($f->is_control_fieldscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/is_control_field().) {
132 pushscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/push(). @fields, controlfield_to_mij_structurescip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/controlfield_to_mij_structure().($f);
133 } else {
134 pushscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/push(). @fields, valuefield_to_mij_structurescip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/valuefield_to_mij_structure().($f);
135 }
136 }
137 $h->{fields} = \@fields;
138 return $h;
139 }
140
141
142 =head2 controlfield_to_mij_structure($field)
143
144 Turn a MARC::Record controlfield into an appropriate hash
145
146 =cut
147
148 sub controlfield_to_mij_structurescip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/controlfield_to_mij_structure(). {
149 my $cf = shiftscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/shift().;
150 return {$cf->tagscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/tag(). => $cf->datascip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/data(). };
151 }
152
153 =head2 valuefield_to_mij_structure($field)
154
155 Turn a MARC::Record valuefield into an appropriate hash
156
157
158 =cut
159
160 sub valuefield_to_mij_structurescip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/valuefield_to_mij_structure(). {
161 my $vf = shiftscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/shift().;
162 my @subfields;
163 my $h = {ind1=>$vf->indicatorscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/indicator().(1), ind2=>$vf->indicatorscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/indicator().(2)};
164 foreach my $sf ($vf->subfieldsscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/subfields().) {
165 pushscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/push(). @subfields, subfield_to_mij_structurescip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/subfield_to_mij_structure().($sf);
166 }
167 $h->{subfields} = \@subfields;
168 return {$vf->tagscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/tag(). => $h};
169
170 }
171
172 =head2 subfield_to_mij_structure($sf)
173
174 Turn a MARC::Record subfield pair (arrayref duple of code/value) into an appropriate hash
175
176
177 =cut
178
179 sub subfield_to_mij_structurescip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/subfield_to_mij_structure(). {
180 my $sf = shiftscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/shift().;
181 return {$sf->[0]=> $sf->[1]};
182 }
183
184
185 =head2 my $r = MARC::Record::JSON->new_from_mij_structure($mij_structure)
186
187 Given a marc-in-json structure, return a MARC::Record object
188
189 =cut
190
191 sub new_from_mij_structurescip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/new_from_mij_structure(). {
192 my $self = shiftscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/shift().;
193 my $h = shiftscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/shift().;
194
195 my $r = new MARC::Record;
196
197 $r->leaderscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/leader().($h->{leader});
198
199 my @fields;
200 foreach my $f (@{$h->{fields}}) {
201 pushscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/push(). @fields, new_field_from_mij_structurescip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/new_field_from_mij_structure().($f);
202 }
203 $r->append_fieldsscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/append_fields().(@fields);
204 return $r;
205 }
206
207 =head2 new_field_from_mij_structure($f)
208
209 Given a field structure, create an appropriate (control or variable) field
210
211 =cut
212
213 sub new_field_from_mij_structurescip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/new_field_from_mij_structure(). {
214 my $mijf = shiftscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/shift().;
215 my ($tag, $h);
216
217
218 while (my ($k, $v) = eachscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/each().(%$mijf)) {
219 $tag = $k;
220 $h = $v;
221 }
222
223 if (refscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/ref().($h)) { # if it's a hashref
224 return new_datafield_from_mij_structurescip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/new_datafield_from_mij_structure().($tag, $h);
225 } else { # create and return a control field
226 return MARC::Field->newscip-perl cpan MARC-File-MiJ 0.04 MARC/Field/new().($tag, $h);
227 }
228
229 }
230
231 =head2 new_datafield_from_mij_structure
232
233 Support for new_field_from_mij_structure; do the more complex work
234 of creating a datafield
235
236 =cut
237
238 sub new_datafield_from_mij_structurescip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/new_datafield_from_mij_structure(). {
239 my ($tag, $h) = @_scip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/`@_`.;
240 my @subfields;
241 foreach my $sf (@{$h->{subfields}}) {
242 while (my ($code, $data) = eachscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/each(). %$sf) {
243 pushscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/push(). @subfields, $code, $data
244 }
245 }
246 return MARC::Field->newscip-perl cpan MARC-File-MiJ 0.04 MARC/Field/new().($tag, $h->{ind1}, $h->{ind2}, @subfields);
247
248 }
249
250 =head1 Monkeypatching MARC::Record
251
•252 Add C<new_from_mij_structure($mij_structure)> and C<to_mij_structure()> to MARC::Record
253
254 my $r = MARC::Record->new_from_mij_structure($mij_structure);
255 $mij_structure = $r->to_mij_structure;
256
257 =cut
258
259 ex::monkeypatched->injectscip-perl cpan MARC-File-MiJ 0.04 ex/monkeypatched/inject().('MARC::Record' =>
260 new_from_mij_structure => sub { my $class = shiftscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/shift().; my $mij = shiftscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/shift().; return
261 MARC::Record::MiJ->new_from_mij_structurescip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/new_from_mij_structure().($mij)},
262 to_mij_structure => sub { my $self = shiftscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/shift().; return MARC::Record::MiJ->to_mij_structurescip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/to_mij_structure().($self) },
263 new_from_mij => sub { my $class = shiftscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/shift().; my $mij = shiftscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/shift().; return MARC::Record::MiJ->newscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/new().($mij) },
264 to_mij => sub { my $self = shiftscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/shift().; return MARC::Record::MiJ->to_mijscip-perl cpan MARC-File-MiJ 0.04 MARC/Record/MiJ/to_mij().($self) }
265
266 );
267
268
269
270 =head1 AUTHOR
271
272 Bill Dueber, C<< <dueberb at umich.edu> >>
273
274 =head1 BUGS
275
276 Please report any bugs or feature requests to C<bug-MARC-File-MiJ at rt.cpan.org>, or through
277 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MARC-File-MiJ>. I will be notified, and then you'll
278 automatically be notified of progress on your bug as I make changes.
279
280
281
282
283 =head1 SUPPORT
284
285 You can find documentation for this module with the perldoc command.
286
287 perldoc MARC::Record::MiJ
288
289
290 You can also look for information at:
291
292 =over 4
293
294 =item * RT: CPAN's request tracker (report bugs here)
295
296 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MARC-File-MiJ>
297
298 =item * AnnoCPAN: Annotated CPAN documentation
299
300 L<http://annocpan.org/dist/MARC-File-MiJ>
301
302 =item * CPAN Ratings
303
304 L<http://cpanratings.perl.org/d/MARC-File-MiJ>
305
306 =item * Search CPAN
307
308 L<http://search.cpan.org/dist/MARC-File-MiJ/>
309
310 =back
311
312
313 =head1 ACKNOWLEDGEMENTS
314
315
316 =head1 LICENSE AND COPYRIGHT
317
318 Copyright 2013 Bill Dueber.
319
320 This software is free software and may be distributed under the same
321 terms as Perl itself.
322
323
324 =cut
325
326 1; # End of MARC::Record::MiJ