Fix json-io parse issues

This commit is contained in:
Reinhard Pointner 2016-03-06 14:42:15 +00:00
parent 335c857688
commit 5156a0f042
6 changed files with 16 additions and 15 deletions

View File

@ -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) {

View File

@ -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")) {

View File

@ -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<String, String> {
public CachedJsonResource(String resource) {
@ -22,9 +21,9 @@ public class CachedJsonResource extends AbstractCachedResource<String, String> {
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<String, String> {
@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

View File

@ -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<FanartProperty, String> fields = new EnumMap<FanartProperty, String>(FanartProperty.class);
fields.put(FanartProperty.type, it.getKey().toString());

View File

@ -171,7 +171,7 @@ public class OMDbClient implements MovieIdentificationService {
}
};
return readJson(json.get());
return asMap(readJson(json.get()));
}
public Map<String, String> getMovieInfo(Integer i, String t, String y, boolean tomatoes) throws IOException {

View File

@ -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