NewPipeExtractor/extractor/src/main/java/org/schabi/newpipe/extractor/utils/JsonUtils.java

74 lines
2.3 KiB
Java

package org.schabi.newpipe.extractor.utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
public class JsonUtils {
private JsonUtils() {
}
@Nonnull
public static Object getValue(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
List<String> keys = Arrays.asList(path.split("\\."));
object = getObject(object, keys.subList(0, keys.size() - 1));
if (null == object) throw new ParsingException("Unable to get " + path);
Object result = object.get(keys.get(keys.size() - 1));
if(null == result) throw new ParsingException("Unable to get " + path);
return result;
}
@Nonnull
public static String getString(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
Object value = getValue(object, path);
if(value instanceof String) {
return (String) getValue(object, path);
}else {
throw new ParsingException("Unable to get " + path);
}
}
@Nonnull
public static Number getNumber(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
Object value = getValue(object, path);
if(value instanceof Number) {
return (Number) getValue(object, path);
}else {
throw new ParsingException("Unable to get " + path);
}
}
@Nonnull
public static List<Object> getValues(@Nonnull JsonArray array, @Nonnull String path) throws ParsingException {
List<Object> result = new ArrayList<>();
for (int i = 0; i < array.size(); i++) {
JsonObject obj = array.getObject(i);
result.add(getValue(obj, path));
}
return result;
}
@Nullable
private static JsonObject getObject(@Nonnull JsonObject object, @Nonnull List<String> keys) {
JsonObject result = object;
for (String key : keys) {
result = result.getObject(key);
if (null == result) break;
}
return result;
}
}