File: MiJ.pm

package info (click to toggle)
libmarc-file-mij-perl 0.04-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 164 kB
  • sloc: perl: 230; makefile: 2
file content (326 lines) | stat: -rw-r--r-- 7,291 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package MARC::Record::MiJ;
use ex::monkeypatched;
use JSON;
use 5.006;
use strict;
use warnings FATAL => 'all';

=head1 NAME

MARC::Record::MiJ - Convert MARC::Record to/from marc-in-json structure

=head1 VERSION

Version 0.04

=cut

our $VERSION = '0.04';

=head1 SYNOPSIS

  use MARC::Record;
  use MARC::Record::MIJ

  my $str = get_marc_in_json_from_somewhere;

  # The most common use will be to use methods monkeypatched into MARC::Record
  my $r = MARC::Record->new_from_mij($str);
  my $json = $r->to_mij;

  # You can also work with the underlying hash/array structure if you're dealing with
  # json serialization/deserialization on your own

  my $mij_structure = $r->to_mij_structure;
  my $r = MARC::Record->new_from_mij_structure($mij_structure);

  # You can also call things on MARC::Record::MiJ

  my $r = MARC::Record::MiJ->new($str);
  my $json = MARC::Record::MiJ->to_mij($r);
  my $mij_structure = MARC::Record::MiJ->to_mij_structure($r);
  my $r = MARC::Record::MiJ->new_from_mij_structure($mij_structure);

=head1 DESCRIPTION

Reads and writes MARC-in-JSON structures and strings as supported by pymarc/ruby-marc/marc4j and
described at http://dilettantes.code4lib.org/blog/2010/09/a-proposal-to-serialize-marc-in-json/

Don't confuse with another (incompatible) JSON encoding in the module C<MARC::File::JSON>, which
to the best of my knowledge isn't supported by other readers/writers.

For reading, you probably don't need to use this directly; take a look at C<MARC::File::MiJ> for reading in 
newline-delimited marc-in-json files by itself or in conjunction with C<MARC::Batch>.

The MARC::Record distribution doesn't so much do do writing out files. You can do something like this:

    # convert file from marc binary to marc-in-json
    use MARC::Batch;
    use MARC::Record::MiJ;
    my $batch = MARC::Batch->new('USMARC', 'file.mrc');
    open(my $jsonfile, '>', 'file.ndj' );
    while (my $r = $batch->next) {
      print $jsonfile MARC::Record::MiJ->to_mij($r), "\n";
    }
    close $jsonfile;

...to produce newline-delimited marc-in-json from a binary file.


=head1 SUBROUTINES/METHODS

=head2 json

Get a json object to work with (memoized). We want to control it so we make sure 
it's not doing anything pretty (like, say, putting newlines in, which woudl make it
harder to produce newline-delimited json file).

=cut

my $json;
sub json {
  return $json if ($json);
  my $self = shift;
  $json =  JSON->new->utf8;
  $json->pretty(0);
  return $json;
}


=head2 new($str)

Take a JSON string and turn it into a MARC::Record object

=cut

sub new {
  my $self = shift;
  my $str = shift;
  return $self->new_from_mij_structure($self->json->decode($str));
}

=head2 to_mij($r)

Take a record; return a JSON string

=cut

sub to_mij {
  my $self = shift;
  my $r = shift;
  return $self->json->encode($self->to_mij_structure($r));
}



=head2 MARC::Record::JSON->to_mij_structure($r)

Turn a record into a marc-in-json compatible hash; return the hash pointer

=cut

sub to_mij_structure {
  my $class = shift;
  my $r = shift;
  
  my $h = {};
  my @fields;
  $h->{leader} = $r->leader;
  
  foreach my $f ($r->fields) {
    if ($f->is_control_field) {
      push @fields, controlfield_to_mij_structure($f);
    } else {
      push @fields, valuefield_to_mij_structure($f);
    }
  }
  $h->{fields} = \@fields;
  return $h;
}


=head2 controlfield_to_mij_structure($field)

Turn a MARC::Record controlfield into an appropriate hash

=cut

sub controlfield_to_mij_structure {
  my $cf = shift;
  return {$cf->tag => $cf->data };
}

=head2 valuefield_to_mij_structure($field)

Turn a MARC::Record valuefield into an appropriate hash


=cut

sub valuefield_to_mij_structure {
  my $vf = shift;
  my @subfields;
  my $h = {ind1=>$vf->indicator(1), ind2=>$vf->indicator(2)};
  foreach my $sf ($vf->subfields) {
    push @subfields, subfield_to_mij_structure($sf);
  }
  $h->{subfields} = \@subfields;
  return {$vf->tag => $h};
  
}

=head2 subfield_to_mij_structure($sf) 

Turn a MARC::Record subfield pair (arrayref duple of code/value) into an appropriate hash


=cut

sub subfield_to_mij_structure {
  my $sf = shift;
  return {$sf->[0]=> $sf->[1]};
}


=head2 my $r =  MARC::Record::JSON->new_from_mij_structure($mij_structure)

Given a marc-in-json structure, return a MARC::Record object

=cut

sub new_from_mij_structure {
  my $self = shift;
  my $h = shift;
  
  my $r = new MARC::Record;
  
  $r->leader($h->{leader});
  
  my @fields;
  foreach my $f (@{$h->{fields}}) {
    push @fields, new_field_from_mij_structure($f);
  }
  $r->append_fields(@fields);
  return $r; 
}

=head2 new_field_from_mij_structure($f)

Given a field structure, create an appropriate (control or variable) field

=cut

sub new_field_from_mij_structure {
  my $mijf = shift;
  my ($tag, $h);


  while (my ($k, $v) = each(%$mijf)) {
    $tag = $k;
    $h = $v;
  }
  
  if (ref($h)) { # if it's a hashref
    return new_datafield_from_mij_structure($tag, $h);
  } else { # create and return a control field
    return MARC::Field->new($tag, $h);
  }

}

=head2 new_datafield_from_mij_structure

Support for new_field_from_mij_structure; do the more complex work
of creating a datafield

=cut

sub new_datafield_from_mij_structure {
  my ($tag, $h) = @_;
  my @subfields;
  foreach my $sf (@{$h->{subfields}}) {
    while (my ($code, $data) = each %$sf) {
      push @subfields, $code, $data
    }
  }
  return MARC::Field->new($tag, $h->{ind1}, $h->{ind2}, @subfields);

}

=head1 Monkeypatching MARC::Record

Add C<new_from_mij_structure($mij_structure)> and C<to_mij_structure()> to MARC::Record

  my $r = MARC::Record->new_from_mij_structure($mij_structure);
  $mij_structure = $r->to_mij_structure;

=cut

ex::monkeypatched->inject('MARC::Record' =>
  new_from_mij_structure => sub { my $class = shift; my $mij = shift; return 
    MARC::Record::MiJ->new_from_mij_structure($mij)},
  to_mij_structure => sub { my $self = shift; return  MARC::Record::MiJ->to_mij_structure($self) },
  new_from_mij => sub { my $class = shift; my $mij = shift; return MARC::Record::MiJ->new($mij) },
  to_mij => sub { my $self = shift; return MARC::Record::MiJ->to_mij($self) }
  
);



=head1 AUTHOR

Bill Dueber, C<< <dueberb at umich.edu> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-MARC-File-MiJ at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MARC-File-MiJ>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.




=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc MARC::Record::MiJ


You can also look for information at:

=over 4

=item * RT: CPAN's request tracker (report bugs here)

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MARC-File-MiJ>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/MARC-File-MiJ>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/MARC-File-MiJ>

=item * Search CPAN

L<http://search.cpan.org/dist/MARC-File-MiJ/>

=back


=head1 ACKNOWLEDGEMENTS


=head1 LICENSE AND COPYRIGHT

Copyright 2013 Bill Dueber.

This software is free software and may be distributed under the same
terms as Perl itself.


=cut

1; # End of MARC::Record::MiJ