File Coverage

blib/lib/Data/iRealPro/Output/JSON.pm
Criterion Covered Total %
statement 41 47 87.2
branch 3 12 25.0
condition 2 5 40.0
subroutine 8 8 100.0
pod 0 1 0.0
total 54 73 73.9


line stmt bran cond sub pod time code
1             #! perl
2              
3             # Data::iRealPro::Output::JSON -- parse iRealPro data and produce JSON
4              
5             # Author : Johan Vromans
6             # Created On : Fri Jan 15 19:15:00 2016
7             # Last Modified By: Johan Vromans
8             # Last Modified On: Thu Nov 1 21:09:11 2018
9             # Update Count : 1102
10             # Status : Unknown, Use with caution!
11              
12             ################ Common stuff ################
13              
14 2     2   2061 use strict;
  2         6  
  2         67  
15 2     2   10 use warnings;
  2         4  
  2         52  
16 2     2   10 use Carp;
  2         5  
  2         90  
17 2     2   551 use utf8;
  2         17  
  2         11  
18              
19             package Data::iRealPro::Output::JSON;
20              
21 2     2   93 use parent qw( Data::iRealPro::Output::Base );
  2         4  
  2         12  
22              
23 2     2   1493 use JSON::PP;
  2         31195  
  2         962  
24              
25             sub process {
26 1     1 0 927 my ( $self, $u, $options ) = @_;
27              
28 1   50     10 $self->{output} ||= $options->{output} || "__new__.json";
      33        
29              
30 1         8 my $json = JSON::PP->new->utf8(1)->pretty->indent->canonical;
31 1         187 $json->allow_blessed->convert_blessed;
32             *UNIVERSAL::TO_JSON = sub {
33 3     3   275 my $obj = "".$_[0];
34             return $obj =~ /=HASH\(/
35 3         18 ? { %{$_[0]} }
36             : $obj =~ /=ARRAY\(/
37 3 0       12 ? [ @{$_[0]} ]
  0 50       0  
38             : undef
39             ;
40 1         37 };
41              
42             # Process the song(s).
43 1         8 my @goners = qw( variant debug a2 data raw_tokens cells
44             transpose _transpose dataxp );
45 1         4 for my $item ( $u, $u->{playlist} ) {
46 2         12 delete( $item->{$_} ) for @goners;
47             }
48 1         3 my $songix;
49 1         2 foreach my $song ( @{ $u->{playlist}->{songs} } ) {
  1         4  
50 1         3 $songix++;
51             warn( sprintf("Song %3d: %s\n", $songix, $song->{title}) )
52 1 50       4 if $self->{verbose};
53 1         2 { local $song->{_transpose} = 0;
  1         3  
54 1         20 $song->tokens; }
55 1         10 delete( $song->{$_} ) for @goners;
56             }
57 1 50       5 if ( ref( $self->{output} ) ) {
    0          
58 1         7 ${ $self->{output} } = $json->encode($u);
  1         3607  
59             }
60             elsif ( $self->{output} eq "-" ) {
61 0           binmode( STDOUT, ':utf8' );
62 0           print( $json->encode($u) );
63             }
64             else {
65             open( my $fd, ">:utf8", $self->{output} )
66 0 0         or die( "Cannot create ", $self->{output}, " [$!]\n" );
67 0           $fd->print( $json->encode($u) );
68 0           $fd->close;
69             }
70             }
71              
72             1;