Add support for -H and -D to JSON

Started by dcampbell, April 01, 2013, 10:37:25 AM

Previous topic - Next topic

dcampbell

Hi,

I would like the JSON output (-j) to support the -D and -H flags. I have attached a patch for exiftool version 9.24.

Phil Harvey

Thanks for this patch, it looks useful.

What about also printing non-numeric tag ID's as is done with XML (-X -H) ?

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Phil Harvey

I have added the functionality of this patch, but I had to make some changes because the -php option uses the same code.  It now also prints non-numerical tag ID's.  The patch from the original exiftool is:

                 $noDups{$tok} = 1;
                 print $fp ',' if $comma;
                 print $fp qq(\n$ind"$tok"$sep );
+                if ($showTagID) {
+                    my $id = $exifTool->GetTagID($tag);
+                    $id = sprintf('0x%.4x', $id) if $showTagID eq 'H' and $id =~ /^\d+$/;
+                    $val = { id => $id, val => $val };
+                }
                 FormatJSON($fp, $val, $ind);
                 $comma = 1;
                 next;


- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

dcampbell

Thanks, this essentially looks good, but -j -H will now print decimal values for some tags, for example MPImage{Flags,Format,Type}, which have fractional decimal values for tag ID's. It would make more sense if their tag ID's were 0 as they are with the text output and -H flag, or if they were strings.

Phil Harvey

I did it this way because that is how it is done for the -X output.  The plain text output is different just to keep the columns lined up nicely.  But thinking about this more, the decimal values should technically be strings (for both -D and -H) because "12.1" is different than "12.10".  But quoting these would be a royal pain because then I couldn't use my FormatJSON() routine. :(

I'm not sure if the gain is worth the pain.  Does it impact you personally?

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

dcampbell

I am migrating some code that was using the plain text output to json so that newline characters will not be lost, so the decimal/string issue may affect us, but even without strings we have more information about the tags than we did before and the decimal values are not hard to check for. So, while I like the idea of -H not returning decimal values and not equal tag IDs not being equal, I don't have a real world example of something it breaks.

Phil Harvey

OK.  It isn't pretty, but with a simple hack to FormatJSON() I can string-ify all decimal ID's (for both -D and -H).  I'll go ahead with this unless you have a better suggestion.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

dcampbell

I don't have a better idea, and I don't know enough about ID's and exiftool to know whether turning all of the decimal values into strings could be harmful.

Phil Harvey

Actually, I think I may have been wrong about "12.1" being different from "12.10".  (I was thinking about tag values, where this does occur.  For ID's this doesn't happen).  So they should probably be numbers with -D, and strings with -H.  Darn.  More complications.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).