diff --git a/source/net/filebot/util/JsonUtilities.java b/source/net/filebot/util/JsonUtilities.java index 557dfeaa..a0b75354 100644 --- a/source/net/filebot/util/JsonUtilities.java +++ b/source/net/filebot/util/JsonUtilities.java @@ -14,8 +14,8 @@ public class JsonUtilities { public static final Object[] EMPTY_ARRAY = new Object[0]; - public static Map readJson(CharSequence json) { - return (Map) JsonReader.jsonToJava(json.toString(), singletonMap(JsonReader.USE_MAPS, true)); + public static Object readJson(CharSequence json) { + return JsonReader.jsonToJava(json.toString(), singletonMap(JsonReader.USE_MAPS, true)); } public static Map asMap(Object node) { diff --git a/source/net/filebot/web/AcoustIDClient.java b/source/net/filebot/web/AcoustIDClient.java index 1aff6d8e..c5ebb8fc 100644 --- a/source/net/filebot/web/AcoustIDClient.java +++ b/source/net/filebot/web/AcoustIDClient.java @@ -107,10 +107,10 @@ public class AcoustIDClient implements MusicIdentificationService { } public AudioTrack parseResult(String json, final int targetDuration) throws IOException { - Map data = readJson(json); + Object data = readJson(json); - if (!data.get("status").equals("ok")) { - throw new IOException("acoustid responded with error: " + data.get("status")); + if (!"ok".equals(getString(data, "status"))) { + throw new IOException("acoustid responded with error: " + getString(data, "status")); } for (Object result : getArray(data, "results")) { diff --git a/source/net/filebot/web/CachedJsonResource.java b/source/net/filebot/web/CachedJsonResource.java index 1db96181..0cf40dd0 100644 --- a/source/net/filebot/web/CachedJsonResource.java +++ b/source/net/filebot/web/CachedJsonResource.java @@ -1,5 +1,7 @@ package net.filebot.web; +import static net.filebot.util.JsonUtilities.*; + import java.io.IOException; import java.net.URL; import java.nio.ByteBuffer; @@ -8,9 +10,6 @@ import java.nio.charset.StandardCharsets; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; -import com.cedarsoftware.util.io.JsonObject; -import com.cedarsoftware.util.io.JsonReader; - public class CachedJsonResource extends AbstractCachedResource { public CachedJsonResource(String resource) { @@ -22,9 +21,9 @@ public class CachedJsonResource extends AbstractCachedResource { return CacheManager.getInstance().getCache("web-datasource-lv3"); } - public JsonObject getJSON() throws IOException { + public Object getJsonObject() throws IOException { try { - return (JsonObject) JsonReader.jsonToMaps(get()); + return readJson(get()); } catch (Exception e) { throw new IOException(String.format("Error while loading JSON resource: %s (%s)", getResourceLocation(resource), e.getMessage())); } @@ -33,11 +32,11 @@ public class CachedJsonResource extends AbstractCachedResource { @Override public String process(String data) throws IOException { try { - JsonReader.jsonToMaps(data); // make sure JSON is valid + readJson(get()); // make sure JSON is valid + return data; } catch (Exception e) { throw new IOException(String.format("Malformed JSON: %s (%s)", getResourceLocation(resource), e.getMessage())); } - return data; } @Override diff --git a/source/net/filebot/web/FanartTVClient.java b/source/net/filebot/web/FanartTVClient.java index cac6b79a..283e2bb6 100644 --- a/source/net/filebot/web/FanartTVClient.java +++ b/source/net/filebot/web/FanartTVClient.java @@ -42,7 +42,9 @@ public class FanartTVClient { @Override public FanartDescriptor[] process(ByteBuffer data) throws Exception { - return readJson(UTF_8.decode(data)).entrySet().stream().flatMap(it -> { + Object json = readJson(UTF_8.decode(data)); + + return asMap(json).entrySet().stream().flatMap(it -> { return stream(asMapArray(it.getValue())).map(item -> { Map fields = new EnumMap(FanartProperty.class); fields.put(FanartProperty.type, it.getKey().toString()); diff --git a/source/net/filebot/web/OMDbClient.java b/source/net/filebot/web/OMDbClient.java index e4ea294a..bf08a748 100644 --- a/source/net/filebot/web/OMDbClient.java +++ b/source/net/filebot/web/OMDbClient.java @@ -171,7 +171,7 @@ public class OMDbClient implements MovieIdentificationService { } }; - return readJson(json.get()); + return asMap(readJson(json.get())); } public Map getMovieInfo(Integer i, String t, String y, boolean tomatoes) throws IOException { diff --git a/source/net/filebot/web/TVMazeClient.java b/source/net/filebot/web/TVMazeClient.java index 28d979ed..f66eca2b 100644 --- a/source/net/filebot/web/TVMazeClient.java +++ b/source/net/filebot/web/TVMazeClient.java @@ -117,7 +117,7 @@ public class TVMazeClient extends AbstractEpisodeListProvider { } public Object request(String resource) throws IOException { - return new CachedJsonResource("http://api.tvmaze.com/" + resource).getJSON(); + return new CachedJsonResource("http://api.tvmaze.com/" + resource).getJsonObject(); } @Override