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
|
#------------------------------------------------------------------------------
# File: JSON.pm
#
# Description: Read JSON files
#
# Notes: Set ExifTool MissingTagValue to "null" to ignore JSON nulls
#
# Revisions: 2017/03/13 - P. Harvey Created
#------------------------------------------------------------------------------
package Image::ExifTool::JSON;
use strict;
use vars qw($VERSION);
use Image::ExifTool qw(:DataAccess :Utils);
use Image::ExifTool::Import;
$VERSION = '1.02';
sub ProcessTag($$$$%);
%Image::ExifTool::JSON::Main = (
GROUPS => { 0 => 'JSON', 1 => 'JSON', 2 => 'Other' },
VARS => { NO_ID => 1 },
NOTES => q{
Other than a few tags in the table below, JSON tags have not been
pre-defined. However, ExifTool will read any existing tags from basic
JSON-formatted files.
},
# ON1 settings tags
ON1_SettingsData => {
RawConv => q{
require Image::ExifTool::XMP;
$val = Image::ExifTool::XMP::DecodeBase64($val);
},
SubDirectory => { TagTable => 'Image::ExifTool::PLIST::Main' },
},
ON1_SettingsMetadataCreated => { Groups => { 2 => 'Time' } },
ON1_SettingsMetadataModified => { Groups => { 2 => 'Time' } },
ON1_SettingsMetadataName => { },
ON1_SettingsMetadataPluginID => { },
ON1_SettingsMetadataTimestamp => { Groups => { 2 => 'Time' } },
ON1_SettingsMetadataUsage => { },
ON1_SettingsMetadataVisibleToUser=>{ },
);
#------------------------------------------------------------------------------
# Store a tag value
# Inputs: 0) ExifTool ref, 1) tag table, 2) tag ID, 3) value, 4) tagInfo flags
sub FoundTag($$$$%)
{
my ($et, $tagTablePtr, $tag, $val, %flags) = @_;
# special case to reformat ON1 tag names
if ($tag =~ s/^settings\w{8}-\w{4}-\w{4}-\w{4}-\w{12}(Data|Metadata.+)$/ON1_Settings$1/) {
$et->OverrideFileType('ONP','application/on1') if $$et{FILE_TYPE} eq 'JSON';
}
# avoid conflict with special table entries
$tag .= '!' if $Image::ExifTool::specialTags{$tag};
AddTagToTable($tagTablePtr, $tag, {
Name => Image::ExifTool::MakeTagName($tag),
%flags,
Temporary => 1,
}) unless $$tagTablePtr{$tag};
$et->HandleTag($tagTablePtr, $tag, $val);
}
#------------------------------------------------------------------------------
# Process a JSON tag
# Inputs: 0) ExifTool ref, 1) tag table, 2) tag ID, 3) value, 4) tagInfo flags
# - expands structures into flattened tags as required
sub ProcessTag($$$$%)
{
local $_;
my ($et, $tagTablePtr, $tag, $val, %flags) = @_;
if (ref $val eq 'HASH') {
if ($et->Options('Struct')) {
FoundTag($et, $tagTablePtr, $tag, $val, %flags, Struct => 1);
return unless $et->Options('Struct') > 1;
}
foreach (sort keys %$val) {
ProcessTag($et, $tagTablePtr, $tag . ucfirst, $$val{$_}, %flags, Flat => 1);
}
} elsif (ref $val eq 'ARRAY') {
foreach (@$val) {
ProcessTag($et, $tagTablePtr, $tag, $_, %flags, List => 1);
}
} elsif (defined $val) {
FoundTag($et, $tagTablePtr, $tag, $val, %flags);
}
}
#------------------------------------------------------------------------------
# Extract meta information from a JSON file
# Inputs: 0) ExifTool object reference, 1) dirInfo reference
# Returns: 1 on success, 0 if this wasn't a recognized JSON file
sub ProcessJSON($$)
{
local $_;
my ($et, $dirInfo) = @_;
my $raf = $$dirInfo{RAF};
my $structOpt = $et->Options('Struct');
my (%database, $key, $tag);
# read information from JSON file into database structure
my $err = Image::ExifTool::Import::ReadJSON($raf, \%database,
$et->Options('MissingTagValue'), $et->Options('Charset'));
return 0 if $err or not %database;
$et->SetFileType();
my $tagTablePtr = GetTagTable('Image::ExifTool::JSON::Main');
# remove any old tag definitions in case they change flags
foreach $key (TagTableKeys($tagTablePtr)) {
delete $$tagTablePtr{$key} if $$tagTablePtr{$key}{Temporary};
}
# extract tags from JSON database
foreach $key (sort keys %database) {
foreach $tag (sort keys %{$database{$key}}) {
my $val = $database{$key}{$tag};
# (ignore SourceFile if generated automatically by ReadJSON)
next if $tag eq 'SourceFile' and defined $val and $val eq '*';
ProcessTag($et, $tagTablePtr, $tag, $val);
}
}
return 1;
}
1; # end
__END__
=head1 NAME
Image::ExifTool::JSON - Read JSON files
=head1 SYNOPSIS
This module is used by Image::ExifTool
=head1 DESCRIPTION
This module contains definitions required by Image::ExifTool read
information from JSON files.
=head1 AUTHOR
Copyright 2003-2021, Phil Harvey (philharvey66 at gmail.com)
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=head1 SEE ALSO
L<Image::ExifTool::TagNames/JSON Tags>,
L<Image::ExifTool(3pm)|Image::ExifTool>
=cut
|