Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make tags serializable for disk caching #102

Closed
vjpr opened this issue Aug 22, 2021 · 1 comment
Closed

Make tags serializable for disk caching #102

vjpr opened this issue Aug 22, 2021 · 1 comment

Comments

@vjpr
Copy link

vjpr commented Aug 22, 2021

Is your feature request related to a problem? Please describe.

I want to cache tags to disk. That is, write exiftool.read results to JSON file on disk.

The results of exiftool.read involve DateTime objects though which makes deserialization difficult.

Describe the solution you'd like

I'd like for be able to call ExifTool.fromJSON(json), to get the original structure back. And maybe a toJSON also.

Describe alternatives you've considered

function exifDateTimeFromJson(dt) {
  return new ExifDateTime(
    dt.year,
    dt.month,
    dt.day,
    dt.hour,
    dt.minute,
    dt.second,
    dt.millisecond,
    dt.offset === UnsetZoneOffsetMinutes ? undefined : dt.offset,
    dt.rawValue,
    dt.zone?.name === UnsetZone.name ? undefined : dt.zoneName,
  )
}
@mceachen
Copy link
Member

mceachen commented Aug 22, 2021

I don't need this, but I think it's a reasonable request. If you want to take a crack at this, I'd be happy to review your PR!

Your API suggestion (ExifTool.toJSON(tags: Tags): string and ExifTool.fromJSON(json: string): Tags) sound good to me.

I've added .toJSON methods to the ExifDateTime, ExifDate, and ExifTime classes, which means that JSON.stringify() "just works".

To reconstitute from JSON, use the new exported parseJSON method:

import { exiftool, parseJSON } from "exiftool-vendored"

const tags: Tags = await exiftool.readTags("/path/to/file.jpg")
const str: string = JSON.stringify(tags)

// parseJSON doesn't validate the input, so we don't assert that it's a Tags
// instance, but you can cast it (unsafely...)

const tags2: Tags = parseJSON(str) as Tags 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants