Image/ExifTool/JSON.pm

filter: Statementstmt 98.6% 71 / 72 Branchbran 70.0% 28 / 40 Conditioncond 39.3% 13 / 33 MC/DCmcdc 5.8% 1 / 17 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 3 totaltotal 69.7% 120 / 172 CC 37 SCAR 31.9
CC
37
Cov
77%
ProcessJSON
37.7
ProcessTag
10.6
FoundTag
5.3
CRAP 24.2
« Image/ExifTool/TagLookup.pm ↑ Summary Image/ExifTool/AES.pm »
1 #------------------------------------------------------------------------------
2 # File: JSON.pm
3 #
4 # Description: Read JSON files
5 #
6 # Notes: Set ExifTool MissingTagValue to "null" to ignore JSON nulls
7 #
8 # Revisions: 2017/03/13 - P. Harvey Created
9 #------------------------------------------------------------------------------
10  
11 package Image::ExifTool::JSON;
12 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 16 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 16 ▶use Image::ExifTool qw(:DataAccess :Utils);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 16 ▶use Image::ExifTool::Import;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 16 $VERSION = '1.11';
18  
19 sub ProcessJSON($$;$);
20 sub ProcessTag($$$$%);
21  
22 16 %Image::ExifTool::JSON::Main = (
23 GROUPS => { 0 => 'JSON', 1 => 'JSON', 2 => 'Other' },
24 VARS => { ID_FMT => 'none' },
25 PROCESS_PROC => \&ProcessJSON,
26 NOTES => q{
27 Other than a few tags in the table below, JSON tags have not been
28 pre-defined. However, ExifTool will read any existing tags from basic
29 JSON-formatted files.
30 },
31 # ON1 settings tags
32 ON1_SettingsData => {
33 RawConv => q{
34 require Image::ExifTool::XMP;
35 $val = Image::ExifTool::XMP::DecodeBase64($val);
36 },
37 SubDirectory => { TagTable => 'Image::ExifTool::PLIST::Main' },
38 },
39 ON1_SettingsMetadataCreated => { Groups => { 2 => 'Time' } },
40 ON1_SettingsMetadataModified => { Groups => { 2 => 'Time' } },
41 ON1_SettingsMetadataName => { },
42 ON1_SettingsMetadataPluginID => { },
43 ON1_SettingsMetadataTimestamp => { Groups => { 2 => 'Time' } },
44 ON1_SettingsMetadataUsage => { },
45 ON1_SettingsMetadataVisibleToUser=>{ },
46 adjustmentsSettingsStatisticsLightMap => { # (in JSON of AAE files)
47 Name => 'AdjustmentsSettingsStatisticsLightMap',
48 ValueConv => 'Image::ExifTool::XMP::DecodeBase64($val)',
49 },
50 );
51  
52 #------------------------------------------------------------------------------
53 # Store a tag value
54 # Inputs: 0) ExifTool ref, 1) tag table, 2) tag ID, 3) value, 4) tagInfo flags
55 sub FoundTag($$$$%)
56 77 ▶{
Subroutine FoundTag: called CC=5 SCAR=16.7
Pod undocumented
57 77 my ($et, $tagTablePtr, $tag, $val, %flags) = @_;
58  
59 # special case to reformat ON1 tag names
60 77 ▶ if ($tag =~ s/^settings\w{8}-\w{4}-\w{4}-\w{4}-\w{12}(Data|Metadata.+)$/ON1_Settings$1/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ s/^settings\w{8}-\w{4}-\w{4}-\w{4}-\w{12}(Data|Metadata.+)$/ON1_Settings$1/) 0 77 77
61 0 ▶ $et->OverrideFileType('ONP','application/on1') if $$et{FILE_TYPE} eq 'JSON';
Branch: 1 branch
BranchTrueFalseTotal
if $et->{"FILE_TYPE"} eq "JSON" 0 0 0
62 }
63  
64 # avoid conflict with special table entries
65 77 ▶ $tag .= '!' if $Image::ExifTool::specialTags{$tag};
Branch: 1 branch
BranchTrueFalseTotal
if $Image::ExifTool::specialTags{$tag} 0 77 77
66  
67 77 ▶ unless ($$tagTablePtr{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tagTablePtr->{$tag}) 68 9 77
68 68 my $name = $tag;
69 68 $name =~ tr/:/_/; # use underlines in place of colons in tag name
70 68 $name =~ s/^c2pa/C2PA/i; # hack to fix "C2PA" case
71 68 $name = Image::ExifTool::MakeTagName($name);
72 68 my $desc = Image::ExifTool::MakeDescription($name);
73 68 $desc =~ s/^C2 PA/C2PA/; # hack to get "C2PA" correct
74 68 $et->VPrint(0, $$et{INDENT}, "[adding $tag]\n");
75 68 AddTagToTable($tagTablePtr, $tag, {
76 Name => $name,
77 Description => $desc,
78 %flags,
79 Temporary => 1,
80 });
81 }
82 77 $et->HandleTag($tagTablePtr, $tag, $val);
83 }
84  
85 #------------------------------------------------------------------------------
86 # Process a JSON tag
87 # Inputs: 0) ExifTool ref, 1) tag table, 2) tag ID, 3) value, 4) tagInfo flags
88 # - expands structures into flattened tags as required
89 sub ProcessTag($$$$%)
90 88 ▶{
Subroutine ProcessTag: called CC=10 SCAR=23.6
Pod undocumented
91 88 local $_;
92 88 my ($et, $tagTablePtr, $tag, $val, %flags) = @_;
93  
94 88 ▶ if (ref $val eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $val eq 'HASH') { } 2 86 88
elsif (ref $val eq 'ARRAY') { } 8 78 86
elsif (defined $val) { } 75 3 78
95 2 ▶ if ($et->Options('Struct')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($et->Options("Struct")) 2 0 2
96 2 FoundTag($et, $tagTablePtr, $tag, $val, %flags, Struct => 1);
97 2 ▶ return unless $et->Options('Struct') > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $et->Options("Struct") > 1 0 2 2
98 }
99 # support hashes with ordered keys
100 2 foreach (Image::ExifTool::OrderedKeys($val)) {
101 4 ▶ my $tg = $tag . ((/^\d/ and $tag =~ /\d$/) ? '_' : '') . ucfirst;
Branch: 1 branch
BranchTrueFalseTotal
/^\d/ && $tag =~ /\d$/ ? : 0 4 4
Condition33%1/3
/^\d/ and $tag =~ /\d$/
!ll&&!rl&&r
400
MC/DC0%0/2
/^\d/ and $tag =~ /\d$/
/^\d/ $tag =~ /\d$/
Truth table33%1/3
A and B
A= /^\d/B= $tag =~ /\d$/
ABresult
0X0
100
111
102 4 $tg =~ s/([^a-zA-Z])([a-z])/$1\U$2/g;
103 4 ProcessTag($et, $tagTablePtr, $tg, $$val{$_}, %flags, Flat => 1);
104 }
105 } elsif (ref $val eq 'ARRAY') {
106 8 foreach (@$val) {
107 13 ProcessTag($et, $tagTablePtr, $tag, $_, %flags, List => 1);
108 }
109 } elsif (defined $val) {
110 75 FoundTag($et, $tagTablePtr, $tag, $val, %flags);
111 }
112 }
113  
114 #------------------------------------------------------------------------------
115 # Extract meta information from a JSON file
116 # Inputs: 0) ExifTool object reference, 1) dirInfo reference, 2) tag table ref
117 # Returns: 1 on success, 0 if this wasn't a recognized JSON file
118 sub ProcessJSON($$;$)
119 61 ▶{
Subroutine ProcessJSON: called CC=24 SCAR=36.3
Pod undocumented
120 61 local $_;
121 61 my ($et, $dirInfo, $tagTablePtr) = @_;
122 61 my $raf = $$dirInfo{RAF};
123 61 my $structOpt = $et->Options('Struct');
124 61 my (%database, $key, $tag, $dataPt);
125  
126 61 ▶ unless ($raf) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($raf) 60 1 61
127 60 $dataPt = $$dirInfo{DataPt};
128 60 ▶ if ($$dirInfo{DirStart} or ($$dirInfo{DirLen} and $$dirInfo{DirLen} ne length($$dataPt))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dirInfo->{"DirStart"} or $dirInfo->{"DirLen"} and $dirInfo->{"DirLen"} ne length $$dataPt) 60 0 60
Condition16%1/6
$dirInfo->{"DirStart"} or $dirInfo->{"DirLen"} and $dirInfo->{"DirLen"} ne length $$dataPt
l!l&&r!l&&!r
6000
$dirInfo->{"DirLen"} and $dirInfo->{"DirLen"} ne length $$dataPt
!ll&&!rl&&r
000
MC/DC0%0/3
$dirInfo->{"DirStart"} or $dirInfo->{"DirLen"} and $dirInfo->{"DirLen"} ne length $$dataPt
$dirInfo->{"DirStart"} $dirInfo->{"DirLen"} $dirInfo->{"DirLen"} ne length $$dataPt
Truth table25%1/4
A or B and C
A= $dirInfo->{"DirStart"}B= $dirInfo->{"DirLen"}C= $dirInfo->{"DirLen"} ne length $$dataPt
ABCresult
1XX1
0111
00X0
0100
129 60 my $buff = substr(${$$dirInfo{DataPt}}, $$dirInfo{DirStart}, $$dirInfo{DirLen});
130 60 $dataPt = \$buff;
131 }
132 60 $raf = File::RandomAccess->new($dataPt);
133 # extract as a block if requested
134 60 ▶ my $blockName = $$dirInfo{BlockInfo} ? $$dirInfo{BlockInfo}{Name} : '';
Branch: 1 branch
BranchTrueFalseTotal
$dirInfo->{'BlockInfo'} ? : 60 0 60
135 60 my $blockExtract = $et->Options('BlockExtract');
136 60 ▶ if ($blockName and ($blockExtract or $$et{REQ_TAG_LOOKUP}{lc $blockName} or
Branch: 1 branch
BranchTrueFalseTotal
if ($blockName and $blockExtract || $et->{"REQ_TAG_LOOKUP"}{lc $blockName} || $et->{"TAGS_FROM_FILE"} && !$et->{"EXCL_TAG_LOOKUP"}{lc $blockName}) 15 45 60
Condition58%7/12
$blockName and $blockExtract || $et->{"REQ_TAG_LOOKUP"}{lc $blockName} || $et->{"TAGS_FROM_FILE"} && !$et->{"EXCL_TAG_LOOKUP"}{lc $blockName}
!ll&&!rl&&r
04515
$blockExtract or $et->{"REQ_TAG_LOOKUP"}{lc $blockName} or $et->{"TAGS_FROM_FILE"} and not $et->{"EXCL_TAG_LOOKUP"}{lc $blockName}
l!l&&r!l&&!r
01545
$blockExtract or $et->{"REQ_TAG_LOOKUP"}{lc $blockName}
l!l&&r!l&&!r
0060
$et->{"TAGS_FROM_FILE"} and not $et->{"EXCL_TAG_LOOKUP"}{lc $blockName}
!ll&&!rl&&r
45015
MC/DC20%1/5
$blockName and $blockExtract || $et->{"REQ_TAG_LOOKUP"}{lc $blockName} || $et->{"TAGS_FROM_FILE"} && !$et->{"EXCL_TAG_LOOKUP"}{lc $blockName}
$blockName $blockExtract $et->{"REQ_TAG_LOOKUP"}{lc $blockName} $et->{"TAGS_FROM_FILE"} not $et->{"EXCL_TAG_LOOKUP"}{lc $blockName}
Truth table33%2/6
A and B or C or D and not(E)
A= $blockNameB= $blockExtractC= $et->{"REQ_TAG_LOOKUP"}{lc $blockName}D= $et->{"TAGS_FROM_FILE"}E= not $et->{"EXCL_TAG_LOOKUP"}{lc $blockName}
ABCDEresult
0XXXX0
1000X0
100100
11XXX1
101XX1
100111
137 ($$et{TAGS_FROM_FILE} and not $$et{EXCL_TAG_LOOKUP}{lc $blockName})))
138 {
139 15 $et->FoundTag($$dirInfo{BlockInfo}, $$dataPt);
140 15 ▶ return 1 if $blockExtract and $blockExtract > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $blockExtract and $blockExtract > 1 0 15 15
Condition33%1/3
$blockExtract and $blockExtract > 1
!ll&&!rl&&r
1500
MC/DC0%0/2
$blockExtract and $blockExtract > 1
$blockExtract $blockExtract > 1
Truth table33%1/3
A and B
A= $blockExtractB= $blockExtract > 1
ABresult
0X0
100
111
141 }
142 60 $et->VerboseDir('JSON');
143 }
144  
145 # read information from JSON file into database structure
146 61 my $err = Image::ExifTool::Import::ReadJSON($raf, \%database,
147 $et->Options('MissingTagValue'), $et->Options('Charset'));
148  
149 61 ▶ return 0 if $err or not %database;
Branch: 1 branch
BranchTrueFalseTotal
if $err or not %database 0 61 61
Condition33%1/3
$err or not %database
l!l&&r!l&&!r
0061
MC/DC0%0/2
$err or not %database
$err not %database
Truth table33%1/3
A or not(B)
A= $errB= not %database
ABresult
1X1
011
000
150  
151 61 ▶ $et->SetFileType() unless $dataPt;
Branch: 1 branch
BranchTrueFalseTotal
unless $dataPt 1 60 61
152  
153 61 ▶ $tagTablePtr or $tagTablePtr = GetTagTable('Image::ExifTool::JSON::Main');
Branch: 1 branch
BranchTrueFalseTotal
$tagTablePtr or $tagTablePtr = &GetTagTable("Image::ExifTool::JSON::Main") 60 1 61
154  
155 # remove any old tag definitions in case they change flags
156 61 foreach $key (TagTableKeys($tagTablePtr)) {
157 604 ▶ delete $$tagTablePtr{$key} if $$tagTablePtr{$key}{Temporary};
Branch: 1 branch
BranchTrueFalseTotal
if $tagTablePtr->{$key}{"Temporary"} 55 549 604
158 }
159  
160 # extract tags from JSON database
161 61 foreach $key (sort keys %database) {
162 61 foreach $tag (Image::ExifTool::OrderedKeys($database{$key})) {
163 71 my $val = $database{$key}{$tag};
164 # (ignore SourceFile if generated automatically by ReadJSON)
165 71 ▶ next if $tag eq 'SourceFile' and defined $val and $val eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "SourceFile" and defined $val and $val eq "*" 0 71 71
Condition33%2/6
$tag eq "SourceFile" and defined $val and $val eq "*"
!ll&&!rl&&r
7100
$tag eq "SourceFile" and defined $val
!ll&&!rl&&r
7100
MC/DC0%0/3
$tag eq "SourceFile" and defined $val and $val eq "*"
$tag eq "SourceFile" defined $val $val eq "*"
Truth table25%1/4
A and B and C
A= $tag eq "SourceFile"B= defined $valC= $val eq "*"
ABCresult
0XX0
10X0
1100
1111
166 71 ProcessTag($et, $tagTablePtr, $tag, $val);
167 }
168 }
169 61 return 1;
170 }
171  
172 16 1; # end
173  
174 __END__
« Image/ExifTool/TagLookup.pm ↑ Summary Image/ExifTool/AES.pm »