SKIP_NULL_FIELDS / MetaAttributes.toJson() / MetaAttributes.toObject()

This commit is contained in:
Reinhard Pointner 2016-10-09 03:55:45 +08:00
parent b9f0a9ef0e
commit 202c9d2dd5
5 changed files with 30 additions and 29 deletions

View File

@ -26,14 +26,12 @@ import java.util.TreeSet;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import com.cedarsoftware.util.io.JsonReader;
import com.cedarsoftware.util.io.JsonWriter;
import groovy.lang.Closure;
import groovy.lang.Range;
import net.filebot.MediaTypes;
import net.filebot.MetaAttributeView;
import net.filebot.media.MediaDetection;
import net.filebot.media.MetaAttributes;
import net.filebot.similarity.NameSimilarityMetric;
import net.filebot.similarity.Normalization;
import net.filebot.similarity.SimilarityMetric;
@ -352,11 +350,11 @@ public class ScriptShellMethods {
}
public static String objectToJson(Object self) throws IOException {
return JsonWriter.objectToJson(self);
return MetaAttributes.toJson(self);
}
public static Object jsonToObject(String self) throws IOException {
return JsonReader.jsonToJava(self);
return MetaAttributes.toObject(self);
}
public static File getStructurePathTail(File self) throws Exception {

View File

@ -36,8 +36,6 @@ import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;
import com.cedarsoftware.util.io.JsonWriter;
import net.filebot.ApplicationFolder;
import net.filebot.Cache;
import net.filebot.CacheType;
@ -47,6 +45,7 @@ import net.filebot.MetaAttributeView;
import net.filebot.Settings;
import net.filebot.WebServices;
import net.filebot.hash.HashType;
import net.filebot.media.MetaAttributes;
import net.filebot.media.NamingStandard;
import net.filebot.mediainfo.MediaInfo;
import net.filebot.mediainfo.MediaInfo.StreamKind;
@ -990,7 +989,7 @@ public class MediaBindingBean {
@Define("json")
public String getInfoObjectDump() {
return JsonWriter.objectToJson(infoObject);
return MetaAttributes.toJson(infoObject);
}
public File getInferredMediaFile() {

View File

@ -60,7 +60,7 @@ public class MetaAttributes {
public void setObject(Object object) {
try {
metaAttributeView.put(METADATA_KEY, JsonWriter.objectToJson(object, singletonMap(JsonWriter.TYPE_NAME_MAP, JSON_TYPE_MAP)));
metaAttributeView.put(METADATA_KEY, toJson(object));
} catch (Exception e) {
throw new RuntimeException(e);
}
@ -68,13 +68,9 @@ public class MetaAttributes {
public Object getObject() {
try {
String jsonObject = metaAttributeView.get(METADATA_KEY);
if (jsonObject != null && jsonObject.length() > 0) {
Map<String, Object> options = new HashMap<String, Object>(2);
options.put(JsonReader.TYPE_NAME_MAP, JSON_TYPE_MAP);
// options must be a modifiable map
return JsonReader.jsonToJava(jsonObject, options);
String json = metaAttributeView.get(METADATA_KEY);
if (json != null && json.length() > 0) {
return toObject(json);
}
} catch (Exception e) {
throw new RuntimeException(e);
@ -87,4 +83,20 @@ public class MetaAttributes {
metaAttributeView.put(METADATA_KEY, null);
}
public static String toJson(Object object) {
Map<String, Object> options = new HashMap<String, Object>();
options.put(JsonWriter.TYPE_NAME_MAP, MetaAttributes.JSON_TYPE_MAP);
options.put(JsonWriter.SKIP_NULL_FIELDS, true);
return JsonWriter.objectToJson(object, options);
}
public static Object toObject(String json) {
Map<String, Object> options = new HashMap<String, Object>();
options.put(JsonReader.TYPE_NAME_MAP, JSON_TYPE_MAP);
// options must be a modifiable map
return JsonReader.jsonToJava(json, options);
}
}

View File

@ -58,9 +58,6 @@ import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rtextarea.RTextScrollPane;
import com.cedarsoftware.util.io.JsonReader;
import com.cedarsoftware.util.io.JsonWriter;
import net.filebot.ResourceManager;
import net.filebot.Settings;
import net.filebot.UserFiles;
@ -68,6 +65,7 @@ import net.filebot.format.BindingException;
import net.filebot.format.ExpressionFormat;
import net.filebot.format.MediaBindingBean;
import net.filebot.mac.MacAppUtilities;
import net.filebot.media.MetaAttributes;
import net.filebot.mediainfo.MediaInfo;
import net.filebot.util.DefaultThreadFactory;
import net.filebot.util.ExceptionUtilities;
@ -397,7 +395,7 @@ public class FormatDialog extends JDialog {
try {
// restore sample from user preferences
String sample = mode.persistentSample().getValue();
info = JsonReader.jsonToJava(sample);
info = MetaAttributes.toObject(sample);
if (info == null) {
throw new NullPointerException();
}
@ -406,7 +404,7 @@ public class FormatDialog extends JDialog {
// restore sample from application properties
ResourceBundle bundle = ResourceBundle.getBundle(getClass().getName());
String sample = bundle.getString(mode.key() + ".sample");
info = JsonReader.jsonToJava(sample);
info = MetaAttributes.toObject(sample);
} catch (Exception illegalSample) {
debug.log(Level.SEVERE, "Illegal Sample", e);
}
@ -594,7 +592,7 @@ public class FormatDialog extends JDialog {
// remember sample
try {
mode.persistentSample().setValue(info == null ? "" : JsonWriter.objectToJson(info));
mode.persistentSample().setValue(info == null ? "" : MetaAttributes.toJson(info));
persistentSampleFile.setValue(file == null ? "" : sample.getFileObject().getAbsolutePath());
} catch (Exception e) {
debug.log(Level.WARNING, e.getMessage(), e);

View File

@ -21,7 +21,6 @@ import java.awt.event.ActionEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
@ -50,7 +49,6 @@ import javax.swing.SwingWorker;
import javax.swing.border.CompoundBorder;
import javax.swing.border.TitledBorder;
import com.cedarsoftware.util.io.JsonWriter;
import com.google.common.eventbus.Subscribe;
import ca.odell.glazedlists.EventList;
@ -622,10 +620,6 @@ public class RenamePanel extends JComponent {
private String getDebugInfo() throws Exception {
StringBuilder sb = new StringBuilder();
Map<String, Object> options = new HashMap<String, Object>(2);
options.put(JsonWriter.TYPE_NAME_MAP, MetaAttributes.JSON_TYPE_MAP);
options.put(JsonWriter.SKIP_NULL_FIELDS, true);
for (Match<Object, File> m : renameModel.matches()) {
String f = getStructurePathTail(m.getCandidate()).getPath();
Object v = m.getValue();
@ -635,7 +629,7 @@ public class RenamePanel extends JComponent {
v = new SimpleFileInfo(getStructurePathTail((File) v).getPath(), ((File) v).length());
}
sb.append(f).append('\t').append(JsonWriter.objectToJson(v, options)).append('\n');
sb.append(f).append('\t').append(MetaAttributes.toJson(v)).append('\n');
}
return sb.toString();