Fix checkstyle issues & more in JsonUtils
Also use Java 8 streams and extract duplicate code to getInstanceOf function
This commit is contained in:
parent
87d2834986
commit
1d5f22e41f
|
@ -12,11 +12,12 @@ import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
public class JsonUtils {
|
public final class JsonUtils {
|
||||||
public static final JsonObject EMPTY_OBJECT = new JsonObject();
|
public static final JsonObject EMPTY_OBJECT = new JsonObject();
|
||||||
public static final JsonArray EMPTY_ARRAY = new JsonArray();
|
public static final JsonArray EMPTY_ARRAY = new JsonArray();
|
||||||
|
|
||||||
|
@ -24,83 +25,85 @@ public class JsonUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static Object getValue(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
|
public static Object getValue(@Nonnull final JsonObject object,
|
||||||
|
@Nonnull final String path) throws ParsingException {
|
||||||
|
|
||||||
List<String> keys = Arrays.asList(path.split("\\."));
|
final List<String> keys = Arrays.asList(path.split("\\."));
|
||||||
object = getObject(object, keys.subList(0, keys.size() - 1));
|
final JsonObject parentObject = getObject(object, keys.subList(0, keys.size() - 1));
|
||||||
if (null == object) throw new ParsingException("Unable to get " + path);
|
if (parentObject == null) {
|
||||||
Object result = object.get(keys.get(keys.size() - 1));
|
throw new ParsingException("Unable to get " + path);
|
||||||
if (null == result) throw new ParsingException("Unable to get " + path);
|
}
|
||||||
|
|
||||||
|
final Object result = object.get(keys.get(keys.size() - 1));
|
||||||
|
if (result == null) {
|
||||||
|
throw new ParsingException("Unable to get " + path);
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
private static <T> T getInstanceOf(@Nonnull final JsonObject object,
|
||||||
public static String getString(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
|
@Nonnull final String path,
|
||||||
Object value = getValue(object, path);
|
@Nonnull final Class<T> klass) throws ParsingException {
|
||||||
if (value instanceof String) {
|
final Object value = getValue(object, path);
|
||||||
return (String) value;
|
if (klass.isInstance(value)) {
|
||||||
|
return klass.cast(value);
|
||||||
} else {
|
} else {
|
||||||
throw new ParsingException("Unable to get " + path);
|
throw new ParsingException("Wrong data type at path " + path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static Boolean getBoolean(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
|
public static String getString(@Nonnull final JsonObject object, @Nonnull final String path)
|
||||||
Object value = getValue(object, path);
|
throws ParsingException {
|
||||||
if (value instanceof Boolean) {
|
return getInstanceOf(object, path, String.class);
|
||||||
return (Boolean) value;
|
|
||||||
} else {
|
|
||||||
throw new ParsingException("Unable to get " + path);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static Number getNumber(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
|
public static Boolean getBoolean(@Nonnull final JsonObject object,
|
||||||
Object value = getValue(object, path);
|
@Nonnull final String path) throws ParsingException {
|
||||||
if (value instanceof Number) {
|
return getInstanceOf(object, path, Boolean.class);
|
||||||
return (Number) value;
|
|
||||||
} else {
|
|
||||||
throw new ParsingException("Unable to get " + path);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static JsonObject getObject(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
|
public static Number getNumber(@Nonnull final JsonObject object,
|
||||||
Object value = getValue(object, path);
|
@Nonnull final String path)
|
||||||
if (value instanceof JsonObject) {
|
throws ParsingException {
|
||||||
return (JsonObject) value;
|
return getInstanceOf(object, path, Number.class);
|
||||||
} else {
|
|
||||||
throw new ParsingException("Unable to get " + path);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static JsonArray getArray(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
|
public static JsonObject getObject(@Nonnull final JsonObject object,
|
||||||
Object value = getValue(object, path);
|
@Nonnull final String path) throws ParsingException {
|
||||||
if (value instanceof JsonArray) {
|
return getInstanceOf(object, path, JsonObject.class);
|
||||||
return (JsonArray) value;
|
|
||||||
} else {
|
|
||||||
throw new ParsingException("Unable to get " + path);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static List<Object> getValues(@Nonnull JsonArray array, @Nonnull String path) throws ParsingException {
|
public static JsonArray getArray(@Nonnull final JsonObject object, @Nonnull final String path)
|
||||||
|
throws ParsingException {
|
||||||
|
return getInstanceOf(object, path, JsonArray.class);
|
||||||
|
}
|
||||||
|
|
||||||
List<Object> result = new ArrayList<>();
|
@Nonnull
|
||||||
|
public static List<Object> getValues(@Nonnull final JsonArray array, @Nonnull final String path)
|
||||||
|
throws ParsingException {
|
||||||
|
|
||||||
|
final List<Object> result = new ArrayList<>();
|
||||||
for (int i = 0; i < array.size(); i++) {
|
for (int i = 0; i < array.size(); i++) {
|
||||||
JsonObject obj = array.getObject(i);
|
final JsonObject obj = array.getObject(i);
|
||||||
result.add(getValue(obj, path));
|
result.add(getValue(obj, path));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private static JsonObject getObject(@Nonnull JsonObject object, @Nonnull List<String> keys) {
|
private static JsonObject getObject(@Nonnull final JsonObject object,
|
||||||
|
@Nonnull final List<String> keys) {
|
||||||
JsonObject result = object;
|
JsonObject result = object;
|
||||||
for (String key : keys) {
|
for (final String key : keys) {
|
||||||
result = result.getObject(key);
|
result = result.getObject(key);
|
||||||
if (null == result) break;
|
if (result == null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -108,7 +111,7 @@ public class JsonUtils {
|
||||||
public static JsonArray toJsonArray(final String responseBody) throws ParsingException {
|
public static JsonArray toJsonArray(final String responseBody) throws ParsingException {
|
||||||
try {
|
try {
|
||||||
return JsonParser.array().from(responseBody);
|
return JsonParser.array().from(responseBody);
|
||||||
} catch (JsonParserException e) {
|
} catch (final JsonParserException e) {
|
||||||
throw new ParsingException("Could not parse JSON", e);
|
throw new ParsingException("Could not parse JSON", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -116,7 +119,7 @@ public class JsonUtils {
|
||||||
public static JsonObject toJsonObject(final String responseBody) throws ParsingException {
|
public static JsonObject toJsonObject(final String responseBody) throws ParsingException {
|
||||||
try {
|
try {
|
||||||
return JsonParser.object().from(responseBody);
|
return JsonParser.object().from(responseBody);
|
||||||
} catch (JsonParserException e) {
|
} catch (final JsonParserException e) {
|
||||||
throw new ParsingException("Could not parse JSON", e);
|
throw new ParsingException("Could not parse JSON", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -128,10 +131,12 @@ public class JsonUtils {
|
||||||
* <p>Example HTML:</p>
|
* <p>Example HTML:</p>
|
||||||
* <pre>
|
* <pre>
|
||||||
* {@code
|
* {@code
|
||||||
* <p data-town="{"name":"Mycenae","country":"Greece"}">This is Sparta!</p>
|
* <p data-town="{"name":"Mycenae","country":"Greece"}">
|
||||||
|
* This is Sparta!</p>
|
||||||
* }
|
* }
|
||||||
* </pre>
|
* </pre>
|
||||||
* <p>Calling this function to get the attribute <code>data-town</code> returns the JsonObject for</p>
|
* <p>Calling this function to get the attribute <code>data-town</code> returns the JsonObject
|
||||||
|
* for</p>
|
||||||
* <pre>
|
* <pre>
|
||||||
* {@code
|
* {@code
|
||||||
* {
|
* {
|
||||||
|
@ -140,6 +145,7 @@ public class JsonUtils {
|
||||||
* }
|
* }
|
||||||
* }
|
* }
|
||||||
* </pre>
|
* </pre>
|
||||||
|
*
|
||||||
* @param html The HTML where the JSON we're looking for is stored inside a
|
* @param html The HTML where the JSON we're looking for is stored inside a
|
||||||
* variable inside some JavaScript block
|
* variable inside some JavaScript block
|
||||||
* @param variable Name of the variable
|
* @param variable Name of the variable
|
||||||
|
@ -153,12 +159,9 @@ public class JsonUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<String> getStringListFromJsonArray(@Nonnull final JsonArray array) {
|
public static List<String> getStringListFromJsonArray(@Nonnull final JsonArray array) {
|
||||||
final List<String> stringList = new ArrayList<>(array.size());
|
return array.stream()
|
||||||
for (Object o : array) {
|
.filter(String.class::isInstance)
|
||||||
if (o instanceof String) {
|
.map(String.class::cast)
|
||||||
stringList.add((String) o);
|
.collect(Collectors.toList());
|
||||||
}
|
|
||||||
}
|
|
||||||
return stringList;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue