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 | use strict;
|
---|
13 | use vars qw($VERSION);
|
---|
14 | use Image::ExifTool qw(:DataAccess :Utils);
|
---|
15 | use Image::ExifTool::Import;
|
---|
16 |
|
---|
17 | $VERSION = '1.02';
|
---|
18 |
|
---|
19 | sub ProcessTag($$$$%);
|
---|
20 |
|
---|
21 | %Image::ExifTool::JSON::Main = (
|
---|
22 | GROUPS => { 0 => 'JSON', 1 => 'JSON', 2 => 'Other' },
|
---|
23 | VARS => { NO_ID => 1 },
|
---|
24 | NOTES => q{
|
---|
25 | Other than a few tags in the table below, JSON tags have not been
|
---|
26 | pre-defined. However, ExifTool will read any existing tags from basic
|
---|
27 | JSON-formatted files.
|
---|
28 | },
|
---|
29 | # ON1 settings tags
|
---|
30 | ON1_SettingsData => {
|
---|
31 | RawConv => q{
|
---|
32 | require Image::ExifTool::XMP;
|
---|
33 | $val = Image::ExifTool::XMP::DecodeBase64($val);
|
---|
34 | },
|
---|
35 | SubDirectory => { TagTable => 'Image::ExifTool::PLIST::Main' },
|
---|
36 | },
|
---|
37 | ON1_SettingsMetadataCreated => { Groups => { 2 => 'Time' } },
|
---|
38 | ON1_SettingsMetadataModified => { Groups => { 2 => 'Time' } },
|
---|
39 | ON1_SettingsMetadataName => { },
|
---|
40 | ON1_SettingsMetadataPluginID => { },
|
---|
41 | ON1_SettingsMetadataTimestamp => { Groups => { 2 => 'Time' } },
|
---|
42 | ON1_SettingsMetadataUsage => { },
|
---|
43 | ON1_SettingsMetadataVisibleToUser=>{ },
|
---|
44 | );
|
---|
45 |
|
---|
46 | #------------------------------------------------------------------------------
|
---|
47 | # Store a tag value
|
---|
48 | # Inputs: 0) ExifTool ref, 1) tag table, 2) tag ID, 3) value, 4) tagInfo flags
|
---|
49 | sub FoundTag($$$$%)
|
---|
50 | {
|
---|
51 | my ($et, $tagTablePtr, $tag, $val, %flags) = @_;
|
---|
52 |
|
---|
53 | # special case to reformat ON1 tag names
|
---|
54 | if ($tag =~ s/^settings\w{8}-\w{4}-\w{4}-\w{4}-\w{12}(Data|Metadata.+)$/ON1_Settings$1/) {
|
---|
55 | $et->OverrideFileType('ONP','application/on1') if $$et{FILE_TYPE} eq 'JSON';
|
---|
56 | }
|
---|
57 |
|
---|
58 | # avoid conflict with special table entries
|
---|
59 | $tag .= '!' if $Image::ExifTool::specialTags{$tag};
|
---|
60 |
|
---|
61 | AddTagToTable($tagTablePtr, $tag, {
|
---|
62 | Name => Image::ExifTool::MakeTagName($tag),
|
---|
63 | %flags,
|
---|
64 | Temporary => 1,
|
---|
65 | }) unless $$tagTablePtr{$tag};
|
---|
66 |
|
---|
67 | $et->HandleTag($tagTablePtr, $tag, $val);
|
---|
68 | }
|
---|
69 |
|
---|
70 | #------------------------------------------------------------------------------
|
---|
71 | # Process a JSON tag
|
---|
72 | # Inputs: 0) ExifTool ref, 1) tag table, 2) tag ID, 3) value, 4) tagInfo flags
|
---|
73 | # - expands structures into flattened tags as required
|
---|
74 | sub ProcessTag($$$$%)
|
---|
75 | {
|
---|
76 | local $_;
|
---|
77 | my ($et, $tagTablePtr, $tag, $val, %flags) = @_;
|
---|
78 |
|
---|
79 | if (ref $val eq 'HASH') {
|
---|
80 | if ($et->Options('Struct')) {
|
---|
81 | FoundTag($et, $tagTablePtr, $tag, $val, %flags, Struct => 1);
|
---|
82 | return unless $et->Options('Struct') > 1;
|
---|
83 | }
|
---|
84 | foreach (sort keys %$val) {
|
---|
85 | ProcessTag($et, $tagTablePtr, $tag . ucfirst, $$val{$_}, %flags, Flat => 1);
|
---|
86 | }
|
---|
87 | } elsif (ref $val eq 'ARRAY') {
|
---|
88 | foreach (@$val) {
|
---|
89 | ProcessTag($et, $tagTablePtr, $tag, $_, %flags, List => 1);
|
---|
90 | }
|
---|
91 | } elsif (defined $val) {
|
---|
92 | FoundTag($et, $tagTablePtr, $tag, $val, %flags);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | #------------------------------------------------------------------------------
|
---|
97 | # Extract meta information from a JSON file
|
---|
98 | # Inputs: 0) ExifTool object reference, 1) dirInfo reference
|
---|
99 | # Returns: 1 on success, 0 if this wasn't a recognized JSON file
|
---|
100 | sub ProcessJSON($$)
|
---|
101 | {
|
---|
102 | local $_;
|
---|
103 | my ($et, $dirInfo) = @_;
|
---|
104 | my $raf = $$dirInfo{RAF};
|
---|
105 | my $structOpt = $et->Options('Struct');
|
---|
106 | my (%database, $key, $tag);
|
---|
107 |
|
---|
108 | # read information from JSON file into database structure
|
---|
109 | my $err = Image::ExifTool::Import::ReadJSON($raf, \%database,
|
---|
110 | $et->Options('MissingTagValue'), $et->Options('Charset'));
|
---|
111 |
|
---|
112 | return 0 if $err or not %database;
|
---|
113 |
|
---|
114 | $et->SetFileType();
|
---|
115 |
|
---|
116 | my $tagTablePtr = GetTagTable('Image::ExifTool::JSON::Main');
|
---|
117 |
|
---|
118 | # remove any old tag definitions in case they change flags
|
---|
119 | foreach $key (TagTableKeys($tagTablePtr)) {
|
---|
120 | delete $$tagTablePtr{$key} if $$tagTablePtr{$key}{Temporary};
|
---|
121 | }
|
---|
122 |
|
---|
123 | # extract tags from JSON database
|
---|
124 | foreach $key (sort keys %database) {
|
---|
125 | foreach $tag (sort keys %{$database{$key}}) {
|
---|
126 | my $val = $database{$key}{$tag};
|
---|
127 | # (ignore SourceFile if generated automatically by ReadJSON)
|
---|
128 | next if $tag eq 'SourceFile' and defined $val and $val eq '*';
|
---|
129 | ProcessTag($et, $tagTablePtr, $tag, $val);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | return 1;
|
---|
133 | }
|
---|
134 |
|
---|
135 | 1; # end
|
---|
136 |
|
---|
137 | __END__
|
---|
138 |
|
---|
139 | =head1 NAME
|
---|
140 |
|
---|
141 | Image::ExifTool::JSON - Read JSON files
|
---|
142 |
|
---|
143 | =head1 SYNOPSIS
|
---|
144 |
|
---|
145 | This module is used by Image::ExifTool
|
---|
146 |
|
---|
147 | =head1 DESCRIPTION
|
---|
148 |
|
---|
149 | This module contains definitions required by Image::ExifTool read
|
---|
150 | information from JSON files.
|
---|
151 |
|
---|
152 | =head1 AUTHOR
|
---|
153 |
|
---|
154 | Copyright 2003-2021, Phil Harvey (philharvey66 at gmail.com)
|
---|
155 |
|
---|
156 | This library is free software; you can redistribute it and/or modify it
|
---|
157 | under the same terms as Perl itself.
|
---|
158 |
|
---|
159 | =head1 SEE ALSO
|
---|
160 |
|
---|
161 | L<Image::ExifTool::TagNames/JSON Tags>,
|
---|
162 | L<Image::ExifTool(3pm)|Image::ExifTool>
|
---|
163 |
|
---|
164 | =cut
|
---|
165 |
|
---|