From d06352e5072d061c32ec5abc065ecb0797d054b3 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 13 Aug 2016 23:07:24 +0800 Subject: [PATCH] Simplify json xattr metadata --- source/net/filebot/media/MetaAttributes.java | 17 ++++++++++++++--- source/net/filebot/media/XattrMetaInfo.java | 9 ++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/source/net/filebot/media/MetaAttributes.java b/source/net/filebot/media/MetaAttributes.java index 57632e4b..cc6e433c 100644 --- a/source/net/filebot/media/MetaAttributes.java +++ b/source/net/filebot/media/MetaAttributes.java @@ -1,10 +1,14 @@ package net.filebot.media; +import static java.util.Collections.*; + import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.attribute.BasicFileAttributeView; import java.nio.file.attribute.FileTime; +import java.util.HashMap; +import java.util.Map; import com.cedarsoftware.util.io.JsonReader; import com.cedarsoftware.util.io.JsonWriter; @@ -19,9 +23,12 @@ public class MetaAttributes { private final BasicFileAttributeView fileAttributeView; private final MetaAttributeView metaAttributeView; - public MetaAttributes(File file) throws IOException { + private final Map jsonTypeMap; + + public MetaAttributes(File file, Map jsonTypeMap) throws IOException { this.metaAttributeView = new MetaAttributeView(file); this.fileAttributeView = Files.getFileAttributeView(file.toPath(), BasicFileAttributeView.class); + this.jsonTypeMap = jsonTypeMap; } public void setCreationDate(long millis) throws IOException { @@ -46,7 +53,7 @@ public class MetaAttributes { public void setObject(Object object) { try { - metaAttributeView.put(METADATA_KEY, JsonWriter.objectToJson(object)); + metaAttributeView.put(METADATA_KEY, JsonWriter.objectToJson(object, singletonMap(JsonWriter.TYPE_NAME_MAP, jsonTypeMap))); } catch (Exception e) { throw new RuntimeException(e); } @@ -56,7 +63,11 @@ public class MetaAttributes { try { String jsonObject = metaAttributeView.get(METADATA_KEY); if (jsonObject != null && jsonObject.length() > 0) { - return JsonReader.jsonToJava(jsonObject); + Map options = new HashMap(2); + options.put(JsonReader.TYPE_NAME_MAP, jsonTypeMap); + + // options must be a modifiable map + return JsonReader.jsonToJava(jsonObject, options); } } catch (Exception e) { throw new RuntimeException(e); diff --git a/source/net/filebot/media/XattrMetaInfo.java b/source/net/filebot/media/XattrMetaInfo.java index 0b4e0aa4..531bd6e7 100644 --- a/source/net/filebot/media/XattrMetaInfo.java +++ b/source/net/filebot/media/XattrMetaInfo.java @@ -1,19 +1,24 @@ package net.filebot.media; +import static java.util.stream.Collectors.*; import static net.filebot.Logging.*; import static net.filebot.Settings.*; import static net.filebot.util.ExceptionUtilities.*; import java.io.File; import java.util.Locale; +import java.util.Map; import java.util.function.Function; +import java.util.stream.Stream; import net.filebot.Cache; import net.filebot.CacheType; import net.filebot.Resource; import net.filebot.WebServices; +import net.filebot.web.AudioTrack; import net.filebot.web.Episode; import net.filebot.web.Movie; +import net.filebot.web.MoviePart; import net.filebot.web.SimpleDate; public class XattrMetaInfo { @@ -26,6 +31,8 @@ public class XattrMetaInfo { private final Cache xattrMetaInfoCache = Cache.getCache(MetaAttributes.METADATA_KEY, CacheType.Ephemeral); private final Cache xattrOriginalNameCache = Cache.getCache(MetaAttributes.FILENAME_KEY, CacheType.Ephemeral); + private final Map jsonTypeMap = Stream.of(Episode.class, Movie.class, MoviePart.class, AudioTrack.class).collect(toMap(Class::getName, Class::getSimpleName)); + public XattrMetaInfo(boolean useExtendedFileAttributes, boolean useCreationDate) { this.useExtendedFileAttributes = useExtendedFileAttributes; this.useCreationDate = useCreationDate; @@ -76,7 +83,7 @@ public class XattrMetaInfo { } private MetaAttributes xattr(File file) throws Exception { - return new MetaAttributes(file); + return new MetaAttributes(file, jsonTypeMap); } public synchronized void setMetaInfo(File file, Object model, String original) {